From dcefb8b2687b41f166cc13515342b36da12788c3 Mon Sep 17 00:00:00 2001 From: Ed Moss Date: Tue, 19 Mar 2024 00:30:48 -0300 Subject: [PATCH] adding function for handling China QR --- chatbot/index.js | 15 +++++ chatbot/insert/modify_quick_replies.js | 80 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/chatbot/index.js b/chatbot/index.js index 9fb75b9..5eb7246 100644 --- a/chatbot/index.js +++ b/chatbot/index.js @@ -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); @@ -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)) diff --git a/chatbot/insert/modify_quick_replies.js b/chatbot/insert/modify_quick_replies.js index eec2ebc..fe602f9 100644 --- a/chatbot/insert/modify_quick_replies.js +++ b/chatbot/insert/modify_quick_replies.js @@ -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 = [] @@ -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 };