Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #29 from tombell/twilio-adapter

Twilio adapter
  • Loading branch information...
commit d7d2a2183a12d18e251e108be32051d2f284f7b8 2 parents dc073f6 + 45ea2ad
@atmos atmos authored
Showing with 106 additions and 0 deletions.
  1. +2 −0  bin/hubot
  2. +91 −0 src/hubot/twilio.coffee
  3. +13 −0 src/templates/README.md
View
2  bin/hubot
@@ -59,6 +59,8 @@ else
Hubot = require("hubot/irc").IrcBot
when "campfire"
Hubot = require("hubot/campfire").Campfire
+ when "twilio"
+ Hubot = require("hubot/twilio").Twilio
else
Hubot = require("hubot/shell").Shell
View
91 src/hubot/twilio.coffee
@@ -0,0 +1,91 @@
+Robot = require 'robot'
+Http = require 'http'
+Https = require 'https'
+Qs = require 'querystring'
+
+class Twilio extends Robot
+ constructor: ->
+ @config =
+ sid: process.env.HUBOT_SMS_SID
+ token: process.env.HUBOT_SMS_TOKEN
+ from: process.env.HUBOT_SMS_FROM
+ super
+
+ send: (user, strings...) ->
+ message = strings.join "\n"
+ @post message, user.id, (error, body) ->
+ if error or not body
+ console.log "error sending response: #{error}"
+ else
+ console.log "successful sending #{body}"
+
+ reply: (user, strings...) ->
+ strings.forEach (str) =>
+ @send user, "#{user.name}: #{str}"
+
+ run: ->
+ console.log "Hubot: the SMS reader."
+
+ server = Http.createServer (request, response) =>
+ payload = Qs.parse(request.url)
+
+ @handle payload.Body, payload.From if payload.Body and payload.From
+
+ response.writeHead 200, 'Content-Type': 'text/plain'
+ response.end()
+
+ server.listen process.env.HUBOT_SMS_PORT, "0.0.0.0"
+
+ handle: (body, from) ->
+ return if body.length == 0
+ user = @userForId from
+ @receive new Robot.Message user, body
+
+ post: (message, to, callback) ->
+ host = "api.twilio.com"
+ path = "/2010-04-01/Accounts/#{@config.sid}/SMS/Messages.json"
+
+ auth = new Buffer(@config.sid + ':' + @config.token).toString("base64")
+
+ headers =
+ 'Authorization': "Basic " + auth
+ 'Content-Type': "application/x-www-form-urlencoded"
+ 'Host': host
+
+ params = Qs.stringify
+ 'From': @config.from
+ 'To': to
+ 'Body': message
+
+ headers['Content-Length'] = params.length
+
+ opts =
+ host: host
+ port: 443
+ method: "POST"
+ path: path
+ headers: headers
+
+ console.log opts
+
+ request = Https.request opts, (response) ->
+ data = ""
+
+ response.setEncoding "utf8"
+
+ response.on "data", (chunk) ->
+ data += chunk
+
+ response.on "end", ->
+ body = JSON.parse data
+
+ if response.statusCode is 201
+ callback null, body
+ else
+ callback body.message
+
+ request.write params
+
+ request.end()
+
+exports.Twilio = Twilio
View
13 src/templates/README.md
@@ -84,6 +84,19 @@ IRC Variables
% heroku config:add HUBOT_IRC_NICK="MICCHECK1212"
+Twilio Variables
+------------------
+
+You must have a Twilio account with credit and a number that can send and
+receive SMS messages.
+
+ % heroku config:add HUBOT_SMS_FROM="+14156662671"
+
+ % heroku config:add HUBOT_SMS_SID="AC5d10e5624da757326d12f8d31c08c20b"
+
+ % heroku config:add HUBOT_SMS_TOKEN="4ada63e18146a204e468fb6289030231"
+
+
Restart the bot
---------------
You may want to get comfortable with `heroku logs` and `heroku restart`
Please sign in to comment.
Something went wrong with that request. Please try again.