-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
script.js
70 lines (56 loc) · 1.93 KB
/
script.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
'use strict';
const _ = require('lodash');
const Script = require('smooch-bot').Script;
const scriptRules = require('./script.json');
module.exports = new Script({
processing: {
//prompt: (bot) => bot.say('Beep boop...'),
receive: () => 'processing'
},
start: {
receive: (bot) => {
return bot.say('So you want to learn about Esther? Just say HELLO to get started.')
.then(() => 'speak');
}
},
speak: {
receive: (bot, message) => {
let upperText = message.text.trim().toUpperCase();
function updateSilent() {
switch (upperText) {
case "CONNECT ME":
return bot.setProp("silent", true);
case "DISCONNECT":
return bot.setProp("silent", false);
default:
return Promise.resolve();
}
}
function getSilent() {
return bot.getProp("silent");
}
function processMessage(isSilent) {
if (isSilent) {
return Promise.resolve("speak");
}
if (!_.has(scriptRules, upperText)) {
return bot.say(`I didn't understand that.`).then(() => 'speak');
}
var response = scriptRules[upperText];
var lines = response.split('\n');
var p = Promise.resolve();
_.each(lines, function(line) {
line = line.trim();
p = p.then(function() {
console.log(line);
return bot.say(line);
});
})
return p.then(() => 'speak');
}
return updateSilent()
.then(getSilent)
.then(processMessage);
}
}
});