Skip to content

Commit bb77f42

Browse files
Add node snippets for all folders except taskrouter and rest.
1 parent 48bd462 commit bb77f42

File tree

29 files changed

+680
-0
lines changed

29 files changed

+680
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const http = require('http');
2+
const express = require('express');
3+
const twilio = require('twilio');
4+
5+
const app = express();
6+
7+
app.get('/token', function(req, res) {
8+
// put your Twilio API credentials here
9+
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
10+
const authToken = 'your_auth_token';
11+
12+
// put your Twilio Application Sid here
13+
const appSid = 'APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
14+
15+
let capability = new twilio.jwt.Capability(accountSid, authToken);
16+
capability.allowClientOutgoing(appSid);
17+
const token = capability.generate();
18+
19+
res.set('Content-Type', 'application/jwt');
20+
res.send(token);
21+
});
22+
23+
app.post('/voice', function(req, res) {
24+
// TODO: Create TwiML response
25+
});
26+
27+
http.createServer(app).listen(1337, '127.0.0.1');
28+
console.log('Twilio Client app server running at http://127.0.0.1:1337/token/');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const http = require('http');
2+
const express = require('express');
3+
const bodyParser = require('body-parser');
4+
const twilio = require('twilio');
5+
6+
const app = express();
7+
app.use(bodyParser.urlencoded({extended: false}));
8+
9+
app.post('/voice', function(req, res) {
10+
// Create TwiML response
11+
let twiml = new twilio.TwimlResponse();
12+
13+
if(req.body.To) {
14+
twiml.dial({callerId: '+15017250604'}, function() {
15+
// wrap the phone number or client name in the appropriate TwiML verb
16+
// by checking if the number given has only digits and format symbols
17+
if (/^[\d\+\-\(\) ]+$/.test(req.body.To)) {
18+
this.number(req.body.To);
19+
} else {
20+
this.client(req.body.To);
21+
}
22+
});
23+
} else {
24+
twiml.say('Thanks for calling!');
25+
}
26+
27+
res.set('Content-Type', 'text/xml');
28+
res.send(twiml.toString());
29+
});
30+
31+
app.get('/token', function(req, res) {
32+
// TODO: generate token
33+
});
34+
35+
http.createServer(app).listen(1337, '127.0.0.1');
36+
console.log('Twilio Client app server running at http://127.0.0.1:1337/voice/');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const http = require('http');
2+
const express = require('express');
3+
const bodyParser = require('body-parser');
4+
const twilio = require('twilio');
5+
6+
const app = express();
7+
app.use(bodyParser.urlencoded({extended: false}));
8+
9+
app.post('/voice', function(req, res) {
10+
// Create TwiML response
11+
let twiml = new twilio.TwimlResponse();
12+
13+
if(req.body.To) {
14+
twiml.dial({callerId: '+15017250604'}, function() {
15+
this.number(req.body.To);
16+
});
17+
} else {
18+
twiml.say('Thanks for calling!');
19+
}
20+
21+
res.set('Content-Type', 'text/xml');
22+
res.send(twiml.toString());
23+
});
24+
25+
app.get('/token', function(req, res) {
26+
// TODO: generate token
27+
});
28+
29+
http.createServer(app).listen(1337, '127.0.0.1');
30+
console.log('Twilio Client app server running at http://127.0.0.1:1337/voice/');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const http = require('http');
2+
const express = require('express');
3+
const twilio = require('twilio');
4+
5+
const app = express();
6+
7+
app.post('/voice', function(req, res) {
8+
// Create TwiML response
9+
let twiml = new twilio.TwimlResponse(); // FIXME: hows the class now?
10+
twiml.say('Thanks for calling!');
11+
12+
res.set('Content-Type', 'text/xml');
13+
res.send(twiml.toString());
14+
});
15+
16+
app.get('/token', function(req, res) {
17+
// TODO: generate token
18+
});
19+
20+
http.createServer(app).listen(1337, '127.0.0.1');
21+
console.log('Twilio Client app server running at http://127.0.0.1:1337/voice/');
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
const express = require('express');
3+
const twilio = require('twilio');
4+
const urlencoded = require('body-parser').urlencoded;
5+
6+
// Update with your own phone number in E.164 format
7+
const MODERATOR = '+15558675309';
8+
9+
let app = express();
10+
11+
// Parse incoming POST params with Express middleware
12+
app.use(urlencoded({extended: false}));
13+
14+
// Create a route that will handle Twilio webhook requests, sent as an
15+
// HTTP POST to /voice in our application
16+
app.post('/voice', (request, response) => {
17+
// Use the Twilio Node.js SDK to build an XML response
18+
let twiml = new twilio.TwimlResponse();
19+
20+
// Start with a <Dial> verb
21+
twiml.dial(function(dialNode) {
22+
// If the caller is our MODERATOR, then start the conference when they
23+
// join and end the conference when they leave
24+
if(request.body.From == MODERATOR) {
25+
dialNode.conference('My conference', {
26+
startConferenceOnEnter: true,
27+
endConferenceOnExit: true,
28+
});
29+
} else {
30+
// Otherwise have the caller join as a regular participant
31+
dialNode.conference('My conference', {
32+
startConferenceOnEnter: false,
33+
});
34+
}
35+
});
36+
37+
// Render the response as XML in reply to the webhook request
38+
response.type('text/xml');
39+
response.send(twiml.toString());
40+
});
41+
42+
// Create an HTTP server and listen for requests on port 3000
43+
console.log('Twilio Client app HTTP server running at http://127.0.0.1:3000');
44+
app.listen(3000);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
const express = require('express');
3+
const twilio = require('twilio');
4+
const urlencoded = require('body-parser').urlencoded;
5+
6+
let app = express();
7+
8+
// Parse incoming POST params with Express middleware
9+
app.use(urlencoded({extended: false}));
10+
11+
// Create a route that will handle Twilio webhook requests, sent as an
12+
// HTTP POST to /voice in our application
13+
app.post('/voice', (request, response) => {
14+
// Use the Twilio Node.js SDK to build an XML response
15+
let twiml = new twilio.TwimlResponse();
16+
17+
// Use the <Gather> verb to collect user input
18+
twiml.gather({numDigits: 1}, (gatherNode) => {
19+
gatherNode.say('For sales, press 1. For support, press 2.');
20+
});
21+
22+
// If the user doesn't enter input, loop
23+
twiml.redirect('/voice');
24+
25+
// Render the response as XML in reply to the webhook request
26+
response.type('text/xml');
27+
response.send(twiml.toString());
28+
});
29+
30+
// Create an HTTP server and listen for requests on port 3000
31+
console.log('Twilio Client app HTTP server running at http://127.0.0.1:3000');
32+
app.listen(3000);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
app.post('/voice', (request, response) => {
2+
// Use the Twilio Node.js SDK to build an XML response
3+
let twiml = new twilio.TwimlResponse();
4+
5+
// helper function to set up a <Gather>
6+
function gather() {
7+
twiml.gather({numDigits: 1}, (gatherNode) => {
8+
gatherNode.say('For sales, press 1. For support, press 2.');
9+
});
10+
11+
// If the user doesn't enter input, loop
12+
twiml.redirect('/voice');
13+
}
14+
15+
// If the user entered digits, process their request
16+
if (request.body.Digits) {
17+
switch (request.body.Digits) {
18+
case '1': twiml.say('You selected sales. Good for you!'); break;
19+
case '2': twiml.say('You need support. We will help!'); break;
20+
default:
21+
twiml.say('Sorry, I don\'t understand that choice.').pause();
22+
gather();
23+
break;
24+
}
25+
} else {
26+
// If no input was sent, use the <Gather> verb to collect user input
27+
gather();
28+
}
29+
30+
// Render the response as XML in reply to the webhook request
31+
response.type('text/xml');
32+
response.send(twiml.toString());
33+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Create a route that will handle Twilio webhook requests, sent as an
2+
// HTTP POST to /voice in our application
3+
app.post('/voice', (request, response) => {
4+
// Use the Twilio Node.js SDK to build an XML response
5+
let twiml = new twilio.TwimlResponse();
6+
7+
twiml.gather({
8+
numDigits: 1,
9+
action: '/gather',
10+
}, (gatherNode) => {
11+
gatherNode.say('For sales, press 1. For support, press 2.');
12+
});
13+
14+
// If the user doesn't enter input, loop
15+
twiml.redirect('/voice');
16+
17+
// Render the response as XML in reply to the webhook request
18+
response.type('text/xml');
19+
response.send(twiml.toString());
20+
});
21+
22+
// Create a route that will handle <Gather> input
23+
app.post('/gather', (request, response) => {
24+
// Use the Twilio Node.js SDK to build an XML response
25+
let twiml = new twilio.TwimlResponse();
26+
27+
// If the user entered digits, process their request
28+
if (request.body.Digits) {
29+
switch (request.body.Digits) {
30+
case '1': twiml.say('You selected sales. Good for you!'); break;
31+
case '2': twiml.say('You need support. We will help!'); break;
32+
default:
33+
twiml.say('Sorry, I don\'t understand that choice.').pause();
34+
twiml.redirect('/voice');
35+
break;
36+
}
37+
} else {
38+
// If no input was sent, redirect to the /voice route
39+
twiml.redirect('/voice');
40+
}
41+
42+
// Render the response as XML in reply to the webhook request
43+
response.type('text/xml');
44+
response.send(twiml.toString());
45+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Download the Node helper library from twilio.com/docs/node/install
2+
// These vars are your accountSid and authToken from twilio.com/user/account
3+
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
4+
const authToken = 'your_auth_token';
5+
let client = require('twilio')(accountSid, authToken);
6+
7+
client.calls.create({
8+
url: 'http://demo.twilio.com/docs/voice.xml',
9+
to: '+14155551212',
10+
from: '+15017250604',
11+
record: true,
12+
}, function(err, call) {
13+
process.stdout.write(call.sid);
14+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
const express = require('express');
3+
const twilio = require('twilio');
4+
5+
let app = express();
6+
7+
// Returns TwiML which prompts the caller to record a message
8+
app.post('/record', (request, response) => {
9+
// Use the Twilio Node.js SDK to build an XML response
10+
let twiml = new twilio.TwimlResponse();
11+
twiml.say('Hello. Please leave a message after the beep.');
12+
13+
// Use <Record> to record and transcribe the caller's message
14+
twiml.record({transcribe: true, maxLength: 30});
15+
16+
// End the call with <Hangup>
17+
twiml.hangup();
18+
19+
// Render the response as XML in reply to the webhook request
20+
response.type('text/xml');
21+
response.send(twiml.toString());
22+
});
23+
24+
// Create an HTTP server and listen for requests on port 3000
25+
app.listen(3000);

0 commit comments

Comments
 (0)