Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created December 14, 2012 01:41
Show Gist options
  • Select an option

  • Save mheadd/4281807 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/4281807 to your computer and use it in GitHub Desktop.
var Config = function() {
// General
this.listen_port = 3000;
// Twilio
this.twilio_sid = "";
this.twilio_token = "";
this.twilio_outgoing = "";
};
Config.prototype.toTitleCase = function (string)
{
return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
module.exports = function() {
return new Config();
};
var http = require('http');
var qs = require('querystring');
var RestClient = require('twilio').RestClient;
var phlPollFinder = require('phl-poll-finder')();
var config = require('./config')();
// Create HTTP server to process inbound requests from Twilio
var server = http.createServer(function inboundSMSServer(req, res) {
// Variable to hold JSON payload.
var payload = '';
// Get payload from Twilio.
req.addListener('data', function getPayload(data) {
payload += data;
});
// Process inbound message.
req.addListener('end', function processPayload() {
var message = qs.parse(payload);
var from = message.From;
var address = message.Body;
phlPollFinder.find(address, function (result) {
// Prepare response.
var response = "You vote at: ";
response += config.toTitleCase(result[0].attributes.POLLING_PL) + ", " + config.toTitleCase(result[0].attributes.ADDRESS);
console.log(from + ": " + response);
// Send response SMS message.
var sms = new RestClient(config.twilio_sid, config.twilio_token);
sms.sendSms(config.twilio_outgoing, from, response, function sendSMS() {
res.writeHead(200);
res.end();
});
});
});
}).listen(3000);
{
"name": "philly-finder",
"version": "0.0.1",
"description": "SMS-based polling locator for Philly",
"main": "index.js",
"dependencies": {
"phl-poll-finder": "*",
"twilio": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment