Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ 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 }));

// 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({
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<title>Browser Dialer UI with React</title>
<link rel="stylesheet"
<link rel="stylesheet"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet"
href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
Expand All @@ -17,10 +17,10 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js"></script>
<script src="//unpkg.com/babel-core@5.8.38/browser.min.js"></script>

<script src="//media.twiliocdn.com/sdk/js/client/v1.3/twilio.min.js"></script>
<script src="//media.twiliocdn.com/sdk/js/client/v1.4/twilio.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/babel" src="dialer.jsx"></script>
</body>
</html>
</html>
16 changes: 16 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});