forked from esthercrawford/EstherBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (64 loc) · 2.36 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
71
72
73
74
75
76
77
78
'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(/(<img src=\'[^>]*\'\/>)/);
var p = Promise.resolve();
_.each(lines, function(line) {
line = line.trim();
if (!line.startsWith("<")) {
p = p.then(function() {
return bot.say(line);
});
} else {
// p = p.then(function() {
// var start = line.indexOf("'") + 1;
// var end = line.lastIndexOf("'");
// var imageFile = line.substring(start, end);
// return bot.sendImage(imageFile);
// });
}
})
return p.then(() => 'speak');
}
return updateSilent()
.then(getSilent)
.then(processMessage);
}
}
});