Skip to content

Instantly share code, notes, and snippets.

@zbowling
Created June 12, 2011 06:22
Show Gist options
  • Save zbowling/1021301 to your computer and use it in GitHub Desktop.
Save zbowling/1021301 to your computer and use it in GitHub Desktop.
var opts = {};
var io = require('socket.io');
opts.port = 20200;
var rest = require('restler');
var app = require('express').createServer();
var sys = require('sys');
var TwilioClient = require('twilio').Client;
var Twiml = require("twilio").Twiml;
var client = new TwilioClient(ACCOUNT_SID, AUTH_TOKEN, MY_HOSTNAME,opts);
var callcount = 0;
app.get("/phone/count", function(req, res){
//res.sendfile("phone.html");
res.send('Phone calls ' + callcount);
});
app.get('/phone', function(req, res){
res.sendfile("phone.html");
//res.send('Phone calls ' + callcount);
});
var socket = io.listen(app);
var phone = client.getPhoneNumber("+14157993690");
var isPhoneReady = false;
phone.setup(function(){
isPhoneReady = true;
phone.on('incomingCall', function(reqParams, res) {
callcount+=1;
var obj = {};
obj.calls = callcount;
obj.msg = "new call from " + reqParams.Caller;
socket.broadcast(obj);
var getZip = new Twiml.Gather(null, {numDigits: 5});
/*var phoneNum = "";
phoneNum = reqParams.Caller[2] + " " + reqParams.Caller[3] + " " + reqParams.Caller[4] + ", ";
phoneNum += reqParams.Caller[5] + " " + reqParams.Caller[6] + " " + reqParams.Caller[7] + ", ";
phoneNum += reqParams.Caller[8] + " " + reqParams.Caller[9] + " " + reqParams.Caller[10] + " " + reqParams.Caller[11];
*/
getZip.append(new Twiml.Say('Welcome to congress connector. Enter your 5 digit zip code!'));
getZip.on("gathered", function(gParams, gresp) {
var obj = {};
obj.msg = "caller " + reqParams.Caller + " entered zip " + gParams.Digits;
socket.broadcast(obj);
var path = "http://services.sunlightlabs.com/api/legislators.allForZip.json?apikey=d85fe987cb55459385acf04f2c7e9ed6&zip=" + gParams.Digits;
var options = {
host: "services.sunlightlabs.com",
port: 80,
path: path,
method: 'GET'
};
rest.get(path).on('complete', function(data) {
var legislators = data.response.legislators;
var text = "Found " + legislators.length + " legislators. ";
var obj = {};
obj.msg = "For " + reqParams.Caller + " " + text;
socket.broadcast(obj);
var presscount;
for (presscount = 0; presscount<legislators.length;presscount++)
{
var membername = legislators[presscount].legislator.firstname + " " + legislators[presscount].legislator.lastname;
text += "Press " + (presscount + 1) + " for " + membername + ".,";
}
var getLeg = new Twiml.Gather(null, {numDigits: 1});
getLeg.append(new Twiml.Say(text));
getLeg.on("gathered", function(lParams, lresp) {
var index = lParams.Digits-1;
if (index >= 0 && index < legislators.length)
{
var legi = legislators[index].legislator;
var obj = {};
obj.msg = "Connecting " + reqParams.Caller + " to " + legi.firstname + " " + legi.lastname + " (" + legi.phone + ")";
socket.broadcast(obj);
lresp.append(new Twiml.Say("Connecting you! Thanks!,"));
var dial = new Twiml.Dial(legi.phone);
lresp.append(dial);
}
else
{
lresp.append(new Twiml.Say("Invalid input. Try again."));
}
lresp.send();
});
gresp.append(getLeg);
socket.broadcast(data)
gresp.send();
});
});
res.append(getZip);
res.send();
});
});
socket.on('connection', function(client){
var obj = {};
obj.calls = callcount;
client.send(obj);
})
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment