Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HUBOT img me woman laughing alone with salad

HUBOT img me woman laughing alone with salad

Having your very own robot butler is finally possible! HUBOT from GitHub is an easy to setup and highly extensible automation assistant for your everyday needs. Use HUBOT to order food, grab directions, deploy code, and even open the door! Sure, he may take over the world someday, but in the meantime we might as well have some fun.

Jonathan Hoyt

April 20, 2012
Tweet

More Decks by Jonathan Hoyt

Other Decks in Technology

Transcript

  1. module.exports = (robot) -> robot.respond /good morning/i, (msg) -> msg.send

    "Good morning to you." jonmagic> hubot good morning hubot> Good morning to you. Respond & Send
  2. module.exports = (robot) -> robot.hear /gasoline/i, (msg) -> msg.reply "Here

    we call it petrol." jonmagic> Jon, did you get gasoline yet? hubot> Hoyt: Here we call it petrol. Hear & Reply
  3. module.exports = (robot) -> robot.respond /good morning/i, (msg) -> msg.send

    "Good morning to you." common.js module specification
  4. # Public: Loads a file in path # # path

    - A String path on the filesystem. # file - A String filename in path on the filesystem. # # Returns nothing. loadFile: (path, file) -> ext = Path.extname file full = Path.join path, Path.basename(file, ext) if ext is '.coffee' or ext is '.js' try require(full) @ @parseHelp "#{path}/#{file}" catch err @logger.error "#{err}" src/robot.coffee
  5. Options = path: "." name: "Hubot" create: false adapter: "shell"

    alias: false enableHttpd: true ... robot = Hubot.loadBot options... ... robot.run ~/hubot-demo $ ./bin/hubot --name Jeeves ./bin/hubot
  6. class Robot hear: (regex, callback) -> ... respond: (regex, callback)

    -> ... send: (user, strings...) -> ... reply: (user, strings...) -> ... http: (url) -> ... src/robot.coffee
  7. hear: (regex, callback) -> @listeners.push new TextListener(@, regex, callback) class

    TextListener extends Listener constructor: (@robot, @regex, @callback) -> @matcher = (message) => if message instanceof Robot.TextMessage message.match @regex class Listener call: (message) -> if match = @matcher message @callback new @robot.Response(@robot, message, match) true else false src/robot.coffee
  8. respond: (regex, callback) -> re = regex.toString().split("/") re.shift() # remove

    empty first item modifiers = re.pop() # pop off modifiers ... pattern = re.join("/") # combine the pattern back again if @alias alias = @alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") newRegex = new RegExp("^(?:#{alias}[:,]?|#{@name}[:,]?) ... ", mod...) else newRegex = new RegExp("^#{@name}[:,]?\\s*(?:#{pattern})", mod...) @logger.debug newRegex.toString() @listeners.push new TextListener(@, newRegex, callback) src/robot.coffee
  9. send: (strings...) -> @robot.adapter.send @message.user, strings... send: (user, strings...) ->

    if strings.length > 0 @bot.Room(user.room).speak strings.shift(), (err, data) => @robot.logger.error "Campfire error: #{err}" if err? @send user, strings... src/robot.coffee src/adapters/campfire.coffee
  10. reply: (strings...) -> @robot.adapter.reply @message.user, strings... reply: (user, strings...) ->

    @send user, strings.map((str) -> "#{user.name}: #{str}")... src/robot.coffee src/adapters/campfire.coffee
  11. http: (url) -> @httpClient.create(url) class ScopedClient constructor: (url, options) ->

    @options = @buildOptions url, options exports.create = (url, options) -> new ScopedClient url, options src/robot.coffee node-scoped-http-client https://github.com/technoweenie/node-scoped-http-client
  12. module.exports = (robot) -> robot.respond /(image|img)( me)? (.*)/i, (msg) ->

    imageMe msg, msg.match[3], (url) -> msg.send url imageMe = (msg, query, cb) -> msg.http('http://ajax.googleapis.com/ajax/...') .query(v: "1.0", rsz: '8', q: query) .get() (err, res, body) -> images = JSON.parse(body) images = images.responseData.results if images.length > 0 image = msg.random images cb "#{image.unescapedUrl}#.png" google-images.coffee
  13. module.exports = (robot) -> robot.hear /check domain (.*)/i, (msg) ->

    domain = escape(msg.match[1]) user = process.env.DNSIMPLE_USERNAME pass = process.env.DNSIMPLE_PASSWORD auth = 'Basic ' + new Buffer(user + ':' + pass).toString('base64'); msg.http("https://dnsimple.com/domains/#{domain}/check") .headers(Authorization: auth, Accept: 'application/json') .get() (err, res, body) -> switch res.statusCode when 200 msg.send "Sorry, #{domain} is not available." when 404 msg.send "Cybersquat that shit!" when 401 msg.send "You need to authenticate by ..." dnsimple.coffee
  14. module.exports = (robot) -> robot.respond /salesforce query (.*)$/i, (msg) ->

    query = msg.match[1] msg.http(auth_url).post() (err, res, body) -> oath_token = JSON.parse(body).access_token query = encodeURIComponent(query) msg.http(query_url + query) .headers(Authorization: "OAuth #{oath_token}") .get() (err, res, body) -> if err msg.send "Salesforce says: #{err}" return salesforce.coffee
  15. sys = require('sys') exec = require('child_process').exec; express = require 'express'

    app = module.exports = express.createServer() app.get "/door", (req, res) -> # unlock the door exec "echo 38 > /sys/class/gpio/export" exec "echo out > /sys/class/gpio/gpio38/direction" exec "echo 1 > /sys/class/gpio/gpio38/value" sleep 2000 # lock the door and cleanup exec "echo 1 > /sys/class/gpio/gpio38/value" exec "echo 38 > /sys/class/gpio/unexport" res.send "Unlocked the door for you :)" Unlocking a door becomes easy
  16. Robot Butler - do our bidding Internal Logic - easy

    to grok External Services - change the world Other Resources - http://hubot-factory.herokuapp.com - https://github.com/tombell/learnhubotthehardway