-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
241 lines (195 loc) · 8.64 KB
/
index.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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Slack Button application that provides a custom
Slash command.
This bot demonstrates many of the core features of Botkit:
*
* Authenticate users with Slack using OAuth
* Receive messages using the slash_command event
* Reply to Slash command both publicly and privately
# RUN THE BOT:
Create a Slack app. Make sure to configure at least one Slash command!
-> https://api.slack.com/applications/new
Run your bot from the command line:
clientId=<my client id> clientSecret=<my client secret> PORT=3000 node bot.js
Note: you can test your oauth authentication locally, but to use Slash commands
in Slack, the app must be hosted at a publicly reachable IP or host.
# EXTEND THE BOT:
Botkit is has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
-> https://github.com/howdyai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Uses the slack button feature to offer a real time bot to multiple teams */
var Botkit = require('botkit');
/* Airtable Setup */
var Airtable = require('airtable');
var base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_BASE);
/* Grab authentication info from env vars */
if (!process.env.CLIENT_ID || !process.env.CLIENT_SECRET || !process.env.PORT || !process.env.VERIFICATION_TOKEN) {
console.log('Error: Specify CLIENT_ID, CLIENT_SECRET, VERIFICATION_TOKEN and PORT in environment');
process.exit(1);
}
/*
This is required for authorizing the app with slack.
Losing the database or json file will require an oauth
login to occur again from a slack admin before bot will
work
*/
var config = {}
if (process.env.MONGO_URI) {
var BotkitStorage = require('botkit-storage-mongo');
config = {
storage: BotkitStorage({mongoUri: process.env.MONGO_URI}),
};
} else {
config = {
json_file_store: './db_slackbutton_slash_command/',
};
}
var controller = Botkit.slackbot(config).configureSlackApp(
{
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
scopes: ['commands', 'bot'],
}
);
controller.setupWebserver(process.env.PORT, function (err, webserver) {
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver, function (err, req, res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Success!');
}
});
});
//
// Slack slash commands, edit from here down
//
controller.on('slash_command', function (slashCommand, message) {
if (message.token !== process.env.VERIFICATION_TOKEN) {
console.log('Bad token', message.token);
return; //just ignore it.
}
switch (message.command) {
case "/echo":
// if no text was supplied, treat it as a help command
if (message.text === "" || message.text === "help") {
// TODO: reply help with all available OC commands
slashCommand.replyPrivate(message,
"I echo back what you tell me. " +
"Try typing `/echo hello` to see.");
return;
}
// If we made it here, just echo what the user typed back at them
slashCommand.replyPrivate(message, "1", function() {
slashCommand.replyPrivate(message, "2").then(slashCommand.replyPrivate(message, "3"));
});
break;
/* Meetup Events */
/* command /oc events */
case "/oc":
if (message.text == 'events'){
base( "Events" ).select( {
view: "Main View"
} ).firstPage( ( err, records ) => {
if ( err ) {
return console.error( err );
}
const events = records.map( record => `${ record.get( "Name" ) } ${record.get("Notes")}` );
slashCommand.replyPrivate( message, '*OC Events:*\n' + events.join("\n\n"));
} );
}
break;
/* command: /mentees <language> */
// TODO: Create standard function to build queries
case "/mentees":
// prevent too ambiguous of a search
if (message.text.length < 3){
slashCommand.replyPrivate(message, '*Length of search param must be 3 or more characters.*');
return;
}
else if (message.text == 'unassigned'){
base( "Mentees" ).select( {
filterByFormula: `NOT({Assigned?} = "true")`
, view: "Main View"
} ).firstPage( ( err, records ) => {
if ( err ) {
return console.error( err );
}
const mentees = records.map( record => `@${ record.get( "Slack User" ) }` );
slashCommand.replyPrivate( message, '*Mentees without mentors: *\n' + mentees.join("\n"));
} );
}
else {
base( "Mentees" ).select( {
filterByFormula: `SEARCH(LOWER("${message.text}"), LOWER({Language})) >= 1`
, view: "Main View"
} ).firstPage( ( err, records ) => {
if ( err ) {
return console.error( err );
}
const mentees = records.map( record => `@${ record.get( "Slack User" ) }` );
slashCommand.replyPrivate( message, `*Mentees for ${ message.text }:*\n ${ mentees.join( "\n" ) }` );
} );
}
break;
/* command: /mentors <language> */
case "/mentors":
if (message.text){
if (message.text.length < 3){
slashCommand.replyPrivate(message, '*Length of search param must be 3 or more characters.*');
return;
}
base( "Mentors" ).select( {
filterByFormula: `SEARCH(LOWER("${ message.text }"), LOWER({Skillsets})) >= 1`
, view: "Main View"
} ).firstPage( ( err, records ) => {
if ( err ) {
return console.error( err );
}
const mentors = records.map( record => `@${ record.get( "Slack Name" ) }` );
slashCommand.replyPrivate( message, `*Mentors for ${ message.text }:*\n ${ mentors.join( "\n" ) }` );
} );
}
break;
case "/test":
slashCommand.replyPrivate("You've hit the server with /test/");
break;
/* command: /android <message> */
//TODO: Add better verification method for authorized users
case "/android":
slashCommand.replyPrivate(`From the android webhook`);
// if (message.text && message.user_name == "wcriss44"){
// // if (message.text.length < 3){
// // slashCommand.replyPrivate(message, '*Length of notification must be 3 or more characters.*');
// // return;
// // }
// xhr = new XMLHttpRequest();
// var url = "https://fcm.googleapis.com/fcm/send";
// //Headers
// xhr.open("POST", url, true);
// xhr.setRequestHeader("Content-type", "application/json");
// // Replace {serverKey} with local variable
// xhr.setRequestHeader("Authorization", process.env.firebaseServer);
// xhr.onreadystatechange = function () {
// if (xhr.readyState == 4 && xhr.status == 200) {
// //Notify if response status is ok
// slashCommand.replyPrivate(message, "Notification sent!");
// }
// };
// //Body
// var data = JSON.stringify({"to":"/topics/Scholarships","notification":{
// "title": "New Scholarship", "body": message.text}});
// xhr.send(data);
// }
break;
default:
slashCommand.replyPrivate(message, "I'm afraid I don't know how to " + message.command + " yet.");
}
});