-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnode-incoming-sms-to-slack.js
52 lines (44 loc) · 1.14 KB
/
node-incoming-sms-to-slack.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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const server = require('http').Server(app);
const https = require('https')
const port = 5501;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
server.listen(port, (err) => {
if (err) {
throw err;
}
console.log('Server running on port ' + port);
});
const webhookURL = "<YOUR SLACK WEBHOOK>";
const callback = (response) => {
var str = ''
response.on('data', (chunk) => {
str += chunk
})
response.on('end', () => {
console.log(str)
})
}
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
function createText(message){
const userAccountNotification = {
'username': 'SMS Notifier', // This will appear as user name who posts the message
'text': message, // text
};
return userAccountNotification
}
app.post('/slack', (req, res) => {
messageBody = JSON.stringify(createText(req.body.message));
var request = https.request(webhookURL,options, callback)
request.write(messageBody)
request.end()
res.end();
});