forked from howdyai/botkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio_sms_bot.js
108 lines (92 loc) · 3.08 KB
/
twilio_sms_bot.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
var Botkit = require('./lib/Botkit.js');
var os = require('os');
var controller = Botkit.twiliosmsbot({
account_sid: process.env.TWILIO_ACCOUNT_SID,
auth_token: process.env.TWILIO_AUTH_TOKEN,
twilio_number: process.env.TWILIO_NUMBER,
debug: true
});
var bot = controller.spawn({});
controller.setupWebserver(5000, function(err, server) {
server.get('/', function(req, res) {
res.send(':)');
});
controller.createWebhookEndpoints(server, bot);
})
controller.hears(['hello', 'hi'], 'message_received', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Hello ' + user.name + '!!');
} else {
bot.reply(message, 'Hello.');
}
});
});
controller.hears(['call me (.*)'], 'message_received', function(bot, message) {
var matches = message.text.match(/call me (.*)/i);
var name = matches[1];
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = name;
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
});
});
});
controller.hears(['what is my name', 'who am i'], 'message_received', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Your name is ' + user.name);
} else {
bot.reply(message, 'I don\'t know yet!');
}
});
});
controller.hears(['shutdown'], 'message_received', function(bot, message) {
bot.startConversation(message, function(err, convo) {
convo.ask('Are you sure you want me to shutdown?', [{
pattern: bot.utterances.yes,
callback: function(response, convo) {
convo.say('Bye!');
convo.next();
setTimeout(function() {
process.exit();
}, 3000);
}
},
{
pattern: bot.utterances.no,
default: true,
callback: function(response, convo) {
convo.say('*Phew!*');
convo.next();
}
}
]);
});
});
controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'], 'message_received', function(bot, message) {
var hostname = os.hostname();
var uptime = formatUptime(process.uptime());
bot.reply(message, 'I am a bot! I have been running for ' + uptime + ' on ' + hostname + '.');
});
function formatUptime(uptime) {
var unit = 'second';
if (uptime > 60) {
uptime = uptime / 60;
unit = 'minute';
}
if (uptime > 60) {
uptime = uptime / 60;
unit = 'hour';
}
if (uptime != 1) {
unit = unit + 's';
}
uptime = uptime + ' ' + unit;
return uptime;
}