Skip to content

Commit

Permalink
Merge pull request #70 from IDEMSInternational/QR-code-for-whatsapp-f…
Browse files Browse the repository at this point in the history
…ormat

Adding function for whatsapp qr format
  • Loading branch information
edmoss345 authored May 29, 2024
2 parents c02f197 + a94d1cf commit ce7f173
Show file tree
Hide file tree
Showing 7 changed files with 848 additions and 4 deletions.
15 changes: 15 additions & 0 deletions chatbot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const COMMANDS = {
move_quick_replies,
move_and_mod_quick_replies,
reformat_quick_replies,
reformat_quick_replies_whatsapp,
reformat_quick_replies_china,
convert_qr_to_html
};
Expand Down Expand Up @@ -142,6 +143,20 @@ function reformat_quick_replies([input_file, select_phrases, outputName, outputD
}
}

function reformat_quick_replies_whatsapp([input_file, select_phrases, outputName, outputDir, qr_limit = 10, special_words = false]) {

if(special_words != false){
special_words = readInputFile(special_words)
}

const [flows, debug, debug_lang] = modifyqr.reformat_quick_replies_whatsapp(readInputFile(input_file), readInputFile(select_phrases), Number(qr_limit), special_words)
writeOutputFile(outputDir, outputName + '.json', flows);
writeOutputFile(outputDir, 'debug_qr.txt', debug);
for (lang in debug_lang){
writeOutputFile(outputDir, 'debug_qr_' + lang +'.txt', debug_lang[lang]);
}
}

function reformat_quick_replies_china([input_file, select_phrases, outputName, outputDir, count_threshold = 1, length_threshold = 1, qr_limit = 100, special_words = false]) {

if(special_words != false){
Expand Down
76 changes: 74 additions & 2 deletions chatbot/insert/modify_quick_replies.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs');

// Code for running local tests on function - leave in place
// let filePath = "C:/Users/edmun/Code/idems_translation/chatbot/test/Other_Test_files/NEXT_replace_test.json"
// let filePath = "C:/Users/edmun/Code/idems_translation/chatbot/test/Other_Test_files/WApp_Test_Before.json"
// let obj = JSON.parse(fs.readFileSync(filePath).toString());
// const [a, b, c] = move_and_modidy_qr_to_message_text(obj, "./test/Input/select_phrases.json", "./test/Input/replace_phrases.json", "No", 100,"./test/Input/special_words.json")
// const [a, b, c] = reformat_quick_replies_whatsapp(obj, "./test/Input/select_phrases.json", "./test/Input/special_words.json")

function move_quick_replies_to_message_text(flows, select_phrases, add_selectors, qr_limit, special_words) {

Expand Down Expand Up @@ -214,6 +214,77 @@ function reformat_quick_replies(flows, select_phrases, count_threshold, length_t
return [flows, debug, debug_lang];
}

function reformat_quick_replies_whatsapp(flows, select_phrases, qr_limit, special_words) {

const exceptions = [
'no',
'prefer not to say',
'prefer not to answer',
'prefer not to tell',
'i prefer not to tell',
'does not apply',
'go back to the previous options',
'i am not interested',
'no i do not agree'
];

let debug = '';
let debug_lang = {};
if (!select_phrases.hasOwnProperty("eng")){
select_phrases["eng"] = "Please select the number for the following options:"
}

for (const flow of flows.flows) {

let curr_loc = flow.localization;

debug += `\n\n${flow.name}*************************************\n`;
for (const lang in curr_loc) {

if (debug_lang.hasOwnProperty(lang)){
debug_lang[lang] += `\n\n${flow.name}*************************************\n`;
}else{
debug_lang[lang] = `${flow.name}*************************************\n`;
}

}

const routers = flow.nodes
.filter((node) => node.router && node.router.operand === '@input.text')
.reduce(
(acc, node) => {
acc[node.uuid] = node;
return acc;
},
{}
);

let routers_edited = []

for (const node of flow.nodes) {
for (const action of node.actions) {
if (action.type == 'send_msg') {
qr_count = action.quick_replies.length
if (qr_count > 1) {
if(not_contains_numeric_value(action.quick_replies)){
let quick_replies = augment_quick_replies(action, exceptions, curr_loc);
let max_qr_length = find_max_length(quick_replies)
if(qr_count < 4 && max_qr_length > 20 || qr_count > 3 && qr_count < 11 && max_qr_length > 24 || qr_count > qr_limit){

add_quick_replies_to_msg_text(action, quick_replies, curr_loc, select_phrases);
clear_quick_replies(node, routers, action, curr_loc, quick_replies, "yes", special_words, debug, debug_lang, qr_limit);
modify_router_node_cases(node, action, curr_loc, quick_replies, routers, debug, debug_lang, routers_edited);
}
}
}
}
}
}
}

return [flows, debug, debug_lang];
}

function reformat_quick_replies_china(flows, select_phrases, count_threshold, length_threshold, qr_limit ,special_words) {

const exceptions = [
Expand Down Expand Up @@ -650,6 +721,7 @@ module.exports = {
move_quick_replies_to_message_text,
move_and_modidy_qr_to_message_text,
reformat_quick_replies,
reformat_quick_replies_whatsapp,
reformat_quick_replies_china,
convert_qr_to_html
};
2 changes: 1 addition & 1 deletion chatbot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chatbot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@idems/idems_translation_chatbot",
"version": "1.0.18",
"version": "1.0.19",
"description": "Translation tools for chatbots",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit ce7f173

Please sign in to comment.