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

Hubot

 Hubot

A customizable life embetterment robot.

Eugene Oskin

January 20, 2018
Tweet

More Decks by Eugene Oskin

Other Decks in Programming

Transcript

  1. TOC • Installation • Hear & Reply • HTTP call

    & HTTP API • Brain • Documentation 2
  2. Scripts (/scripts/*.{coffee,js}) # Description: # Tell people hubot's new name

    if they use the old one # # Commands: # None # module.exports = (robot) -> robot.hear /^hubot:? (.+)/i, (res) -> response = "Sorry, I'm a diva and only respond to #{robot.name}" response += " or #{robot.alias}" if robot.alias res.reply response return 6
  3. module.exports = (robot) -> robot.hear /badger/i, (res) -> # Will

    be called when hear any ‘badger’ word res.send "Badgers? BADGERS? WE DON'T NEED NO STINKIN BADGERS" # Send a message back to the room robot.respond /open the pod bay doors/i, (res) -> # Will be called when bot was mentioned, ie ‘@hubot: open the pod bay doors’ res.reply "I'm afraid I can't let you do that." # Reply to the person that sent the message Respond and hear 7
  4. Http call module.exports = (robot) -> robot.hear /badger/i, (res) ->

    robot.http("https://midnight-train") .get() (err, res, body) -> # your code here 8
  5. Http API module.exports = (robot) -> # the expected value

    of :room is going to vary by adapter, it might be a numeric id, name, token, or some other value robot.router.post '/hubot/chatsecrets/:room', (req, res) -> room = req.params.room data = if req.body.payload? then JSON.parse req.body.payload else req.body secret = data.secret robot.messageRoom room, "I have a secret: #{secret}" res.send 'OK' 9
  6. Brain (aka key-value storage) robot.respond /have a soda/i, (res) ->

    # Get number of sodas had (coerced to a number). sodasHad = robot.brain.get('totalSodas') * 1 or 0 if sodasHad > 4 res.reply "I'm too fizzy.." else res.reply 'Sure!' robot.brain.set 'totalSodas', sodasHad+1 robot.respond /sleep it off/i, (res) -> robot.brain.set 'totalSodas', 0 msg.reply 'zzzzz' 10
  7. Scheduling module.exports = (robot) -> annoyIntervalId = null robot.respond /annoy

    me/, (res) -> if annoyIntervalId res.send "AAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIHHHHHHHHHH" return res.send "Hey, want to hear the most annoying sound in the world?" annoyIntervalId = setInterval () -> res.send "AAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIHHHHHHHHHH" , 1000 robot.respond /unannoy me/, (res) -> if annoyIntervalId res.send "GUYS, GUYS, GUYS!" clearInterval(annoyIntervalId) -> annoyIntervalId = null else res.send "Not annoying you right now, am I?" 11
  8. Documentation # Description: # <description of the scripts functionality> #

    # Dependencies: # "<module name>": "<module version>" # # Configuration: # LIST_OF_ENV_VARS_TO_SET # # Commands: # hubot <trigger> - <what the respond trigger does> # <trigger> - <what the hear trigger does> # # Notes: # <optional notes required for the script> # # Author: # <github username of the original script author> 12
  9. Summary • Hear & Reply • HTTP call & HTTP

    API • Brain • Scheduling • Documentation 13