forked from DEGoodmanWilson/easy-peasy-slash-command-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (87 loc) · 3.77 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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Uses the slack button feature to offer a real time bot to multiple teams */
var Botkit = require('botkit');
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);
}
var config = {}
if (process.env.MONGOLAB_URI) {
var BotkitStorage = require('botkit-storage-mongo');
config = {
storage: BotkitStorage({mongoUri: process.env.MONGOLAB_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'],
}
);
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!');
}
});
});
//
// BEGIN EDITING HERE!
//
controller.on('slash_command', function (slashCommand, message) {
switch (message.command) {
case "/echo": //handle the `/echo` slash command. We might have others assigned to this app too!
// The rules are simple: If there is no text following the command, treat it as though they had requested "help"
// Otherwise just echo back to them what they sent us.
// but first, let's make sure the token matches!
if (message.token !== process.env.VERIFICATION_TOKEN) return; //just ignore it.
// if no text was supplied, treat it as a help command
if (message.text === "" || message.text === "help") {
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
//TODO You do it!
slashCommand.replyPublic(message, "1", function() {
slashCommand.replyPublicDelayed(message, "2").then(slashCommand.replyPublicDelayed(message, "3"));
});
break;
default:
slashCommand.replyPublic(message, "I'm afraid I don't know how to " + message.command + " yet.");
}
})
;