Last Updated: July 27, 2016
· jsolis

hubot http listener

If you have a hubot running, making your life better, it can be useful to have him listening for incoming message from outside sources. You can have hubot run an HTTP listener and define routes that trigger custom actions (much like routes in an express node app or a Backbone app).

The first thing you have to do is make sure your hubot is listening for HTTP requests. To do so, simply define the PORT env variable in your bin/hubot config to whatever port you want.

export PORT=8080

Now you have to create a coffee script in your hubot's script directory that listens for routes, or URL paths, and then does something for you. Here is a pretty generic example of a script that gets triggered when "/flybot/message" is hit and passes a message along to a user or channel. It looks for two query string parameters, the user or channel to relay the message to, and the message itself.

url = require('url')
querystring = require('querystring')

module.exports = (robot) ->

  robot.router.get "/flybot/message", (req, res) ->
    query = querystring.parse(url.parse(req.url).query)

    user = {} = if

       robot.send user, "INCOMING MESSAGE: " + query.message

       res.end "message sent: #{query.message}"

    catch error
      console.log "message-listner error: #{error}."
  • robot.router.get "/flybot/message" is what tells hubot to listen for GET requests to "/flybot/message". You can also listen for POSTs via robot.router.get and the POST payload is available in req.body.payload

  • robot.send sends the message to the User object (which can be a user or a channel). For a user you can use something like "jsolis" and for a channel you would have to send something like "#mychannel" (remember to URLEncode the # in your querystring %23mychannel).

  • res.end will end your response and send the string parameter back to caller.

Say Thanks

1 Response
Add your response


It would be great if this were fleshed out a little. I see 2700 views... which are probably all newbies or they would not need this. I am just such a newbie.

But this is frustrating because it gets you close, but there are lots of little subtle open ends. If someone has already become familiar with hubot/coffee script they would be simple... but while you are first experimenting with it these little things mean everything.

Can you please include:

  • a pretty literal example of what the entire url should look like

  • how to handle common gotchas such as cannot query

can you please tighten up things like:

"for a channel you would have to send something like..."

The value of this awesome post is that it can help onboard noobs. But the above items are working against that. To recap: please provide specifics. If you have a friend who does not use coffeescript... watch them trying to work with coffeescript after reading your article. Documenting their trouble spots would be awesome.


over 1 year ago ·