added various fields inside the dropdown
This commit is contained in:
@@ -50,19 +50,69 @@ public class WhatsAppMessageService {
|
||||
}
|
||||
|
||||
private void executePost(Map<String, Object> payload) {
|
||||
String url = "https://graph.facebook.com/v19.0/" + phoneNumberId + "/messages";
|
||||
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(apiToken);
|
||||
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 sent successfully. Response: " + response.getBody());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user