tamil error fixed
This commit is contained in:
101
fix_menu.py
Normal file
101
fix_menu.py
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
with open("src/main/java/com/example/whatsappautomation/service/ChatbotService.java", "r") as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
old_paginated = """ private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) {
|
||||||
|
int totalOptions = ids.length;
|
||||||
|
|
||||||
|
// Loop and send chunks of up to 3 buttons
|
||||||
|
for (int i = 0; i < totalOptions; i += 3) {
|
||||||
|
int end = Math.min(i + 3, totalOptions);
|
||||||
|
int count = end - i;
|
||||||
|
String[] chunkIds = new String[count];
|
||||||
|
String[] chunkTitles = new String[count];
|
||||||
|
|
||||||
|
for (int j = 0; j < count; j++) {
|
||||||
|
chunkIds[j] = ids[i + j];
|
||||||
|
chunkTitles[j] = titles[i + j];
|
||||||
|
}
|
||||||
|
|
||||||
|
String chunkBodyText = (i == 0) ? bodyText : ( "en".equals(state.getLanguagePreference()) ? "More options:" : "மேலும் விருப்பங்கள்:" );
|
||||||
|
|
||||||
|
sendInteractiveButtons(chunkBodyText, chunkIds, chunkTitles);
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
new_methods = """ private void sendInteractiveList(String bodyText, String buttonText, String sectionTitle, String[] ids, String[] titles) {
|
||||||
|
String phoneNumberId = currentPhoneId.get();
|
||||||
|
String recipientPhoneNumber = currentFrom.get();
|
||||||
|
if (phoneNumberId == null || recipientPhoneNumber == null) return;
|
||||||
|
String url = "https://graph.facebook.com/v20.0/" + phoneNumberId + "/messages";
|
||||||
|
|
||||||
|
try {
|
||||||
|
ObjectNode requestBody = objectMapper.createObjectNode();
|
||||||
|
requestBody.put("messaging_product", "whatsapp");
|
||||||
|
requestBody.put("recipient_type", "individual");
|
||||||
|
requestBody.put("to", recipientPhoneNumber);
|
||||||
|
requestBody.put("type", "interactive");
|
||||||
|
|
||||||
|
ObjectNode interactiveNode = objectMapper.createObjectNode();
|
||||||
|
interactiveNode.put("type", "list");
|
||||||
|
|
||||||
|
ObjectNode bodyNode = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
UserState uState = userStateRepository.findById(recipientPhoneNumber).orElse(null);
|
||||||
|
if (uState != null && !"MAIN_MENU".equals(uState.getState()) && !"LANGUAGE_SELECTION".equals(uState.getState())) {
|
||||||
|
String restartText = "en".equals(uState.getLanguagePreference())
|
||||||
|
? "\\n\\n(Press 0 to restart the conversation)"
|
||||||
|
: "\\n\\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
bodyText += restartText;
|
||||||
|
}
|
||||||
|
bodyNode.put("text", bodyText);
|
||||||
|
interactiveNode.set("body", bodyNode);
|
||||||
|
|
||||||
|
ObjectNode actionNode = objectMapper.createObjectNode();
|
||||||
|
actionNode.put("button", buttonText);
|
||||||
|
|
||||||
|
com.fasterxml.jackson.databind.node.ArrayNode sectionsArray = actionNode.putArray("sections");
|
||||||
|
ObjectNode section = objectMapper.createObjectNode();
|
||||||
|
section.put("title", sectionTitle);
|
||||||
|
com.fasterxml.jackson.databind.node.ArrayNode rowsArray = section.putArray("rows");
|
||||||
|
|
||||||
|
int limit = Math.min(ids.length, 10);
|
||||||
|
for (int i = 0; i < limit; i++) {
|
||||||
|
ObjectNode row = objectMapper.createObjectNode();
|
||||||
|
row.put("id", ids[i]);
|
||||||
|
row.put("title", titles[i]);
|
||||||
|
rowsArray.add(row);
|
||||||
|
}
|
||||||
|
sectionsArray.add(section);
|
||||||
|
|
||||||
|
interactiveNode.set("action", actionNode);
|
||||||
|
requestBody.set("interactive", interactiveNode);
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
|
||||||
|
headers.setBearerAuth(accessToken);
|
||||||
|
|
||||||
|
org.springframework.http.HttpEntity<String> entity = new org.springframework.http.HttpEntity<>(requestBody.toString(), headers);
|
||||||
|
restTemplate.postForEntity(url, entity, String.class);
|
||||||
|
} catch (org.springframework.web.client.HttpStatusCodeException e) {
|
||||||
|
logger.error("Error sending interactive list. Status: {}, Response: {}", e.getStatusCode(), e.getResponseBodyAsString(), e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error sending interactive list", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) {
|
||||||
|
if (ids.length <= 3) {
|
||||||
|
sendInteractiveButtons(bodyText, ids, titles);
|
||||||
|
} else {
|
||||||
|
String buttonText = "en".equals(state.getLanguagePreference()) ? "Select Option" : "விருப்பத்தை தேர்ந்தெடுக்கவும்";
|
||||||
|
String sectionTitle = "en".equals(state.getLanguagePreference()) ? "Options" : "விருப்பங்கள்";
|
||||||
|
sendInteractiveList(bodyText, buttonText, sectionTitle, ids, titles);
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
content = content.replace(old_paginated, new_methods)
|
||||||
|
|
||||||
|
with open("src/main/java/com/example/whatsappautomation/service/ChatbotService.java", "w") as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
print("Replacement complete.")
|
||||||
91
fix_restart.py
Normal file
91
fix_restart.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
with open("src/main/java/com/example/whatsappautomation/service/ChatbotService.java", "r") as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# 1. Modify sendInteractiveButtons to fetch state and append restart text
|
||||||
|
old_method_start = """ private void sendInteractiveButtons(String bodyText, String[] ids, String[] titles) {
|
||||||
|
String phoneNumberId = currentPhoneId.get();
|
||||||
|
String recipientPhoneNumber = currentFrom.get();"""
|
||||||
|
|
||||||
|
new_method_start = """ private void sendInteractiveButtons(String bodyText, String[] ids, String[] titles) {
|
||||||
|
String phoneNumberId = currentPhoneId.get();
|
||||||
|
String recipientPhoneNumber = currentFrom.get();
|
||||||
|
|
||||||
|
UserState uState = userStateRepository.findById(recipientPhoneNumber).orElse(null);
|
||||||
|
if (uState != null && !"MAIN_MENU".equals(uState.getState()) && !"LANGUAGE_SELECTION".equals(uState.getState())) {
|
||||||
|
String restartText = "en".equals(uState.getLanguagePreference())
|
||||||
|
? "\\n\\n(Press 0 to restart the conversation)"
|
||||||
|
: "\\n\\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
bodyText += restartText;
|
||||||
|
}"""
|
||||||
|
|
||||||
|
content = content.replace(old_method_start, new_method_start)
|
||||||
|
|
||||||
|
# 2. Modify sendPaginatedButtons to not append restart text itself
|
||||||
|
old_paginated = """ private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) {
|
||||||
|
int totalOptions = ids.length;
|
||||||
|
String restartText = "en".equals(state.getLanguagePreference())
|
||||||
|
? "\\n\\n(Send 0 to go back to the main menu / Restart the conversation)"
|
||||||
|
: "\\n\\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
|
||||||
|
// Loop and send chunks of up to 3 buttons
|
||||||
|
for (int i = 0; i < totalOptions; i += 3) {
|
||||||
|
int end = Math.min(i + 3, totalOptions);
|
||||||
|
int count = end - i;
|
||||||
|
String[] chunkIds = new String[count];
|
||||||
|
String[] chunkTitles = new String[count];
|
||||||
|
|
||||||
|
for (int j = 0; j < count; j++) {
|
||||||
|
chunkIds[j] = ids[i + j];
|
||||||
|
chunkTitles[j] = titles[i + j];
|
||||||
|
}
|
||||||
|
|
||||||
|
String chunkBodyText = (i == 0) ? bodyText : ( "en".equals(state.getLanguagePreference()) ? "More options:" : "மேலும் விருப்பங்கள்:" );
|
||||||
|
if (end == totalOptions) {
|
||||||
|
chunkBodyText += restartText;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendInteractiveButtons(chunkBodyText, chunkIds, chunkTitles);
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
new_paginated = """ private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) {
|
||||||
|
int totalOptions = ids.length;
|
||||||
|
|
||||||
|
// Loop and send chunks of up to 3 buttons
|
||||||
|
for (int i = 0; i < totalOptions; i += 3) {
|
||||||
|
int end = Math.min(i + 3, totalOptions);
|
||||||
|
int count = end - i;
|
||||||
|
String[] chunkIds = new String[count];
|
||||||
|
String[] chunkTitles = new String[count];
|
||||||
|
|
||||||
|
for (int j = 0; j < count; j++) {
|
||||||
|
chunkIds[j] = ids[i + j];
|
||||||
|
chunkTitles[j] = titles[i + j];
|
||||||
|
}
|
||||||
|
|
||||||
|
String chunkBodyText = (i == 0) ? bodyText : ( "en".equals(state.getLanguagePreference()) ? "More options:" : "மேலும் விருப்பங்கள்:" );
|
||||||
|
|
||||||
|
sendInteractiveButtons(chunkBodyText, chunkIds, chunkTitles);
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
content = content.replace(old_paginated, new_paginated)
|
||||||
|
|
||||||
|
# 3. Change processMessage to say "Press 0 to restart the conversation" instead of "(Send 0 to go back to the main menu / Restart the conversation)" for text responses
|
||||||
|
old_process_text = """ if (response != null && !response.isEmpty() && !response.startsWith("INTERACTIVE:") && !userState.getState().equals("MAIN_MENU") && !userState.getState().equals("LANGUAGE_SELECTION")) {
|
||||||
|
response += "\\n\\n(Send 0 to go back to the main menu / Restart the conversation)";
|
||||||
|
}"""
|
||||||
|
|
||||||
|
new_process_text = """ if (response != null && !response.isEmpty() && !response.startsWith("INTERACTIVE:") && !userState.getState().equals("MAIN_MENU") && !userState.getState().equals("LANGUAGE_SELECTION")) {
|
||||||
|
String rt = "en".equals(userState.getLanguagePreference())
|
||||||
|
? "\\n\\n(Press 0 to restart the conversation)"
|
||||||
|
: "\\n\\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
response += rt;
|
||||||
|
}"""
|
||||||
|
|
||||||
|
content = content.replace(old_process_text, new_process_text)
|
||||||
|
|
||||||
|
with open("src/main/java/com/example/whatsappautomation/service/ChatbotService.java", "w") as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
print("Replacement complete.")
|
||||||
@@ -66,7 +66,10 @@ public class ChatbotService {
|
|||||||
|
|
||||||
// If not in MAIN_MENU or LANGUAGE_SELECTION, append restart hint
|
// If not in MAIN_MENU or LANGUAGE_SELECTION, append restart hint
|
||||||
if (response != null && !response.isEmpty() && !response.startsWith("INTERACTIVE:") && !userState.getState().equals("MAIN_MENU") && !userState.getState().equals("LANGUAGE_SELECTION")) {
|
if (response != null && !response.isEmpty() && !response.startsWith("INTERACTIVE:") && !userState.getState().equals("MAIN_MENU") && !userState.getState().equals("LANGUAGE_SELECTION")) {
|
||||||
response += "\n\n(Send 0 to go back to the main menu / Restart the conversation)";
|
String rt = "en".equals(userState.getLanguagePreference())
|
||||||
|
? "\n\n(Press 0 to restart the conversation)"
|
||||||
|
: "\n\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
response += rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
userStateRepository.save(userState);
|
userStateRepository.save(userState);
|
||||||
@@ -1052,6 +1055,14 @@ public class ChatbotService {
|
|||||||
private void sendInteractiveButtons(String bodyText, String[] ids, String[] titles) {
|
private void sendInteractiveButtons(String bodyText, String[] ids, String[] titles) {
|
||||||
String phoneNumberId = currentPhoneId.get();
|
String phoneNumberId = currentPhoneId.get();
|
||||||
String recipientPhoneNumber = currentFrom.get();
|
String recipientPhoneNumber = currentFrom.get();
|
||||||
|
|
||||||
|
UserState uState = userStateRepository.findById(recipientPhoneNumber).orElse(null);
|
||||||
|
if (uState != null && !"MAIN_MENU".equals(uState.getState()) && !"LANGUAGE_SELECTION".equals(uState.getState())) {
|
||||||
|
String restartText = "en".equals(uState.getLanguagePreference())
|
||||||
|
? "\n\n(Press 0 to restart the conversation)"
|
||||||
|
: "\n\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
bodyText += restartText;
|
||||||
|
}
|
||||||
if (phoneNumberId == null || recipientPhoneNumber == null) return;
|
if (phoneNumberId == null || recipientPhoneNumber == null) return;
|
||||||
String url = "https://graph.facebook.com/v20.0/" + phoneNumberId + "/messages";
|
String url = "https://graph.facebook.com/v20.0/" + phoneNumberId + "/messages";
|
||||||
try {
|
try {
|
||||||
@@ -1097,41 +1108,74 @@ public class ChatbotService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) {
|
private void sendInteractiveList(String bodyText, String buttonText, String sectionTitle, String[] ids, String[] titles) {
|
||||||
int totalOptions = ids.length;
|
String phoneNumberId = currentPhoneId.get();
|
||||||
if (totalOptions <= 3) {
|
String recipientPhoneNumber = currentFrom.get();
|
||||||
sendInteractiveButtons(bodyText, ids, titles);
|
if (phoneNumberId == null || recipientPhoneNumber == null) return;
|
||||||
return;
|
String url = "https://graph.facebook.com/v20.0/" + phoneNumberId + "/messages";
|
||||||
}
|
|
||||||
|
try {
|
||||||
|
ObjectNode requestBody = objectMapper.createObjectNode();
|
||||||
|
requestBody.put("messaging_product", "whatsapp");
|
||||||
|
requestBody.put("recipient_type", "individual");
|
||||||
|
requestBody.put("to", recipientPhoneNumber);
|
||||||
|
requestBody.put("type", "interactive");
|
||||||
|
|
||||||
int page = state.getMenuPage();
|
ObjectNode interactiveNode = objectMapper.createObjectNode();
|
||||||
int maxItemsPerPage = 2;
|
interactiveNode.put("type", "list");
|
||||||
int startIndex = page * maxItemsPerPage;
|
|
||||||
|
ObjectNode bodyNode = objectMapper.createObjectNode();
|
||||||
if (startIndex >= totalOptions) {
|
|
||||||
startIndex = 0;
|
UserState uState = userStateRepository.findById(recipientPhoneNumber).orElse(null);
|
||||||
state.setMenuPage(0);
|
if (uState != null && !"MAIN_MENU".equals(uState.getState()) && !"LANGUAGE_SELECTION".equals(uState.getState())) {
|
||||||
|
String restartText = "en".equals(uState.getLanguagePreference())
|
||||||
|
? "\n\n(Press 0 to restart the conversation)"
|
||||||
|
: "\n\n(உரையாடலை மீண்டும் தொடங்க 0 ஐ அழுத்தவும்)";
|
||||||
|
bodyText += restartText;
|
||||||
|
}
|
||||||
|
bodyNode.put("text", bodyText);
|
||||||
|
interactiveNode.set("body", bodyNode);
|
||||||
|
|
||||||
|
ObjectNode actionNode = objectMapper.createObjectNode();
|
||||||
|
actionNode.put("button", buttonText);
|
||||||
|
|
||||||
|
com.fasterxml.jackson.databind.node.ArrayNode sectionsArray = actionNode.putArray("sections");
|
||||||
|
ObjectNode section = objectMapper.createObjectNode();
|
||||||
|
section.put("title", sectionTitle);
|
||||||
|
com.fasterxml.jackson.databind.node.ArrayNode rowsArray = section.putArray("rows");
|
||||||
|
|
||||||
|
int limit = Math.min(ids.length, 10);
|
||||||
|
for (int i = 0; i < limit; i++) {
|
||||||
|
ObjectNode row = objectMapper.createObjectNode();
|
||||||
|
row.put("id", ids[i]);
|
||||||
|
row.put("title", titles[i]);
|
||||||
|
rowsArray.add(row);
|
||||||
|
}
|
||||||
|
sectionsArray.add(section);
|
||||||
|
|
||||||
|
interactiveNode.set("action", actionNode);
|
||||||
|
requestBody.set("interactive", interactiveNode);
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
|
||||||
|
headers.setBearerAuth(accessToken);
|
||||||
|
|
||||||
|
org.springframework.http.HttpEntity<String> entity = new org.springframework.http.HttpEntity<>(requestBody.toString(), headers);
|
||||||
|
restTemplate.postForEntity(url, entity, String.class);
|
||||||
|
} catch (org.springframework.web.client.HttpStatusCodeException e) {
|
||||||
|
logger.error("Error sending interactive list. Status: {}, Response: {}", e.getStatusCode(), e.getResponseBodyAsString(), e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error sending interactive list", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
int endIndex = Math.min(startIndex + maxItemsPerPage, totalOptions);
|
|
||||||
boolean hasMore = (endIndex < totalOptions) || (totalOptions > 3);
|
private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) {
|
||||||
|
if (ids.length <= 3) {
|
||||||
int buttonCount = endIndex - startIndex;
|
sendInteractiveButtons(bodyText, ids, titles);
|
||||||
if (hasMore) buttonCount++;
|
} else {
|
||||||
|
String buttonText = "en".equals(state.getLanguagePreference()) ? "Select Option" : "தேர்ந்தெடு";
|
||||||
String[] pageIds = new String[buttonCount];
|
String sectionTitle = "en".equals(state.getLanguagePreference()) ? "Options" : "விருப்பங்கள்";
|
||||||
String[] pageTitles = new String[buttonCount];
|
sendInteractiveList(bodyText, buttonText, sectionTitle, ids, titles);
|
||||||
|
|
||||||
for (int i = 0; i < endIndex - startIndex; i++) {
|
|
||||||
pageIds[i] = ids[startIndex + i];
|
|
||||||
pageTitles[i] = titles[startIndex + i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasMore) {
|
|
||||||
pageIds[buttonCount - 1] = "NEXT_PAGE";
|
|
||||||
pageTitles[buttonCount - 1] = getMessage("pagination.next", state);
|
|
||||||
}
|
|
||||||
|
|
||||||
sendInteractiveButtons(bodyText, pageIds, pageTitles);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
49
test_restart.py
Normal file
49
test_restart.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"object": "whatsapp_business_account",
|
||||||
|
"entry": [
|
||||||
|
{
|
||||||
|
"id": "1068191009070265",
|
||||||
|
"changes": [
|
||||||
|
{
|
||||||
|
"value": {
|
||||||
|
"messaging_product": "whatsapp",
|
||||||
|
"metadata": {
|
||||||
|
"display_phone_number": "15551914329",
|
||||||
|
"phone_number_id": "1236658909534007"
|
||||||
|
},
|
||||||
|
"contacts": [
|
||||||
|
{
|
||||||
|
"profile": {
|
||||||
|
"name": "Dev"
|
||||||
|
},
|
||||||
|
"wa_id": "919994834737",
|
||||||
|
"user_id": "IN.1458184416042595"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"from": "919994834737",
|
||||||
|
"from_user_id": "IN.1458184416042595",
|
||||||
|
"id": "wamid.HBgMOTE2Mzc5NzgzOTExFQIAEhgWM0VCMDU3MDk2N0Q2ODk0MzE2N0U4NwA=",
|
||||||
|
"timestamp": "1786945902",
|
||||||
|
"interactive": {
|
||||||
|
"type": "button_reply",
|
||||||
|
"button_reply": {
|
||||||
|
"id": "MENU_SERVICES",
|
||||||
|
"title": "My Services"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": "interactive"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"field": "messages"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
requests.post('http://localhost:8085/webhook', json=payload)
|
||||||
Reference in New Issue
Block a user