From 20805c133817892bbe48e15846874cf60ced343c Mon Sep 17 00:00:00 2001 From: Dev102030-pixel Date: Mon, 17 Aug 2026 12:36:16 +0530 Subject: [PATCH] tamil error fixed --- fix_menu.py | 101 ++++++++++++++++ fix_restart.py | 91 ++++++++++++++ .../service/ChatbotService.java | 112 ++++++++++++------ test_restart.py | 49 ++++++++ 4 files changed, 319 insertions(+), 34 deletions(-) create mode 100644 fix_menu.py create mode 100644 fix_restart.py create mode 100644 test_restart.py diff --git a/fix_menu.py b/fix_menu.py new file mode 100644 index 0000000..6e1b5c3 --- /dev/null +++ b/fix_menu.py @@ -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 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.") diff --git a/fix_restart.py b/fix_restart.py new file mode 100644 index 0000000..360767f --- /dev/null +++ b/fix_restart.py @@ -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.") diff --git a/src/main/java/com/example/whatsappautomation/service/ChatbotService.java b/src/main/java/com/example/whatsappautomation/service/ChatbotService.java index e386dd7..6fc4cb6 100644 --- a/src/main/java/com/example/whatsappautomation/service/ChatbotService.java +++ b/src/main/java/com/example/whatsappautomation/service/ChatbotService.java @@ -66,7 +66,10 @@ public class ChatbotService { // 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")) { - 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); @@ -1052,6 +1055,14 @@ public class ChatbotService { 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; + } if (phoneNumberId == null || recipientPhoneNumber == null) return; String url = "https://graph.facebook.com/v20.0/" + phoneNumberId + "/messages"; try { @@ -1097,41 +1108,74 @@ public class ChatbotService { } } - private void sendPaginatedButtons(UserState state, String bodyText, String[] ids, String[] titles) { - int totalOptions = ids.length; - if (totalOptions <= 3) { - sendInteractiveButtons(bodyText, ids, titles); - return; - } + 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"); - int page = state.getMenuPage(); - int maxItemsPerPage = 2; - int startIndex = page * maxItemsPerPage; - - if (startIndex >= totalOptions) { - startIndex = 0; - state.setMenuPage(0); + 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 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); - - int buttonCount = endIndex - startIndex; - if (hasMore) buttonCount++; - - String[] pageIds = new String[buttonCount]; - String[] pageTitles = new String[buttonCount]; - - for (int i = 0; i < endIndex - startIndex; i++) { - pageIds[i] = ids[startIndex + i]; - pageTitles[i] = titles[startIndex + i]; + } + + 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); } - - if (hasMore) { - pageIds[buttonCount - 1] = "NEXT_PAGE"; - pageTitles[buttonCount - 1] = getMessage("pagination.next", state); - } - - sendInteractiveButtons(bodyText, pageIds, pageTitles); } } diff --git a/test_restart.py b/test_restart.py new file mode 100644 index 0000000..3c7a664 --- /dev/null +++ b/test_restart.py @@ -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)