hubot: a look inside our robot friend

Post on 28-May-2015

1.984 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

An overview of Hubot, describing setup and writing scripts.

TRANSCRIPT

HUBOTA LOOK INSIDE OUR ROBOT FRIEND

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

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

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

FIND A NICE HOMEcd to directory of choice

run hubot -c airbot to generate a boilerplatecd airbot

chmod +x bin/hubot

HELLO WORLDrun bin/hubot

type hubot ping and hit enter

A BRIEF DIVERSION INTOCOFFEESCRIPT

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

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)

A HUBOT SCRIPTEXAMPLE

CREATE AND EDITSCRIPTS/GOODBYE.COFFEE

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)

run bin/hubot againsay goodbye Hubotsay later, Hubot

HUBOT-SCRIPTSGITHUB.COM/GITHUB/HUBOT-SCRIPTS

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

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

edit hubot-scripts.jsonadd "clark.coffee"

run bin/hubotsay hubot clark 1 2 3 4 5

HUBOT PERSISTANCEsimple storage through hubot.brain

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

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'

HUBOT HTTP LISTENERCREATE AND EDIT

SCRIPTS/SAY.COFFEE

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}"

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

run npm installrun bin/hubot

visit localhost:8080/say?message=hello

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)

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"

top related