Second Commit

parent f458b68d
...@@ -2,7 +2,9 @@ package sacooliveros.whatsappweb; ...@@ -2,7 +2,9 @@ package sacooliveros.whatsappweb;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication @SpringBootApplication
public class WhatsappwebApplication { public class WhatsappwebApplication {
......
...@@ -3,6 +3,7 @@ package sacooliveros.whatsappweb.api; ...@@ -3,6 +3,7 @@ package sacooliveros.whatsappweb.api;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.catalina.connector.Response; import org.apache.catalina.connector.Response;
import org.json.JSONObject;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
...@@ -23,16 +24,19 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils; ...@@ -23,16 +24,19 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils;
public class WhatsappRest { public class WhatsappRest {
Logger logger = Logger.getLogger(WhatsappRest.class.getName()); Logger logger = Logger.getLogger(WhatsappRest.class.getName());
Whatsapp whatsapp;
WhatsappBean whatsappBean;
WhatsappService whatsappService; WhatsappService whatsappService;
WhatsappUtils utils;
public WhatsappRest(WhatsappBean whatsapp, WhatsappService whatsappService) { public WhatsappRest(WhatsappBean whatsappBean, WhatsappService whatsappService) {
this.whatsapp = whatsapp.getWhatsapp(); this.whatsappBean = whatsappBean;
this.whatsappService = whatsappService; this.whatsappService = whatsappService;
} }
@GetMapping("qr") @GetMapping("qr")
public ResponseEntity<?> getQR() { public ResponseEntity<?> getQR() {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
WhatsappUtils utils = new WhatsappUtils();
if (whatsapp == null) { if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body(utils.generateQR()); return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
} }
...@@ -46,29 +50,31 @@ public class WhatsappRest { ...@@ -46,29 +50,31 @@ public class WhatsappRest {
@GetMapping("logout") @GetMapping("logout")
public ResponseEntity<?> logout() { public ResponseEntity<?> logout() {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
if (whatsapp == null) { 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()); System.out.println(whatsapp.store().about());
try { try {
if(whatsapp.isConnected()){ if(whatsapp.isConnected()){
whatsapp.disconnect().join(); whatsapp.disconnect().join();
whatsapp.logout().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{ }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) { } catch (Exception e) {
logger.severe(e.getMessage()); 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) @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) { public ResponseEntity<?> send(@RequestParam Map<String, String> payload, @RequestParam Map<String, MultipartFile> files) {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
if (whatsapp == null) { 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{ try{
whatsapp.connect().join(); whatsapp.connect().join();
...@@ -76,11 +82,11 @@ public class WhatsappRest { ...@@ -76,11 +82,11 @@ public class WhatsappRest {
whatsappService.send(payload, files, whatsapp); whatsappService.send(payload, files, whatsapp);
return ResponseEntity.status(Response.SC_OK).body(payload); return ResponseEntity.status(Response.SC_OK).body(payload);
}else{ }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){ }catch (Exception e){
logger.severe(e.getMessage()); 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; ...@@ -2,6 +2,8 @@ package sacooliveros.whatsappweb.bean;
import java.util.Optional; import java.util.Optional;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import it.auties.whatsapp.api.Whatsapp; import it.auties.whatsapp.api.Whatsapp;
...@@ -9,15 +11,26 @@ import it.auties.whatsapp.api.Whatsapp; ...@@ -9,15 +11,26 @@ import it.auties.whatsapp.api.Whatsapp;
@Component @Component
public class WhatsappBean { public class WhatsappBean {
Optional<Whatsapp> w = Whatsapp.webBuilder().firstConnection().registered(); Optional<Whatsapp> w = Whatsapp.webBuilder().lastConnection().registered();
@Bean
public Whatsapp getWhatsapp() { public Whatsapp getWhatsapp() {
if(w.isEmpty()){ return w.orElse(null);
w = Whatsapp.webBuilder().lastConnection().registered(); }
return w.orElse(null);
@Scheduled(fixedRate = 1000 * 60 )
public void refreshConnection() {
if (w.isPresent()) {
if (!w.get().isConnected()) {
w.get().connect().join();
}
}else{ }else{
return w.get(); w = Optional.empty();
} }
} }
public void setWhatsapp(Whatsapp whatsapp) {
w = Optional.ofNullable(whatsapp);
}
} }
...@@ -71,8 +71,8 @@ public class WhatsappService { ...@@ -71,8 +71,8 @@ public class WhatsappService {
AudioMessage audioMessage = new AudioMessageSimpleBuilder() AudioMessage audioMessage = new AudioMessageSimpleBuilder()
.media(bytes) .media(bytes)
.build(); .build();
whatsapp.sendMessage(contact,audioMessage).get(10, TimeUnit.SECONDS); whatsapp.sendMessage(contact,audioMessage).join();
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
...@@ -84,8 +84,8 @@ public class WhatsappService { ...@@ -84,8 +84,8 @@ public class WhatsappService {
.media(bytes) .media(bytes)
.fileName(file.getOriginalFilename()) .fileName(file.getOriginalFilename())
.build(); .build();
whatsapp.sendMessage(contact,documentMessage).get(10, TimeUnit.SECONDS); whatsapp.sendMessage(contact,documentMessage).join();
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
......
...@@ -10,17 +10,15 @@ import java.util.logging.Logger; ...@@ -10,17 +10,15 @@ import java.util.logging.Logger;
import it.auties.whatsapp.api.QrHandler; import it.auties.whatsapp.api.QrHandler;
import it.auties.whatsapp.api.Whatsapp; import it.auties.whatsapp.api.Whatsapp;
import sacooliveros.whatsappweb.bean.WhatsappBean;
public class WhatsappUtils { public class WhatsappUtils {
Whatsapp whatsapp;
Logger logger = Logger.getLogger(WhatsappUtils.class.getName()); Logger logger = Logger.getLogger(WhatsappUtils.class.getName());
public WhatsappUtils(Whatsapp whatsapp) {
this.whatsapp = whatsapp;
}
public String generateQR() { public String generateQR() {
WhatsappBean whatsappBean = new WhatsappBean();
StringBuilder qr = new StringBuilder(); StringBuilder qr = new StringBuilder();
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
...@@ -30,9 +28,15 @@ public class WhatsappUtils { ...@@ -30,9 +28,15 @@ public class WhatsappUtils {
}; };
try { try {
CompletableFuture.supplyAsync(() -> CompletableFuture.supplyAsync(() ->{
Whatsapp.webBuilder().newConnection().unregistered(QrHandler.toString(smallQrConsumer)).connect().join() try {
).get(10, TimeUnit.SECONDS); 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)) { if (!latch.await(10, TimeUnit.SECONDS)) {
throw new TimeoutException("QR generation timed out"); 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