-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixingQuickReplies.js
88 lines (76 loc) · 2.73 KB
/
FixingQuickReplies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This script is used to look at quick replies within the PLH app
// As part of the App Build process, we sometime need to convert quick replies to text inputs
// In this instance we need to reformat the quick replies so that there are no duplicate words between them
// This script takes in a JSON file exported from RapidPro, extracts the quickreplies, then creates a new set of quick replies with no duplicate words
// Include fs module used to import a JSON file
const { debug } = require('console');
var fs = require('fs');
// Calling the readFileSync() method to read json file
var json_string = fs.readFileSync(__dirname + '/em-playground.json').toString();
// Convert to javascript object to work with
var object = JSON.parse(json_string);
// Drill down to find the quick replies
for (const flow of object.flows) {
for (const node of flow.nodes) {
for (const action of node.actions) {
// Check if there are quick replies in this action
try {
if(action.quick_replies.length > 0){
// Quick replies found, store in an array to then work with
const arr = action.quick_replies;
// Process quick replies and remove duplicate words
const UniqueQuickReplies = CreateUniqueQuickReplies(arr)
// Print original and new array to console to see the difference
console.log(arr)
console.log(UniqueQuickReplies);
}
}
catch(err) {
continue
}
}
}
}
function CreateUniqueQuickReplies(arr) {
var UniqueQuickReplies = []
var counter = 1
UniqueWords = FindUniqueWords(arr)
for (const replies of arr){
var NewReply = counter.toString();
const SplitReplies = replies.split(" ");
for (const replyword of SplitReplies){
if (CountIf(replyword,UniqueWords) == 1){
NewReply += " "
NewReply += replyword
}
}
UniqueQuickReplies.push(NewReply)
counter++
}
return UniqueQuickReplies
}
function FindUniqueWords(arr) {
var AllWords = [];
var UniqueWords = [];
for (const replies of arr){
const SplitReplies = replies.split(" ")
for (const replyword of SplitReplies){
AllWords.push(replyword)
}
}
for (const word of AllWords){
if(CountIf(word, AllWords) == 1){
UniqueWords.push(word)
}
}
return UniqueWords
}
function CountIf(string, arr) {
var counter = 0
for (const member of arr){
if (string == member){
counter++
}
}
return counter
}