Created
December 14, 2012 01:41
-
-
Save mheadd/4281807 to your computer and use it in GitHub Desktop.
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
| 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(); | |
| }; |
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
| 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); |
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
| { | |
| "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