Skip to content

Commit

Permalink
new functions to move qr to html in message text
Browse files Browse the repository at this point in the history
  • Loading branch information
edmoss345 committed Nov 17, 2023
1 parent 78cba58 commit 08255b1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
13 changes: 12 additions & 1 deletion chatbot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Expand Down
59 changes: 58 additions & 1 deletion chatbot/insert/modify_quick_replies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) => `<a href=""weixin://bizmsgmenu?msgmenucontent=${text}&msgmenuid=projectid"">${text}</a>`;
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

Expand Down Expand Up @@ -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
};

0 comments on commit 08255b1

Please sign in to comment.