-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharnold_bot.js
286 lines (228 loc) · 6.8 KB
/
arnold_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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Slack bot built with Botkit.
This bot demonstrates many of the core features of Botkit:
* Connect to Slack using the real time API
* Receive messages based on "spoken" patterns
* Reply to messages
* Use the conversation system to ask questions
* Use the built in storage system to store and retrieve information
for a user.
# RUN THE BOT:
Get a Bot token from Slack:
-> http://my.slack.com/services/new/bot
Run your bot from the command line:
token=<MY TOKEN> node slack_bot.js
# USE THE BOT:
Find your bot inside Slack to send it a direct message.
Say: "Hello"
The bot will reply "Hello!"
Say: "who are you?"
The bot will tell you its name, where it running, and for how long.
Say: "Call me <nickname>"
Tell the bot your nickname. Now you are friends.
Say: "who am I?"
The bot will tell you your nickname, if it knows one for you.
Say: "shutdown"
The bot will ask if you are sure, and then shut itself down.
Make sure to invite your bot into other channels using /invite @<my bot>!
# EXTEND THE BOT:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if (!process.env.token) {
console.log('Error: Specify token in environment');
process.exit(1);
}
var request = require('superagent');
var Botkit = require('botkit');
var os = require('os');
var controller = Botkit.slackbot({
debug: true,
});
var bot = controller.spawn({
token: process.env.token
}).startRTM();
controller.hears(['hello', 'hi', 'hey', 'welcome', 'yo', 'lets go', 'workout', ':muscle:'], 'direct_message,direct_mention,ambient', function(bot, message) {
bot.startConversation(message, sayHello);
});
sayHello = function(response, convo) {
var intro = pickIntro();
convo.say(intro);
workout(response, convo);
convo.next();
}
workout = function(response, convo) {
var movement = pickWorkout();
convo.say('Ok, lets do '+movement);
askHowMany(response, convo);
convo.next();
}
askHowMany = function(response, convo) {
convo.ask('How many workouts have you done today?', [
{
pattern: /^\d{1,2}$/,
callback: function(response, convo) {
if(response.text > 2){
bot.api.reactions.add({
timestamp: response.ts,
channel: response.channel,
name: pickGoodEmoji(),
}, function(err, res) {
if (err) {
bot.botkit.log('Failed to add emoji reaction :(', err);
}
});
convo.say(pickGood());
convo.say("*Don't forget to set an alarm for your next workout!*");
getRandomGif('strong', function(imageUrl){
convo.say(imageUrl);
convo.next();
});
} else {
bot.api.reactions.add({
timestamp: response.ts,
channel: response.channel,
name: pickBadEmoji(),
}, function(err, res) {
if (err) {
bot.botkit.log('Failed to add emoji reaction :(', err);
}
});
convo.say(pickBaby());
convo.say("*Don't forget to set an alarm for your next workout!*");
getRandomGif('baby', function(imageUrl){
convo.say(imageUrl);
convo.next();
});
}
}
},
{
pattern: 'stop',
callback: function(response, convo) {
bot.api.reactions.add({
timestamp: response.ts,
channel: response.channel,
name: pickBadEmoji(),
}, function(err, res) {
if (err) {
bot.botkit.log('Failed to add emoji reaction :(', err);
}
});
convo.say(pickBaby());
getRandomGif('baby', function(imageUrl){
convo.say(imageUrl);
convo.next();
});
}
},
{
default: true,
callback: function(response, convo) {
convo.say('Sorry I need a number, stupid!');
getRandomGif('stupid', function(imageUrl){
convo.say(imageUrl);
askHowMany(response, convo);
convo.next();
});
}
}
]);
}
controller.hears(['shutdown'], 'direct_message,direct_mention', 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("I'll be back!");
convo.next();
setTimeout(function() {
process.exit();
}, 3000);
}
},
{
pattern: bot.utterances.no,
default: true,
callback: function(response, convo) {
convo.say('*Phew!*');
convo.next();
}
}
]);
});
});
function getRandomGif(keyword, callback){
var url = 'http://api.giphy.com/v1/gifs/search?q='+keyword+'&api_key=dc6zaTOxFJmzC';
request.get(url)
.end(function(err, res){
if(err || !res.ok) {
console.log(err)
return
}
image = res.body.data[Math.floor(Math.random()*res.body.data.length)];
callback(image.images.fixed_height.url);
})
}
function pickIntro() {
var items = Array(
'I am here to pump you up!',
'I am the workoutanator!',
'Strength does not come from sitting!'
);
return items[Math.floor(Math.random()*items.length)];
}
function pickBaby() {
var items = Array(
"c'mon is that all you got? silly baby!",
"don't be a baby!",
'thats why tou have baby arms!'
);
return items[Math.floor(Math.random()*items.length)];
}
function pickBadEmoji() {
var items = Array(
'rage',
'rage1',
'rage2',
'rage3',
'rage4'
);
return items[Math.floor(Math.random()*items.length)];
}
function pickGoodEmoji() {
var items = Array(
'muscle::skin-tone-4',
'weight_lifter::skin-tone-4',
'apple'
);
return items[Math.floor(Math.random()*items.length)];
}
function pickGood() {
var items = Array(
'Keep it up!',
'Thats what I am talking about!',
'Do you have a permit for those guns?'
)
return items[Math.floor(Math.random()*items.length)];
}
function pickWorkout() {
var items = Array(
'a *wall sit*',
'some *push ups*',
'a *plank*',
'some *jumping jacks*',
'some *chair dips*',
'some *crunches*',
'some *step ups*',
'some *lunges*'
);
return items[Math.floor(Math.random()*items.length)];
}