-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (66 loc) · 2.02 KB
/
server.js
File metadata and controls
75 lines (66 loc) · 2.02 KB
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
var Slack = require('slack-node');
var express = require('express');
var bodyParser = require('body-parser');
// Setup Slack API
var slackApiToken = process.env.SLACK_API_TOKEN || 'your Slack API token here';
var slack = new Slack(slackApiToken);
// Setup users you want to invite the created channel
// Find user ID:s here https://api.slack.com/methods/users.list/test
var supportUserIds = ['U02UXACCS','U02A60UCV'];
// Setup Express
var app = express();
var server = app.listen( 1337, function() {
console.log('Express listening at http://%s:%s', server.address().address, server.address().port);
});
// Express app configs
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Allow crossdomain
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
// Query messages
app.get('/channel/:id/messages', function(req, res, next) {
if( req.params.id ) {
slack.api('channels.history', {
channel: req.params.id,
oldest: req.query.oldest
}, function(err, response) {
res.send(response);
});
} else {
res.send({ error : 'Data missing'});
}
});
// Create channel
app.post('/channels', function(req, res, next) {
slack.api('channels.create', {
name: req.body.channel
}, function(err, response) {
res.send(response);
// Invite extra users
supportUserIds.forEach(function(userid) {
slack.api('channels.invite', {
channel: response.channel.id,
user: userid
});
});
});
});
// Send message
app.post('/message', function(req, res, next) {
if( req.body.message && req.body.channel ){
slack.api('chat.postMessage', {
text:req.body.message,
channel: req.body.channel,
attachments: req.body.attachments
}, function(err, postMessageResponse){
res.send(postMessageResponse);
});
} else{
res.send({ error : 'Data missing'});
}
});