forked from twilio/starter-node
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
84 lines (73 loc) · 2.78 KB
/
app.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
79
80
81
82
83
84
// Load app dependencies
var http = require('http'),
path = require('path'),
express = require('express'),
twilio = require('twilio');
// Load configuration information from system environment variables.
var TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN = process.env.TWILIO_AUTH_TOKEN,
TWILIO_NUMBER = process.env.TWILIO_NUMBER;
// Create an authenticated client to access the Twilio REST API
var client = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
// Create an Express web application with some basic configuration
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// render our home page
app.get('/', function(request, response) {
response.render('index');
});
// handle a POST request to send a text message. This is sent via ajax on our
// home page
app.post('/message', function(request, response) {
// Use the REST client to send a text message
client.sendSms({
to: request.param('to'),
from: TWILIO_NUMBER,
body: 'Have fun with your Twilio development!'
}, function(err, data) {
// When we get a response from Twilio, respond to the HTTP POST request
response.send('Message is inbound!');
});
});
// handle a POST request to make an outbound call. This is sent via ajax on our
// home page
app.post('/call', function(request, response) {
// Use the REST client to send a text message
client.makeCall({
to: request.param('to'),
from: TWILIO_NUMBER,
url: 'http://twilio-elearning.herokuapp.com/starter/voice.php'
}, function(err, data) {
// When we get a response from Twilio, respond to the HTTP POST request
response.send('Call incoming!');
});
});
// Create a TwiML document to provide instructions for an outbound call
app.get('/hello', function(request, response) {
// Create a TwiML generator
var twiml = new twilio.TwimlResponse();
twiml.say('Hello there! You have successfully configured a web hook.');
twiml.say('Have fun with your Twilio development!', {
voice:'woman'
});
// Return an XML response to this request
response.set('Content-Type','text/xml');
response.send(twiml.toString());
});
// Start our express app, by default on port 3000
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});