-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.js
executable file
·78 lines (66 loc) · 2.38 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
const config = require('./config');
const express = require('express');
const bodyParser = require('body-parser');
const twilio = require('twilio');
const ngrok = require('ngrok');
const app = new express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/token/:identity', (request, response) => {
const identity = request.params.identity;
const accessToken = new twilio.jwt.AccessToken(config.twilio.accountSid, config.twilio.apiKey, config.twilio.apiSecret);
const chatGrant = new twilio.jwt.AccessToken.ChatGrant({
serviceSid: config.twilio.chatServiceSid,
});
accessToken.addGrant(chatGrant);
accessToken.identity = identity;
response.set('Content-Type', 'application/json');
response.send(JSON.stringify({
token: accessToken.toJwt(),
identity: identity
}));
})
app.listen(config.port, () => {
console.log(`Application started at localhost:${config.port}`);
});
// ============================================
// ============================================
// ====== HANDLE NEW-CONVERSATION HOOK ========
// ============================================
// ============================================
let client = new twilio(config.twilio.accountSid, config.twilio.authToken);
app.post('/chat', (req, res) => {
console.log("Received a webhook:", req.body);
if (req.body.EventType === 'onConversationAdded') {
const me = "Tackleton";
client.conversations.v1.conversations(req.body.ConversationSid)
.participants
.create({
identity: me
})
.then(participant => console.log(`Added ${participant.identity} to ${req.body.ConversationSid}.`))
.catch(err => console.error(`Failed to add a member to ${req.body.ConversationSid}!`, err));
}
console.log("(200 OK!)");
res.sendStatus(200);
});
app.post('/outbound-status', (req, res) => {
console.log(`Message ${req.body.SmsSid} to ${req.body.To} is ${req.body.MessageStatus}`);
res.sendStatus(200);
})
app.use((error, req, res, next) => {
res.status(500)
res.send({error: error})
console.error(error.stack)
next(error)
})
var ngrokOptions = {
proto: 'http',
addr: config.port
};
if (config.ngrokSubdomain) {
ngrokOptions.subdomain = config.ngrokSubdomain
}
ngrok.connect(ngrokOptions).then(url => {
console.log('ngrok url is ' + url);
}).catch(console.error);