-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsmoochy.js
executable file
·129 lines (103 loc) · 4.07 KB
/
smoochy.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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______..___ ___. ______ ______ ______ __ __ .______ ______ .___________.
/ || \/ | / __ \ / __ \ / || | | | | _ \ / __ \ | |
| (----`| \ / | | | | | | | | | | ,----'| |__| | | |_) | | | | | `---| |----`
\ \ | |\/| | | | | | | | | | | | | __ | | _ < | | | | | |
.----) | | | | | | `--' | | `--' | | `----.| | | | | |_) | | `--' | | |
|_______/ |__| |__| \______/ \______/ \______||__| |__| |______/ \______/ |__|
SmoochBot is
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var Botkit = require('./lib/Botkit.js')
var os = require('os');
var validator = require("email-validator");
//Initialization
var controller = Botkit.smoochbot({
appToken:process.env.APPTOKEN,
key:process.env.KEY,
secret:process.env.SECRET,
incoming_webhook:{url:process.env.INCOMING_WEBHOOK},
bot_name:process.env.BOT_NAME,
avatar_url:process.env.AVATAR_URL
});
var bot = controller.spawn()
bot.configureIncomingWebhook();
controller.startTicking();
controller.setupWebserver(process.env.PORT, function(err, server) {
controller.createWebhookEndpoints(server);
});
//A greeting will start the conversation
controller.hears(['hello','hi','sup','yo','hey'],'message_received',function(bot,message) {
bot.api.appUsers.get(message.user).then(function(response) {
bot.reply(message,"Hello.");
if (response && response.appUser && !response.appUser.email) {
bot.startConversation(message, askEmail);
} else {
bot.startConversation(message, askArea);
}
});
})
//Handlers for various steps in the conversation
var askEmail = function(response, convo) {
console.log(convo);
convo.ask("Before we begin, can you please give me your e-mail address, this way someone from my team can get back to you if they're not around right now?", function(response, convo) {
if(validator.validate("[email protected]")) {
bot.api.appUsers.update(response.user, {"email": response.text}).then(function(response) {
convo.say("Awesome, thanks!");
askArea(response,convo);
convo.next();
});
} else {
convo.say("Alright");
askArea(response,convo);
convo.next();
}
});
}
var askArea = function(response, convo) {
convo.ask("Do you have a technical question?", [
{
pattern: bot.utterances.yes,
callback: function(response, convo) {
convo.say('Cool, one of my @engineers will be able to help you if I can\'t');
askPlatform(response,convo);
convo.next();
}
},
{
pattern: bot.utterances.no,
default: true,
callback: function(response, convo) {
convo.say('*Phew!* I still haven\'t had any coffee and my circuits are a little rusty. One of my human friends from our @biz team will lend a hand in a few.');
convo.next();
}
}
]);
}
var askPlatform = function(response, convo) {
convo.ask("What platform are you working with, iOS, Android, Web, or API?", function(response, convo) {
convo.say("Thanks, I've alterted our @engineers - they'll be around in a jiffy.");
convo.next();
});
}
//Uptime stuff
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 named SmoochBot 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;
}