Second Commit

parent f458b68d
......@@ -2,7 +2,9 @@ package sacooliveros.whatsappweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class WhatsappwebApplication {
......
......@@ -3,6 +3,7 @@ package sacooliveros.whatsappweb.api;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.catalina.connector.Response;
import org.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
......@@ -23,16 +24,19 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils;
public class WhatsappRest {
Logger logger = Logger.getLogger(WhatsappRest.class.getName());
Whatsapp whatsapp;
WhatsappBean whatsappBean;
WhatsappService whatsappService;
WhatsappUtils utils;
public WhatsappRest(WhatsappBean whatsapp, WhatsappService whatsappService) {
this.whatsapp = whatsapp.getWhatsapp();
public WhatsappRest(WhatsappBean whatsappBean, WhatsappService whatsappService) {
this.whatsappBean = whatsappBean;
this.whatsappService = whatsappService;
}
@GetMapping("qr")
public ResponseEntity<?> getQR() {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
WhatsappUtils utils = new WhatsappUtils();
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
}
......@@ -46,29 +50,31 @@ public class WhatsappRest {
@GetMapping("logout")
public ResponseEntity<?> logout() {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body("Not connected");
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Not connected.").put("status",false).toMap());
}
System.out.println(whatsapp.store().about());
try {
if(whatsapp.isConnected()){
whatsapp.disconnect().join();
whatsapp.logout().join();
return ResponseEntity.status(Response.SC_OK).body("Logged out");
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Logout has been done correctly, wait some minutes untill it actually logs out").put("status",false).toMap());
}else{
return ResponseEntity.status(Response.SC_OK).body("Not connected");
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Not connected.").put("status",false).toMap());
}
} catch (Exception e) {
logger.severe(e.getMessage());
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Logout failed");
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body(new JSONObject().put("message", "Logout has failed.").put("status",false).toMap());
}
}
@PostMapping(value = "send", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> send(@RequestParam Map<String, String> payload, @RequestParam Map<String, MultipartFile> files) {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body("Not connected");
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Whatsapp hasn't been initialized").put("status",false).toMap());
}
try{
whatsapp.connect().join();
......@@ -76,11 +82,11 @@ public class WhatsappRest {
whatsappService.send(payload, files, whatsapp);
return ResponseEntity.status(Response.SC_OK).body(payload);
}else{
return ResponseEntity.status(Response.SC_OK).body("Not connected");
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Not connected.").put("status",false).toMap());
}
}catch (Exception e){
logger.severe(e.getMessage());
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Send failed");
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body(new JSONObject().put("message", "Message hasn't been sent.").put("status",false).toMap());
}
}
......
......@@ -2,6 +2,8 @@ package sacooliveros.whatsappweb.bean;
import java.util.Optional;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import it.auties.whatsapp.api.Whatsapp;
......@@ -9,15 +11,26 @@ import it.auties.whatsapp.api.Whatsapp;
@Component
public class WhatsappBean {
Optional<Whatsapp> w = Whatsapp.webBuilder().firstConnection().registered();
Optional<Whatsapp> w = Whatsapp.webBuilder().lastConnection().registered();
@Bean
public Whatsapp getWhatsapp() {
if(w.isEmpty()){
w = Whatsapp.webBuilder().lastConnection().registered();
return w.orElse(null);
return w.orElse(null);
}
@Scheduled(fixedRate = 1000 * 60 )
public void refreshConnection() {
if (w.isPresent()) {
if (!w.get().isConnected()) {
w.get().connect().join();
}
}else{
return w.get();
w = Optional.empty();
}
}
public void setWhatsapp(Whatsapp whatsapp) {
w = Optional.ofNullable(whatsapp);
}
}
......@@ -71,8 +71,8 @@ public class WhatsappService {
AudioMessage audioMessage = new AudioMessageSimpleBuilder()
.media(bytes)
.build();
whatsapp.sendMessage(contact,audioMessage).get(10, TimeUnit.SECONDS);
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
whatsapp.sendMessage(contact,audioMessage).join();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
......@@ -84,8 +84,8 @@ public class WhatsappService {
.media(bytes)
.fileName(file.getOriginalFilename())
.build();
whatsapp.sendMessage(contact,documentMessage).get(10, TimeUnit.SECONDS);
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
whatsapp.sendMessage(contact,documentMessage).join();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
......
......@@ -10,17 +10,15 @@ import java.util.logging.Logger;
import it.auties.whatsapp.api.QrHandler;
import it.auties.whatsapp.api.Whatsapp;
import sacooliveros.whatsappweb.bean.WhatsappBean;
public class WhatsappUtils {
Whatsapp whatsapp;
Logger logger = Logger.getLogger(WhatsappUtils.class.getName());
public WhatsappUtils(Whatsapp whatsapp) {
this.whatsapp = whatsapp;
}
public String generateQR() {
WhatsappBean whatsappBean = new WhatsappBean();
StringBuilder qr = new StringBuilder();
CountDownLatch latch = new CountDownLatch(1);
......@@ -30,9 +28,15 @@ public class WhatsappUtils {
};
try {
CompletableFuture.supplyAsync(() ->
Whatsapp.webBuilder().newConnection().unregistered(QrHandler.toString(smallQrConsumer)).connect().join()
).get(10, TimeUnit.SECONDS);
CompletableFuture.supplyAsync(() ->{
try {
whatsappBean.setWhatsapp(Whatsapp.webBuilder().newConnection().unregistered(QrHandler.toString(smallQrConsumer)).connect().get(10, TimeUnit.SECONDS));
return true;
} catch (Exception e) {
return false;
}
}
);
if (!latch.await(10, TimeUnit.SECONDS)) {
throw new TimeoutException("QR generation timed out");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment