Skip to content

Commit 81d8df1

Browse files
adding API for reply webhook
1 parent e5cc09c commit 81d8df1

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.twilio.whatsapp;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import lombok.RequiredArgsConstructor;
9+
10+
@RestController
11+
@RequiredArgsConstructor
12+
public class ReplyController {
13+
14+
private final WhatsappMessageDispatcher whatsappMessageDispatcher;
15+
16+
@PostMapping(value = "/api/v1/whatsapp-message-reply")
17+
public ResponseEntity<Void> reply(@RequestParam("ProfileName") String username,
18+
@RequestParam("WaId") String phoneNumber) {
19+
whatsappMessageDispatcher.dispatchReplyMessage(phoneNumber, username);
20+
return ResponseEntity.ok().build();
21+
}
22+
23+
}

saas-modules/twilio-whatsapp/src/main/java/com/baeldung/twilio/whatsapp/ArticlePublishedNotificationDispatcher.java renamed to saas-modules/twilio-whatsapp/src/main/java/com/baeldung/twilio/whatsapp/WhatsappMessageDispatcher.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,29 @@
1212
@Service
1313
@RequiredArgsConstructor
1414
@EnableConfigurationProperties(TwilioConfigurationProperties.class)
15-
public class ArticlePublishedNotificationDispatcher {
15+
public class WhatsappMessageDispatcher {
1616

1717
private final TwilioConfigurationProperties twilioConfigurationProperties;
1818

19-
public void dispatch(String phoneNumber, String articleUrl) {
19+
public void dispatchNewArticleNotification(String phoneNumber, String articleUrl) {
2020
String messagingSid = twilioConfigurationProperties.getMessagingSid();
2121
String contentSid = twilioConfigurationProperties.getNewArticleNotification().getContentSid();
2222
PhoneNumber toPhoneNumber = new PhoneNumber(String.format("whatsapp:%s", phoneNumber));
2323

2424
JSONObject contentVariables = new JSONObject();
2525
contentVariables.put("ArticleURL", articleUrl);
26-
26+
2727
Message.creator(toPhoneNumber, messagingSid, "")
2828
.setContentSid(contentSid)
29-
.setContentVariables(contentVariables.toString())
30-
.create();
29+
.setContentVariables(contentVariables.toString()).create();
30+
}
31+
32+
public void dispatchReplyMessage(String phoneNumber, String username) {
33+
String messagingSid = twilioConfigurationProperties.getMessagingSid();
34+
PhoneNumber toPhoneNumber = new PhoneNumber(String.format("whatsapp:%s", phoneNumber));
35+
36+
String message = String.format("Hey %s, our team will get back to you shortly.", username);
37+
Message.creator(toPhoneNumber, messagingSid, message);
3138
}
3239

3340
}

0 commit comments

Comments
 (0)