Last active
December 18, 2015 12:28
-
-
Save ruzz311/5782498 to your computer and use it in GitHub Desktop.
playing with twilio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var express = require('express'), | |
http = require('http'), | |
path = require('path'), | |
twilio = require('twilio'), | |
radish = "\"In all my days as a turnip farmer, I have not yet experienced the love, of a white radish!\", thought the farmer as he sighed then walked up the dusty warn trail, towards the barn. How long had he been there? One hour? Two Hours? The sun was shining bright and warm against the old man's skin and that's when he drifted off to sleep."; | |
persona = { | |
lilly : { voice:'woman', language:'en-gb' }, | |
niles : { voice:'man', language:'en-gb' }, | |
hank : { voice:'man', language:'en-us' }, | |
jacques : { voice:'man', language:'fr' } | |
}; | |
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()); | |
}); | |
// Config Twilio | |
var config = {}; | |
if (process.env.TWILIO_KEY) { | |
config.accountSid = process.env.TWILIO_KEY; | |
config.authToken = process.env.TWILIO_TOKEN; | |
config.appid = process.env.TWILIO_APPID; | |
config.from = process.env.TWILIO_FROM; | |
} else { | |
config = require('config'); | |
} | |
app.post('/callfinished', function(req, res){ | |
console.log('call finished'); | |
}); | |
//Twilio request authentication | |
app.post('/twiml', function(req, res) { | |
var resp; | |
// dont allow calls outside of twilio | |
// if (!twilio.validateExpressRequest(req, config.authToken)) { | |
// return res.send('you are not twilio. Buzz off.'); | |
// } | |
resp = new twilio.TwimlResponse(); | |
resp.gather({ | |
action : 'http://'+req.host+'/step2', | |
finishOnKey : '123', | |
numDigits : '1' | |
}, function() { | |
this.say('Press 1 for perky cheer leaders') | |
.say('Press 2 for nasty skags gov-nah', persona.niles) | |
.say('Press 3 for stinky cheese man', persona.jacques); | |
}); | |
resp | |
.say('YOU ARE TOO SLOW!', persona.lilly) | |
.redirect('/twiml',function() { console.log(arguments); }); | |
res.type('text/xml'); | |
res.send(resp.toString()); | |
}); | |
app.post('/step2',function(req, res){ | |
resp = new twilio.TwimlResponse(); | |
var digit = req.body.Digits; | |
switch(digit){ | |
case '1': | |
resp.gather({ | |
action : 'http://'+req.host+'/step2', | |
finishOnKey : '123456789#*', | |
numDigits : '1', | |
timeout : 70 | |
}, function() { | |
this.say('Welcome to the sexy music server!', persona.lilly) | |
.say('Please wait while I pick a song. You gonna love the way I twerk it?', persona.lilly) | |
.play('http://'+req.host+'/Music/kiloali.mp3', { loop:2 }); | |
}); | |
break; | |
case '2': | |
resp.say(radish, persona.niles); | |
break; | |
case '3': | |
resp.say(radish, persona.jacques); | |
break; | |
default: break; | |
} | |
resp.redirect('/twiml',function() { console.log(arguments); }); | |
res.type('text/xml'); | |
res.send(resp.toString()); | |
}); | |
http.createServer(app).listen(app.get('port'), '0.0.0.0', function () { | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment