-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
210 lines (168 loc) · 6.43 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const _ = require('lodash')
const cache = require('@fwd/cache')
const greetings = [ 'Hello', 'How can I help?', 'Yo', "What's up", 'Sup', 'How can I be of assistance?' ]
const confirmations = [ 'Done.', 'Confirmed.', 'Consider it done.', 'Finished.' ]
const thanks = [ "Np", "You're very welcome.", "All good.", "You're welcomed.", "Got chu, fam.", "Don't mention it." ]
const oks = [ "Ok", "Np", "Aight", "Sure" ]
function validEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
function validPhone(phone) {
var re = /^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g;
return re.test(phone);
}
function editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue),
costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
}
function similarity(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2; shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) return 1.0
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
}
function answerQuestion(id, question, answer) {
var conversation = cache(id)
question.value = answer
cache(id, conversation)
}
function unansweredQuestions(id, message) {
var conversation = cache(id)
if (!conversation || !conversation.fields || !conversation.fields.length) return false
if (!conversation.fields) return false
var unanswered = conversation.fields.filter(a => !a.value)
if (!unanswered[0]) return false
return unanswered[0]
}
function thanked(msg) {
msg = msg.toLowerCase()
return msg == 'thanks' || msg == 'thank you' || msg == 'thx' || msg == 'gracias'
}
function agreed(msg) {
msg = msg.toLowerCase()
return msg == 'yes' || msg == 'yea' || msg == 'yeah' || msg == 'mhm' || msg == 'si' || msg == 'yep' || msg == 'please' || msg == 'sure' || msg == 'alright'
}
function cancel(msg) {
msg = msg.toLowerCase()
return msg == 'nvm' || msg == 'never mind' || msg == 'cancel'
}
module.exports = (id, message, capabilities, config) => {
return new Promise(async (resolve, reject) => {
config = config || {}
// check if conversion exists
let conversation = cache(id)
if (!conversation && thanked(message)) {
resolve( _.sample(config.thanks || thanks) )
return
}
if (cancel(message) || conversation && !conversation.confirmed && message.toLowerCase() == 'no') {
resolve( _.sample(config.oks || oks) )
conversation = false
cache(id, false)
return
}
// if not, find a matching feature
if (!conversation) {
conversation = JSON.parse(JSON.stringify(capabilities)).find(a => {
return a.triggers.includes(message.toLowerCase()) || a.triggers.some(b => similarity(b, message.toLowerCase()) > 1)
})
}
// if not, tell user
if (!conversation) {
resolve( _.sample(config.greetings || greetings) )
return
}
if (conversation.confirmation === false) {
conversation.confirmed = true
}
// if yes, ask user if that's what they want to do
if (!conversation.confirmed && !agreed(message)) {
cache(id, conversation)
if (Array.isArray(conversation.confirmation)) conversation.confirmation = _.sample(conversation.confirmation)
resolve(conversation.confirmation || `Are you sure you want to run ${conversation.name}`)
return
}
// if yes, load feature into memory and ask first question
if (!conversation.confirmed && agreed(message)) {
conversation.confirmed = true
cache(id, conversation)
if (unansweredQuestions(id)) {
resolve( unansweredQuestions(id).label )
return
}
}
var question = unansweredQuestions(id)
var _question = capabilities.find(a => {
return a.fields && a.fields.find(b => b.label == question.label && b.name == question.name)
})
if (_question && Array.isArray(_question)) {
_question = _question.find(b => b.label == question.label && b.name == question.name)
}
if (_question && _question.type) {
if (_question.type == 'email' && !validEmail(message)) {
resolve("Answer must be valid email.")
return
}
if (_question.type == 'number' && !parseFloat(message)) {
resolve("Answer must be valid number.")
return
}
if (_question.type == 'phone' && !validPhone(message)) {
resolve("Answer must be valid phone number.")
return
}
}
if (_question && _question.validate) {
var answer = await _question.validate(message)
if (answer !== true) {
var response = Array.isArray(answer) ? _.sample(answer) : answer
resolve(response)
return
}
}
if (_question && _question.format) message = _question.format(message)
if (question) {
answerQuestion(id, question, message)
var question = unansweredQuestions(id)
if (question) return resolve( question.label )
}
if (conversation.confirmed && !question) {
var response = ''
var feature = capabilities.find(a => a.name == conversation.name)
if (feature) {
response = await feature.action(conversation.fields)
if (Array.isArray(response)) response = _.sample(response)
}
resolve( typeof response == 'string' ? response : _.sample(config.confirmations || confirmations) )
conversation = false
cache(id, false)
return
}
})
}