119 lines
4.5 KiB
Java
119 lines
4.5 KiB
Java
package com.rit.whatsapp.service;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.HttpEntity;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class WhatsAppMessageService {
|
|
|
|
@Value("${whatsapp.api-token}")
|
|
private String apiToken;
|
|
|
|
@Value("${whatsapp.phone-number-id}")
|
|
private String phoneNumberId;
|
|
|
|
private final RestTemplate restTemplate;
|
|
|
|
public WhatsAppMessageService() {
|
|
this.restTemplate = new RestTemplate();
|
|
}
|
|
|
|
public void sendMessage(String toPhoneNumber, String messageText) {
|
|
Map<String, Object> payload = new HashMap<>();
|
|
payload.put("messaging_product", "whatsapp");
|
|
payload.put("to", toPhoneNumber);
|
|
payload.put("type", "text");
|
|
|
|
Map<String, String> textPayload = new HashMap<>();
|
|
textPayload.put("body", messageText);
|
|
payload.put("text", textPayload);
|
|
|
|
executePost(payload);
|
|
}
|
|
|
|
public void sendInteractiveMessage(String toPhoneNumber, Map<String, Object> interactiveObject) {
|
|
Map<String, Object> payload = new HashMap<>();
|
|
payload.put("messaging_product", "whatsapp");
|
|
payload.put("to", toPhoneNumber);
|
|
payload.put("type", "interactive");
|
|
payload.put("interactive", interactiveObject);
|
|
|
|
executePost(payload);
|
|
}
|
|
|
|
private void executePost(Map<String, Object> payload) {
|
|
String cleanPhoneId = phoneNumberId != null ? phoneNumberId.trim() : "";
|
|
String cleanToken = apiToken != null ? apiToken.trim() : "";
|
|
|
|
String url = "https://graph.facebook.com/v19.0/" + cleanPhoneId + "/messages";
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
headers.setBearerAuth(cleanToken);
|
|
|
|
HttpEntity<Map<String, Object>> request = new HttpEntity<>(payload, headers);
|
|
|
|
try {
|
|
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
|
|
System.out.println("Message successfully delivered to WhatsApp.");
|
|
} catch (Exception e) {
|
|
System.err.println("Failed to send message: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
public String uploadMedia(byte[] fileData, String fileName, String mimeType) {
|
|
String cleanPhoneId = phoneNumberId != null ? phoneNumberId.trim() : "";
|
|
String cleanToken = apiToken != null ? apiToken.trim() : "";
|
|
String url = "https://graph.facebook.com/v19.0/" + cleanPhoneId + "/media";
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
headers.setBearerAuth(cleanToken);
|
|
|
|
org.springframework.util.MultiValueMap<String, Object> body = new org.springframework.util.LinkedMultiValueMap<>();
|
|
body.add("messaging_product", "whatsapp");
|
|
body.add("file", new org.springframework.core.io.ByteArrayResource(fileData) {
|
|
@Override
|
|
public String getFilename() {
|
|
return fileName;
|
|
}
|
|
});
|
|
body.add("type", mimeType);
|
|
|
|
HttpEntity<org.springframework.util.MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
|
|
|
try {
|
|
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
|
|
com.fasterxml.jackson.databind.JsonNode root = new com.fasterxml.jackson.databind.ObjectMapper().readTree(response.getBody());
|
|
return root.get("id").asText();
|
|
} catch (Exception e) {
|
|
System.err.println("Media upload failed: " + e.getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void sendDocumentMessage(String toPhoneNumber, String mediaId, String caption, String filename) {
|
|
Map<String, Object> payload = new HashMap<>();
|
|
payload.put("messaging_product", "whatsapp");
|
|
payload.put("to", toPhoneNumber);
|
|
payload.put("type", "document");
|
|
|
|
Map<String, Object> doc = new HashMap<>();
|
|
doc.put("id", mediaId);
|
|
if (caption != null && !caption.isEmpty()) doc.put("caption", caption);
|
|
if (filename != null && !filename.isEmpty()) doc.put("filename", filename);
|
|
|
|
payload.put("document", doc);
|
|
|
|
executePost(payload);
|
|
}
|
|
}
|