+ All Categories
Home > Technology > Hubot: a look inside our robot friend

Hubot: a look inside our robot friend

Date post: 28-May-2015
Category:
Upload: ajacksified
View: 1,980 times
Download: 1 times
Share this document with a friend
Description:
An overview of Hubot, describing setup and writing scripts.
Popular Tags:
27
Transcript
Page 1: Hubot: a look inside our robot friend
Page 2: Hubot: a look inside our robot friend

HUBOTA LOOK INSIDE OUR ROBOT FRIEND

Page 3: Hubot: a look inside our robot friend

1. Setting Up Hubot2. Coffeescript Primer3. Hubot Overview4. APIs

Page 4: Hubot: a look inside our robot friend
Page 5: Hubot: a look inside our robot friend

(disclaimer: this assumes OSX. commands shouldtranslate easily to windows and linux.)

Page 6: Hubot: a look inside our robot friend

PREREQUISITESInstall Node

brew install node

Install and run Redisadd /usr/local/share/npm/bin to your path

Install Hubotnpm install -g coffee-script

npm install -g hubot

An account is suggested.Heroku

Page 7: Hubot: a look inside our robot friend

FIND A NICE HOMEcd to directory of choice

run hubot -c airbot to generate a boilerplatecd airbot

chmod +x bin/hubot

Page 8: Hubot: a look inside our robot friend

HELLO WORLDrun bin/hubot

type hubot ping and hit enter

Page 9: Hubot: a look inside our robot friend

A BRIEF DIVERSION INTOCOFFEESCRIPT

Page 10: Hubot: a look inside our robot friend

introMessage = (user) -> if user?.name? and user?.hobby? "hello, I am #{response.name}, " + "a #{response.hobby}ist." else message = "Please tell me more about yourself."

message

me = name: "Jack" hobby: "Hubot curation"

console.log introMessage(me)

#hello, I am Jack, a Hubot curationist

Page 11: Hubot: a look inside our robot friend

HUBOT STRUCTUREProcfile (Heroku startup script)README.mdbin/ (contains hubot executable)external-scripts.json (list of packages from npm)hubot-scripts.json (list of packages from hubot-scripts)package.json (node package managermetainformation)scripts/ (custom hubot script directory)

Page 12: Hubot: a look inside our robot friend

A HUBOT SCRIPTEXAMPLE

CREATE AND EDITSCRIPTS/GOODBYE.COFFEE

Page 13: Hubot: a look inside our robot friend

goodbyes = [ "Bye, {name}.", "Later, {name}.", "Take care, {name}."]

goodbye = (name) -> index = parseInt(Math.random() * goodbyes.length) message = goodbyes[index] message.replace(/{name}/, name);

module.exports = (robot) -> robot.hear /(bye|later),?\s(.*)/i, (msg) -> if robot.name.toLowerCase() == msg.match[2].toLowerCase() byeMessage = goodbye(msg.message.user.name) msg.send(byeMessage)

Page 14: Hubot: a look inside our robot friend
Page 15: Hubot: a look inside our robot friend

run bin/hubot againsay goodbye Hubotsay later, Hubot

Page 16: Hubot: a look inside our robot friend

HUBOT-SCRIPTSGITHUB.COM/GITHUB/HUBOT-SCRIPTS

hubot will automatically download and keep up-to-dateany hubot-scripts you add

Page 17: Hubot: a look inside our robot friend

copy the dependencies from the hubot-script into yourpackage.json

from :clark.coffee# Description:# None## Dependencies:# "clark": "0.0.5"## Configuration:# None## Commands:# hubot clark <data> - build sparklines out of data## Author:# ajacksified

Page 18: Hubot: a look inside our robot friend

edit hubot-scripts.jsonadd "clark.coffee"

run bin/hubotsay hubot clark 1 2 3 4 5

Page 19: Hubot: a look inside our robot friend
Page 20: Hubot: a look inside our robot friend

HUBOT PERSISTANCEsimple storage through hubot.brain

overloaded by redis-brain, mongo-brain, etc.(you can find these in hubot-scripts or write your own)

Page 21: Hubot: a look inside our robot friend

You can save any arbitrary data in the brain.# you may want to wait until the brain has been initialized# and there is a database connectionrobot.brain.on 'loaded', -> robot.brain.lastAccessed = new Date() robot.brain.seagulls = 12 robot.brain.flowers = { pansies: true, daffodils: false }

# hubot brain runs on events robot.brain.emit 'save'

Page 22: Hubot: a look inside our robot friend

HUBOT HTTP LISTENERCREATE AND EDIT

SCRIPTS/SAY.COFFEE

Page 23: Hubot: a look inside our robot friend

querystring = require('querystring')

module.exports = (robot) -> robot.router.get "/hubot/say", (req, res) -> query = querystring.parse(req._parsedUrl.query) message = query.message

user = {} user.room = query.room if query.room

robot.send(user, message) res.end "said #{message}"

Page 24: Hubot: a look inside our robot friend

edit package.json to include"querystring": ">= 0.1.0" in the dependencies

run npm installrun bin/hubot

visit localhost:8080/say?message=hello

Page 25: Hubot: a look inside our robot friend

DEPLOYMENT99 times out of 100, you'll probably just deploy to Heroku

hubot -c creates a Heroku Procfile for yourun Heroku create

deploy with git push heroku masterstart with heroku ps:scale web=1

(you'll only have to run ps:scale this the first time)

Page 26: Hubot: a look inside our robot friend

CHAT ADAPTERSHubot ships with a Campfire adapter. You can get more

from hubot-scripts.Assuming Heroku deployment, run

Heroku config:addHUBOT_CAMPFIRE_TOKEN=secretHUBOT_CAMPFIRE_ROOMS=123,456HUBOT_CAMPFIRE_ACCOUNT="hubot"

Page 27: Hubot: a look inside our robot friend

Recommended