Hubot 的簡單用法

簡介

Hubot 是 Github 的開源聊天機器人,能夠用來作一些自動化任務,如部署網站,翻譯語言等等。node

你可能會說,這些只要寫個腳本就能夠作到了吧?git

確實,但你寫完腳本以後仍是須要手動運行那些腳本。github

你有沒想過其實你能夠在經常使用的聊天軟件上說 @xxx, 部署新版本的網站,而後機器人就自動登陸服務器,而後執行部署腳本,部署成功後告訴你 新版本的網站已經部署成功shell

是的,若是你的聊天軟件上集成了 Hubot,你就能夠輕鬆地用它來管理一些繁瑣的事情啦!npm

安裝

官方推薦咱們用 yeoman + hubot 生成器來生成咱們的聊天機器人,方法以下:api

$ npm install -g yo generator-hubot
$ mkdir myhubot && cd myhubot
$ yo hubot

回答一些基本的問題後,咱們的聊天機器人就生成好啦~服務器

基本用法

咱們的聊天機器人的執行文件是 bin/hubot,咱們先看看裏面寫什麼:dom

$ cat ./bin/hubot

#!/bin/sh

set -e

npm install
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"

exec node_modules/.bin/hubot --name "myhubot" "$@"

這份執行文件只是先執行 npm install,而後設置環境變量,再執行 node_modules/.bin/hubot 而已,沒什麼神祕的。函數

咱們試試運行一下這份可執行文件:測試

$ ./bin/hubot
myhubot>

咱們看到了一個相似 shell 的東東!試試隨便輸入一些東西:

myhubot> hello
myhubot> world
myhubot> how are you?
myhubot> can you hear me?

咱們發現不管咱們輸入什麼,咱們的機器人都沒有反應,是否是壞掉了?
其實並非這樣的,它沒反應是由於咱們沒有對「輸入」的處理,若是咱們輸入一些特定的「輸入」,它就會有反應啦!

myhubot> myhubot ping
myhubot> PONG

myhubot> myhubot pug me
myhubot> http://28.media.tumblr.com/tumblr_locinzasB91qzj3syo1_500.jpg

myhubot> myhubot help
myhubot> myhubot adapter - Reply with the adapter
myhubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
myhubot echo <text> - Reply back with <text>
myhubot help - Displays all of the help commands that Hubot knows about.
myhubot help <query> - Displays all help commands that match <query>.
myhubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
myhubot map me <query> - Returns a map view of the area returned by `query`.
myhubot mustache me <url|query> - Adds a mustache to the specified URL or query result.
myhubot ping - Reply with pong
myhubot pug bomb N - get N pugs
myhubot pug me - Receive a pug
myhubot the rules - Make sure hubot still knows the rules.
myhubot time - Reply with current time
myhubot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
myhubot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
ship it - Display a motivation squirrel

看到了吧!若是咱們輸入了特定的「輸入」,機器人就會有反應啦!

當咱們輸入 myhubot help 的時候,返回的東東其實就是預約義的「輸入」,這些預約義的「輸入」只在 shell adapter 下有效哦!

Adapter

什麼是 shell adapter ? 咱們運行 ./bin/hubot 時默認的 adapter 就是 shell adapter。

什麼是 adapter ? 所謂的 adapter 實際上是一些讓機器人接收輸入的接口。

剛纔提到,shell adapter 是默認狀況下的 adapter,主要是用來測試 adapter 是否生效。說白了,其實就是沒什麼用!

以爲很坑爹是吧?說好的讓咱們的聊天軟件整合咱們的機器人呢?

實際上社區已經爲咱們提供了各類各樣的 adapter,咱們只要下載就能夠用啦!具體請看看 https://hubot.github.com/docs/adapters/

那麼咱們如何指定用某個 adapter 呢?很簡單啦,只要啓動機器人的時候帶上 -a 參數就行了。
譬如若是咱們想讓機器人整合到 telegram,咱們只要執行下面的命令就能夠了:

$ npm install --save hubot-telegram
$ ./bin/hubot -a telegram

固然咱們還須要設置一下,這些設置會根據不一樣的 adapter 而有所不一樣,具體請看對應的文檔!

若是你所用的聊天軟件並不在社區的支持列表中,又想把整合 Hubot 的話,能夠本身寫 adapter,文檔在這裏:https://hubot.github.com/docs/adapters/d...

Scripts

咱們一直說 Hubot 是聊天機器人,機器人最基本的是根據不一樣的「輸入」給出不一樣的「輸出」。
在 Hubot 應該怎麼處理不一樣「輸入」,給出不一樣的「輸出」呢?
答案就是用 Scripts 啦!

有沒有發現咱們機器人的目錄下有個 scripts/ 文件夾?咱們能夠在這個文件夾下添加各類腳本文件,根據不一樣的「輸入」給出不一樣的「輸出」。
在咱們啓動 Hubot 的時候,它會加載 scripts/ 文件夾下的腳本,賦予 Hubot 強大的交互能力!

須要注意的是,scripts/ 下的腳本必須是 .coffee 或者 .js 格式的,並且必須暴露一個接受 robot 參數的函數!
咱們仍是先打開 scripts/example.coffee 看看吧!

// coffee
module.exports = (robot) ->

// js
module.exports = function(robot) {}

在這個函數裏面,咱們能夠利用 robot.hearrobot.responserobot.sendrobot.reply 等 api 爲不一樣的「輸入」給出不一樣的「輸出」!
咱們還能夠用 robot.http(url).get() 等方法來發出 http 請求!這樣咱們的機器人就能夠有更強大的交互能力了!

想知道更多 api 的用法的話,能夠參考文檔:https://hubot.github.com/docs/scripting/

寫在最後

Hubot 真的是一個簡單易用的聊天機器人,咱們能夠把它整合到咱們的聊天軟件中,讓那些簡單但繁瑣的任務自動化起來,提升咱們的工做效率!
最後強烈推薦各位同窗去讀一下 Hubot 的源碼,簡單易懂,以後會對 Hubot 有更深入的認識!

出處

http://scarletsky.github.io/2016/04/03/h...

參考資料

https://hubot.github.com/

相關文章
相關標籤/搜索