Skip to content

Commit

Permalink
Merge pull request #56 from IDEMSInternational/replace-qr-with-html
Browse files Browse the repository at this point in the history
new functions to move qr to html in message text
  • Loading branch information
edmoss345 authored Dec 15, 2023
2 parents 78cba58 + 554c670 commit b3d1106
Show file tree
Hide file tree
Showing 8 changed files with 1,111 additions and 55 deletions.
5 changes: 5 additions & 0 deletions chatbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ if the count of quick_replies is above the count threshold or the longest quick_
'special_words' expects a path to JSON which has a list of words which will be reinstated as full quick replies as opposed to numbers. The special_words should be organised by language, an example of the file can be found in `test/Input/special_words.json`. This file can be reviewed for info but should not be modified as it is part of the test script
```

Convert Quick Replies to HTML in message text
```
node index convert_qr_to_html <input-rapidpro-flow-file> <output_name> <output-dir>
Overall process visualised in flowchart linked below
https://docs.google.com/drawings/d/1i-64dAkcYqkLWNJmCpl7no6mQqFD3DJ1vI1lnKqd12U/edit?usp=sharing
```
Expand Down
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
60 changes: 59 additions & 1 deletion chatbot/insert/modify_quick_replies.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,48 @@ function reformat_quick_replies(flows, select_phrases, count_threshold, length_t
return [flows, debug, debug_lang];
}

function convert_qr_to_html(flows) {

const exceptions = []
let debug = '';
let debug_lang = {};

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);

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 +238,21 @@ 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) {
const formatQuickReplyLink = (text) => `<a href="weixin://bizmsgmenu?msgmenucontent=${text}&msgmenuid=projectid">${text}</a>`;
action.text = [
action.text,
...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],
...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 +456,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
};
113 changes: 62 additions & 51 deletions chatbot/package-lock.json

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

4 changes: 2 additions & 2 deletions 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.11",
"version": "1.0.13",
"description": "Translation tools for chatbots",
"main": "index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"mocha": "^9.1.3"
},
"dependencies": {
"excel4node": "^1.7.2",
"excel4node": "^1.8.2",
"xlsx": "^0.18.2"
},
"directories": {
Expand Down
Loading

0 comments on commit b3d1106

Please sign in to comment.