[ADD] AVANCE

parent 234713b1
......@@ -62,7 +62,11 @@
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<finalName>whatsappweb</finalName>
......
......@@ -4,6 +4,7 @@ import java.util.Map;
import java.util.logging.Logger;
import org.apache.catalina.connector.Response;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
......@@ -25,14 +26,11 @@ public class WhatsappRest {
Logger logger = Logger.getLogger(WhatsappRest.class.getName());
@Autowired
WhatsappBean whatsappBean;
@Autowired
WhatsappService whatsappService;
public WhatsappRest(WhatsappBean whatsappBean, WhatsappService whatsappService) {
this.whatsappBean = whatsappBean;
this.whatsappService = whatsappService;
}
@GetMapping("qr")
public ResponseEntity<?> getQR() {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
......@@ -44,7 +42,7 @@ public class WhatsappRest {
if (whatsapp.isConnected()) {
return ResponseEntity.status(Response.SC_OK).body("Already connected");
}else{
return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
return ResponseEntity.status(Response.SC_OK).body("Connecting...");
}
}
......@@ -59,6 +57,7 @@ public class WhatsappRest {
if(whatsapp.isConnected()){
whatsapp.disconnect().join();
whatsapp.logout().join();
whatsappBean.setWhatsapp(null);
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(new JSONObject().put("message", "Not connected.").put("status",false).toMap());
......@@ -70,6 +69,24 @@ public class WhatsappRest {
}
}
@GetMapping("status")
public ResponseEntity<?> status() {
Whatsapp whatsapp = whatsappBean.getWhatsapp();
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Not connected.").put("status",false).toMap());
}
try {
if(whatsapp.isConnected()){
return ResponseEntity.status(Response.SC_OK).body(new JSONObject().put("message", "Connected").put("status",true).toMap());
}else{
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(new JSONObject().put("message", "Status 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();
......
......@@ -2,6 +2,7 @@ package sacooliveros.whatsappweb.bean;
import java.util.Optional;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -13,7 +14,7 @@ public class WhatsappBean {
Optional<Whatsapp> w = Whatsapp.webBuilder().lastConnection().registered();
@Bean
@PostConstruct
public Whatsapp getWhatsapp() {
return w.orElse(null);
}
......@@ -24,13 +25,16 @@ public class WhatsappBean {
if (!w.get().isConnected()) {
w.get().connect().join();
}
}else{
w = Optional.empty();
}
}
public void setWhatsapp(Whatsapp whatsapp) {
w = Optional.ofNullable(whatsapp);
if(whatsapp != null) {
w = Optional.of(whatsapp);
}else{
Whatsapp.webBuilder().lastConnection().registered().get().disconnect().join();
w = Optional.empty();
}
}
}
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