102 lines
5.0 KiB
Python
102 lines
5.0 KiB
Python
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.")
|