92 lines
4.8 KiB
Python
92 lines
4.8 KiB
Python
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.")
|