Skip to content

Commit

Permalink
adding function for handling China QR
Browse files Browse the repository at this point in the history
  • Loading branch information
edmoss345 committed Mar 19, 2024
1 parent b51f5ff commit dcefb8b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
15 changes: 15 additions & 0 deletions chatbot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const COMMANDS = {
localize,
move_quick_replies,
reformat_quick_replies,
reformat_quick_replies_china,
convert_qr_to_html
};
const args = process.argv.slice(2);
Expand Down Expand Up @@ -124,6 +125,20 @@ function reformat_quick_replies([input_file, select_phrases, outputName, outputD
}
}

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){
special_words = readInputFile(special_words)
}

const [flows, debug, debug_lang] = modifyqr.reformat_quick_replies_china(readInputFile(input_file), readInputFile(select_phrases), Number(count_threshold), Number(length_threshold), 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 convert_qr_to_html([input_file, outputName, outputDir]) {

const [flows, debug, debug_lang] = modifyqr.convert_qr_to_html(readInputFile(input_file))
Expand Down
80 changes: 80 additions & 0 deletions chatbot/insert/modify_quick_replies.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,85 @@ function reformat_quick_replies(flows, select_phrases, count_threshold, length_t
return [flows, debug, debug_lang];
}

function reformat_quick_replies_china(flows, select_phrases, count_threshold, length_threshold, 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 > count_threshold || max_qr_length > length_threshold){

add_quick_replies_to_msg_text(action, quick_replies, curr_loc, select_phrases);

clear_quick_replies(node, routers, action, curr_loc, quick_replies, "no", special_words, debug, debug_lang, qr_limit);

modify_router_node_cases(node, action, curr_loc, quick_replies, routers, debug, debug_lang, routers_edited);
}
}
} else if (qr_count = 1) {
let quick_replies = augment_quick_replies(action, exceptions, curr_loc);

add_quick_replies_to_msg_text_html(action, quick_replies, curr_loc);

clear_quick_replies(node, routers, action, curr_loc, quick_replies, "no");
}
}
}
}
}

return [flows, debug, debug_lang];
}

function convert_qr_to_html(flows) {

const exceptions = []
Expand Down Expand Up @@ -467,5 +546,6 @@ function not_contains_numeric_value(list) {
module.exports = {
move_quick_replies_to_message_text,
reformat_quick_replies,
reformat_quick_replies_china,
convert_qr_to_html
};

0 comments on commit dcefb8b

Please sign in to comment.