Created
March 11, 2016 22:55
-
-
Save kwhinnery/d6019d2b78d2836f64a3 to your computer and use it in GitHub Desktop.
This file contains 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
var http = require('http'); | |
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var KeenClient = require('keen-js'); | |
// 1.) create an authenticated Keen analytics client | |
var keen = new KeenClient({ | |
projectId: 'your project ID here', | |
writeKey: 'your write key here' | |
}); | |
// 2.) Create an Express web application | |
var app = express(); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.set('view engine', 'ejs'); | |
// 3.) Create an endpoint to handle incoming calls from Twilio | |
app.post('/call', function(request, response) { | |
// 4.) Send an event to Keen tracking the incoming call | |
keen.addEvent('calls', { | |
number: request.body.To, // The twilio number that got the call | |
city: request.body.FromCity // the city the call came from | |
}, function(error, apiResponse) { | |
// 5.) Return TwiML to provide instructions for what to do during the | |
// inbound call | |
// First, set the response Content-Type to XML | |
response.type('text/xml'); | |
// Then, decide what message to read back to the user. If the Keen API | |
// request was successful, then read back a happy message. If not, read | |
// a sad message :( | |
if (error) { | |
console.log(error); | |
return response.render('twiml', { message: 'Sorry, an error happened.' }); | |
} | |
response.render('twiml', { message: 'Your call has been recorded!' }) | |
}); | |
}); | |
// 6.) Start web application | |
var server = http.createServer(app); | |
server.listen(process.env.PORT || 3000, function() { | |
console.log('Express server started.'); | |
}); |
This file contains 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
<Response> | |
<Say voice="alice"><%= message %></Say> | |
</Response> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment