Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Twilio adapter #29

Merged
merged 2 commits into from

2 participants

@tombell

Ok, this is the quickly hacked together Twilio adapter for Hubot. The environment variables are outlined in the templates README.

It's buggy in the sense that Twilio can't seem to send messages > 160 chars, I'm planning on working around this. Also it cannot seem to send SMS internationally (outside US/Canada?).

Any feedback/improvements are welcome. :)

I don't actually have an iPhone 4S, so any Siri testers would awesome.

@atmos atmos merged commit d7d2a21 into from
@atmos

Hey Tom,

Thanks a lot. We'd discussed this idea already, glad to see someone hammer it out. I don't have a 4S either so I'll get someone else to test/verify it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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`
Something went wrong with that request. Please try again.