Created
July 4, 2015 18:50
-
-
Save spalladino/47f7539a5e346a9c97e9 to your computer and use it in GitHub Desktop.
Verboice API demo from node.js
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 request = require('request'); | |
var express = require('express'); | |
var http = require('http'); | |
var url = require('url'); | |
// Parse command line options | |
var program = require('commander'); | |
program | |
.version("1.0.0") | |
.option('-p, --port <port>', 'Port on which to listen to status callbacks (defaults to 8080)', parseInt) | |
.option('-a, --address <number>', 'Number to call') | |
.option('-c, --channel <channel>', 'ID of the channel to use for the call') | |
.option('-f, --call-flow <call_flow>', 'ID of the call flow to use for the call') | |
.parse(process.argv); | |
var port = program.port || 8080; | |
var call_flow = program.call_flow || 1201; | |
var channel = program.channel || "spalladino-twilio"; | |
var address = program.address || "+16314857346"; | |
var projectId = program.project || 836; | |
// Set up basic authentication | |
var auth = 'Basic ' + new Buffer(process.env.VERBOICE_USER + ':' + process.env.VERBOICE_PASS).toString('base64'); | |
// Request to make the call | |
request.post('http://verboice.instedd.org/api/call', { | |
form: { | |
channel: channel, | |
address: address, | |
call_flow_id: call_flow | |
}, | |
headers: { | |
'Authorization': auth | |
} | |
}, function(err, response, body) { | |
console.log(body); | |
}); | |
// Get info from a phone number | |
var queryPhonebook = function(phone) { | |
request.get('http://verboice.instedd.org/api/projects/' + projectId + '/contacts/by_address/' + phone + '.json', { | |
headers: { | |
'Authorization': auth | |
} | |
}, function(err, response, body) { | |
console.log("Contact " + phone + ":") | |
console.log(JSON.parse(body).vars); | |
}); | |
}; | |
// Listen to the server response | |
var server = http.createServer(function(req, res) { | |
var queryArgs = url.parse(req.url, true).query; | |
console.log("Call status updated:") | |
console.log(queryArgs); | |
if (queryArgs.CallStatus == 'completed') { | |
queryPhonebook(queryArgs.From); | |
} | |
res.writeHead(200); | |
res.end(); | |
}); | |
server.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment