This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (72 loc) · 2.05 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
var express = require('express');
var app = express();
var url = require('url');
var request = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 9001));
var VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN || 'faketoken';
var API_TOKEN = process.env.API_ACCESS_TOKEN || 'fakeapitoken';
var BOT_TOKEN = process.env.BOT_USER_ACCESS_TOKEN || 'fakebottoken';
function post_message(channel, text) {
request.post("https://slack.com/api/chat.postMessage", {
json: true,
body: {
token: BOT_TOKEN,
channel: channel,
text: text
},
headers: {
'Authorization': 'Bearer ' + BOT_TOKEN,
'Content-Type': 'application/json'
}
}, function (err, res) {
console.log('error', err);
console.log('response', res.body);
});
}
app.get('/', function (req, res) {
res.send('It works!');
});
app.post('/event', function (req, res) {
switch (req.body.type) {
case "url_verification":
var token = req.body.token;
var challenge = req.body.challenge;
if (token === VERIFICATION_TOKEN) {
res.send({
challenge: challenge
});
} else {
res.status(403);
res.send('');
}
break;
case "event_callback":
var event_body = req.body.event;
switch (event_body.type) {
case "app_mention":
post_message(event_body.channel, "🗣🤚");
res.status(200);
res.send('');
break;
default:
console.log('unknown callback event type: ' + event_body.type);
console.log(Object.keys(event_body));
res.status(400);
res.send('');
break;
}
break;
default:
console.log('unknown event type: ' + req.body.type);
console.log(Object.keys(req.body));
res.status(400);
res.send('');
break;
}
});
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});