-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
99 lines (89 loc) · 2.89 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
require('dotenv').load();
var express = require('express'),
app = express(),
slackAPI = require('slackbotapi'),
config = require('config.json')('./config.json');
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
const slackToken = process.env.SLACKTOKEN
const lunchChannelId = (process.env.NODE_ENV === 'testing') ? process.env.TESTING_CHANNEL_ID : process.env.CHANNEL_ID;
const twilioAccountSid = process.env.TWILIO_SID;
const twilioAuthToken = process.env.TWILIO_TOKEN;
var forkableDriverNumber;
var slackbot = new slackAPI({
'token': slackToken,
'logging': true,
'autoReconnect': true,
'name': 'LunchBot'
});
var slackEmoji = {
icon_emoji: ':camchef:'
};
// Logging output for testing
if (process.env.NODE_ENV === 'testing') {
slackbot.on('message', function(data) {
if (data) {
console.log(data);
}
});
}
var twilio = require('twilio');
var client = new twilio.RestClient(twilioAccountSid, twilioAuthToken);
app.post('/webhook/twilio/sms', function(req, res) {
forkableDriverNumber = req.body.From;
slackbot.sendMsg(lunchChannelId, 'Forkable Driver: ' + req.body.Body.toString() + '\nFrom Number: ' + forkableDriverNumber, slackEmoji);
var twiml = new twilio.TwimlResponse();
console.log(lunchChannelId)
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(twiml.toString());
});
app.post('/webhook/twilio/voice', function(req, res) {
var call = req.body;
forkableDriverNumber = call.From;
var twiml = new twilio.TwimlResponse();
var Notifiees = [];
for (var i = 0; i < config.PeopleToNotify.length; i++) {
twiml.dial('number', config.PeopleToNotify[i].Number);
Notifiees.push(config.PeopleToNotify[i].Name)
}
slackbot.sendMsg(lunchChannelId, 'Forkable Driver is Calling \n Forwarding to ' + Notifiees.toString() + '\n Call from: ' + forkableDriverNumber, slackEmoji);
console.log(twiml.toString())
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(twiml.toString());
});
slackbot.on('message', function(data) {
if (data.type === "message") {
var message = data.text;
if (message === undefined) {
return;
}
message = message.toLowerCase();
if (data.channel === lunchChannelId) {
if (message.substring(0, 12) === 'lunchbot sms') {
var reply = data.text.substring(12);
client.messages.create({
to: forkableDriverNumber,
from: config.TwilioNumber,
body: reply
}, function(err, message) {
if (err) {
if(err.code === 21604){
slackbot.sendMsg(lunchChannelId, "Sorry I don't know who to text, a little brain dead right now.", slackEmoji);
}
} else {
console.log(message.sid);
}
});
}
}
}
});
app.listen(app.get('port'), function() {});