diff --git a/index.js b/index.js index 125faf1..76faefb 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ 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')); @@ -12,12 +14,17 @@ app.use(urlencoded({ extended: false })); // Generate a Twilio Client capability token app.get('/token', (request, response) => { - let capability = new twilio.Capability( - process.env.TWILIO_ACCOUNT_SID, - process.env.TWILIO_AUTH_TOKEN + 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}) ); - capability.allowClientOutgoing(process.env.TWILIO_TWIML_APP_SID); - let token = capability.generate(); + + const token = capability.toJwt();; // Include token in a JSON response response.send({ @@ -27,12 +34,12 @@ app.get('/token', (request, response) => { // Create TwiML for outbound calls app.post('/voice', (request, response) => { - let twiml = new twilio.TwimlResponse(); - twiml.dial(request.body.number, { + let voiceResponse = new VoiceResponse(); + voiceResponse.dial({ callerId: process.env.TWILIO_NUMBER - }); + }, request.body.number); response.type('text/xml'); - response.send(twiml.toString()); + response.send(voiceResponse.toString()); }); let server = http.createServer(app); diff --git a/package.json b/package.json index b8a5110..0e10e88 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "body-parser": "^1.15.2", "dotenv-safe": "^3.0.0", "express": "^4.14.0", - "twilio": "^2.11.0" + "twilio": "~3.0.0" }, "devDependencies": { "chai": "^3.5.0", diff --git a/public/index.html b/public/index.html index f4622d2..f6d3306 100644 --- a/public/index.html +++ b/public/index.html @@ -3,7 +3,7 @@ Browser Dialer UI with React - @@ -17,10 +17,10 @@ - + - + - \ No newline at end of file + diff --git a/spec/index.spec.js b/spec/index.spec.js index 4ca49d7..d6302a4 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -32,3 +32,19 @@ describe('index route', function () { }); }); }); + +describe('token route', function () { + describe('GET /token', function () { + it('responds with token', function (done) { + var testApp = supertest(app); + testApp + .get('/token') + .expect(200) + .end(function(err, res) { + const jsonResponse = JSON.parse(res.text); + expect(jsonResponse.token.length).to.be.above(0); + done(); + }); + }); + }); +});