本地運行Hubot
Hubot
Hubot是Github開發并開源的chatbot,但它并不僅僅是一個聊天機器人,Hubot已經廣泛應用于Github的日常運維工作,被稱為最忙碌的員工。
安裝Hubot
Hubot是基于node.js、npm技術體系,使用CoffeeScript語言開發的開源chatbot,github地址:https://github.com/github/hubot
安裝 node.js & npm
直接去官網下載安裝:https://nodejs.org/
安裝Hubot生成器
要運行自己的Hubot,需要通過生成器生成,首先安裝generator-hubot
npm install -g yo generator-hubot
生成自己的hubot
mkdir myhubot
cd myhubot
yo hubot
啟動myhubot
windows下直接執行hubot命令即可
bin\hubot.cmd
控制臺應該可以看到如下信息:
loadDep:basic-auth ▌
loadDep:base64-url ▌
loadDep:debug ▌
loadAllDepsIntoIdealTree ?
build:. → linkMans ▄
runTopLevelLifecycles ?
myhubot> [Mon May 16 2016 14:55:09 GMT+0800 (中國標準時間)] WARNING Loading scripts from hubot-scripts.json is deprecated and will be remove
d in 3.0 (https://github.com/github/hubot-scripts/issues/1113) in favor of packages for each script.
Your hubot-scripts.json is empty, so you just need to remove it.
[Mon May 16 2016 14:55:09 GMT+0800 (中國標準時間)] ERROR hubot-heroku-alive included, but missing HUBOT_HEROKU_KEEPALIVE_URL. `heroku config
:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s | grep web-url | cut -d= -f2)`
[Mon May 16 2016 14:55:10 GMT+0800 (中國標準時間)] INFO hubot-redis-brain: Using default redis on localhost:6379
myhubot>
出現的錯誤信息是因為默認情況下,hubot使用redis做持久化存儲,并支持heroku部署。
為了方便起見,我們可以去掉redis和heroku配置,在./external-scripts.json文件中找到"hubot-heroku-keepalive"和"hubot-redis-brain"并刪除即可。
也可以忽略這些錯誤信息,繼續使用hubot。
如何使用myhubot
可以通過help查看hubot支持的指令
myhubot help
myhubot> Shell: 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> myhubot pug me
myhubot> http://26.media.tumblr.com/tumblr_lteebc0WeC1qb08qmo1_500.jpg
至此,本地的myhubot已經可以運行起來,下一步就是擴展myhubot,編寫自定義scripts來支持自定義指令。
開發自定義機器人腳本
首先,在決定開發自定義腳本時,可以使用npm search命令看看是不是有類似的腳本可以使用。
$ npm search hubot-scripts github
NAME DESCRIPTION
hubot-deployer Giving Hubot the ability to deploy GitHub repos to PaaS providers hubot hubot-scripts hubot-gith
hubot-gh-release-pr A hubot script to create GitHub's PR for release
hubot-github Giving Hubot the ability to be a vital member of your github organization
…
應用NPM 包:
- npm install --save <package-name>
- 將package-name添加到external-scripts.json
第一個script
myhubot 項目結構
- bin/ myhubot運行腳本
- node_modules/ 引用的包文件
- scripts/ 存放自定義腳本
- external-scripts.json 引用的外部腳本
- package.json 項目全局配置信息
我們編寫的自定義腳本要放在scripts中,可以是.coffee或.js文件,你可以用CoffeeScript或純JS編寫他們。
默認情況下,需要導出一個function:
module.exports = (robot) ->
# your code here
首先我們在scripts中創建第一個腳本,hello.coffee
module.exports = (robot) ->
robot.respond /greet/i, (res) ->
res.send 'hello world.'
重啟myhubot,輸入myhubot greet
myhubot> myhubot greet
myhubot> hello world.
Hubot script語法
Hearing & responding
hearing可以監聽房間或群組中任何消息。
respond只監聽直接發送給機器人的消息,也就是需要指定機器人名稱或別名,加入機器人名稱是rob,別名是/,則如下格式會觸發腳本:
- rob open the pod bay doors
- ROB: open the pod bay doors
- @ROB open the pod bay doors
- /open the pod bay doors
Send & reply
res參數是Response實例,如果要從機器人返回消息,可以使用send或reply。
send會將消息發送到整個聊天房間或群組。
reply會將消息回復給具體的人。
更多信息請參考官網 https://hubot.github.com/docs/scripting/
復雜一點的例子
Hubot是基于node.js開發的,所以它可以做到node.js所能做的,如:
- 發消息
- 回復消息
- 發起http請求
- 返回http響應
- 在遠程服務器上執行shell命令
- ...
例子: 查看服務端日歷信息
scripts/calculate.coffee
child_process = require('child_process')
module.exports = (robot) ->
robot.respond /(cal|日歷)( me)?/i, (res) ->
child_process.exec 'cal', (err, stdout, stderr) ->
res.send(stdout)
重啟myhubot,輸入myhubot cal,就可以看到控制臺返回服務端日歷信息
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
捕獲所有未處理信息
為了使機器人響應更加友好,對于未能識別的信息,可以設置一個默認返回信息。
scripts/catchAll.coffee
module.exports = (robot) ->
robot.catchAll (res) ->
res.send "主人,無法識別您的指令:#{res.message.text}"
集成到微信
hubot默認提供兩種adapter,shell和campfile。
shell提供命令行方式,用于開發調試或者運維還可以,用于聊天太遜了。
campfile聊天工具在中國都沒幾個人聽過,不用也罷。
依賴于hubot的靈活擴展機制,社區提供了多種聊天工具的集成adapter。可以在官網上找到。
不過在中國,最流行的聊天軟件當然是微信,將自己的聊天機器人綁定到微信賬戶上,沒事調戲下,是不是很有趣。
好在,已經有人做出了微信adapter,地址:https://github.com/KasperDeng/Hubot-WeChat
接下來,我們集成Hub-WeChat到我們的工程中來即可。
npm install hubot-weixin --save
運行myhubot,設置名稱為bot,別名/,這樣只需要輸入/cal就可以激活機器人了。
bin\hubot.cmd -n bot -l / -a weixin
這樣運行命令當然會出錯,hubot-weixin工作的主要機制是hack網頁版微信協議,先用手機登錄微信帳號,然后模擬網頁版微信登錄,這樣就可以接受微信消息了。
可以按照如下步驟設置:
1.首先為機器人注冊一個新的微信號或者使用已有的微信號;
2.在手機端登錄微信;
3.在chrome瀏覽器打開網頁微信:web.weixin.qq.com,F12打開控制臺監控請求信息;
4.用手機掃描登錄;
5.控制臺應該會監控到很多請求信息;
6.從這些請求信息中找出如下信息,并填寫到./node_modules/hubot-weixin/config.yaml:
cookie:
Uin:
Sid:
Skey:
DeviceID:
這下,重新運行命令就可以了,如果有錯誤,就按照剛才的步驟檢查這些參數設置是否正確,也可以用postman等工具調試看看。
bin\hubot.cmd -n bot -l / -a weixin
正確啟動后,將機器人微信號添加為好友,就可以通過微信發消息給機器人了。
接下來
接下來,可以考慮做些更好玩的事情。
- 比如綁定語音服務,將語音轉換成文字指令。這樣就可以通過語音指揮機器人做事情了。
- 編寫部署運維腳本,使機器人可以真正用于日常的開發運維工作。也就是所謂的chatops。