From 08255b1c29365d10fe5187baf2e964f4fff54f2e Mon Sep 17 00:00:00 2001 From: Ed Moss Date: Fri, 17 Nov 2023 17:24:49 -0300 Subject: [PATCH] new functions to move qr to html in message text --- chatbot/index.js | 13 +++++- chatbot/insert/modify_quick_replies.js | 59 +++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/chatbot/index.js b/chatbot/index.js index ef360b5..9fb75b9 100644 --- a/chatbot/index.js +++ b/chatbot/index.js @@ -16,7 +16,8 @@ const COMMANDS = { extract_simple, localize, move_quick_replies, - reformat_quick_replies + reformat_quick_replies, + convert_qr_to_html }; const args = process.argv.slice(2); const command = args.shift(); @@ -123,6 +124,16 @@ function reformat_quick_replies([input_file, select_phrases, outputName, outputD } } +function convert_qr_to_html([input_file, outputName, outputDir]) { + + const [flows, debug, debug_lang] = modifyqr.convert_qr_to_html(readInputFile(input_file)) + 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 readInputFile(filePath) { return JSON.parse(fs.readFileSync(filePath).toString()); } diff --git a/chatbot/insert/modify_quick_replies.js b/chatbot/insert/modify_quick_replies.js index a779681..8bf95ca 100644 --- a/chatbot/insert/modify_quick_replies.js +++ b/chatbot/insert/modify_quick_replies.js @@ -140,6 +140,45 @@ function reformat_quick_replies(flows, select_phrases, count_threshold, length_t return [flows, debug, debug_lang]; } +function convert_qr_to_html(flows) { + + + 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 = [] + + 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 > 0) { + let quick_replies = augment_quick_replies(action, exceptions, curr_loc); + + add_quick_replies_to_msg_text_html(action, quick_replies, curr_loc, select_phrases); + + clear_quick_replies(node, routers, action, curr_loc, quick_replies, "no"); + } + } + } + } + } + + return [flows, debug, debug_lang]; +} + function find_max_length(inputArray) { let maxLength = 0; @@ -196,6 +235,23 @@ function add_quick_replies_to_msg_text(action, quick_replies, curr_loc, select_p } } +function add_quick_replies_to_msg_text_html(action, quick_replies, curr_loc, select_phrases) { + const formatQuickReplyLink = (text) => `${text}`; + action.text = [ + action.text, + '\n' + select_phrases["eng"], + ...quick_replies.map((qr) => formatQuickReplyLink(qr.text)) + ].join('\n'); + + for (const [lang, translations] of Object.entries(curr_loc)) { + translations[action.uuid].text[0] = [ + translations[action.uuid].text[0], + '\n' + select_phrases[lang], + ...quick_replies.map((qr) => formatQuickReplyLink(qr.translations[lang])) + ].join('\n'); + } +} + function clear_quick_replies(node, routers, action, curr_loc, quick_replies, add_selectors, special_words, debug, debug_lang, qr_limit = 100) { // id of corresponding wait for response node @@ -399,5 +455,6 @@ function not_contains_numeric_value(list) { module.exports = { move_quick_replies_to_message_text, - reformat_quick_replies + reformat_quick_replies, + convert_qr_to_html };