forked from howdyai/botkit
/
slackbutton_incomingwebhooks.js
121 lines (86 loc) · 3.55 KB
/
slackbutton_incomingwebhooks.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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Slack Button application that allows the application
to post messages into Slack.
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 APP:
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
# USE THE APP
Add the app to your Slack by visiting the login page:
-> http://localhost:3000/login
After you've added the app, send a message using the SUPER INSECURE FORM.
This form is included as an example only, and should definitely not be
left in place if you use this code to start your own project.
Send a message to every team who has added your sample app:
-> http://localhost:3000/
# EXTEND THE APP:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var Botkit = require('../lib/Botkit.js');
if (!process.env.clientId || !process.env.clientSecret || !process.env.port) {
console.log('Error: Specify clientId clientSecret and port in environment');
process.exit(1);
}
var controller = Botkit.slackbot({
json_file_store: './db_slackbutton_incomingwebhook/',
}).configureSlackApp(
{
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
scopes: ['incoming-webhook'],
}
);
controller.setupWebserver(process.env.port,function(err,webserver) {
webserver.get('/',function(req,res) {
var html = '<h1>Super Insecure Form</h1><p>Put text below and hit send - it will be sent to every team who has added your integration.</p><form method="post" action="/unsafe_endpoint"><input type="text" name="text" /><input type="submit"/></form>';
res.send(html);
});
// This is a completely insecure form which would enable
// anyone on the internet who found your node app to
// broadcast to all teams who have added your integration.
// it is included for demonstration purposes only!!!
webserver.post('/unsafe_endpoint',function(req,res) {
var text = req.body.text;
text = text.trim();
controller.storage.teams.all(function(err,teams) {
var count = 0;
for (var t in teams) {
if (teams[t].incoming_webhook) {
count++;
controller.spawn(teams[t]).sendWebhook({
text: text
},function(err) {
if(err) {
console.log(err);
}
});
}
}
res.send('Message sent to ' + count + ' teams!');
});
});
controller.createOauthEndpoints(controller.webserver,function(err,req,res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Success!');
}
});
});
controller.on('create_incoming_webhook',function(bot,webhook_config) {
bot.sendWebhook({
text: ':thumbsup: Incoming webhook successfully configured'
});
})