initial commit

This commit is contained in:
2026-08-19 11:03:29 +05:30
commit 3914e4f304
23 changed files with 3050 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
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 url = "https://graph.facebook.com/v19.0/" + phoneNumberId + "/messages";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(apiToken);
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());
} catch (Exception e) {
System.err.println("Failed to send message: " + e.getMessage());
}
}
}