-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.37 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
'use strict';
const http = require('http');
const express = require('express');
const { urlencoded } = require('body-parser');
const twilio = require('twilio');
const ClientCapability = twilio.jwt.ClientCapability;
const VoiceResponse = twilio.twiml.VoiceResponse;
let app = express();
app.use(express.static(__dirname + '/public'));
app.use(urlencoded({ extended: false }));
require('dotenv').load();
// Generate a Twilio Client capability token
app.get('/token', (request, response) => {
const capability = new ClientCapability({
accountSid: process.env.TWILIO_ACCOUNT_SID,
authToken: process.env.TWILIO_AUTH_TOKEN
});
capability.addScope(
new ClientCapability.OutgoingClientScope({
applicationSid: process.env.TWILIO_TWIML_APP_SID})
);
const token = capability.toJwt();
// Include token in a JSON response
response.send({
token: token
});
});
// Create TwiML for outbound calls
app.post('/voice', (request, response) => {
const voiceResponse = new VoiceResponse();
voiceResponse.dial({
callerId: process.env.TWILIO_NUMBER,
record: 'record-from-answer',
}, request.body.number);
response.type('text/xml');
response.send(voiceResponse.toString());
});
let server = http.createServer(app);
let port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Express Server listening on *:${port}`);
});
module.exports = app;