ChatOps:Hubot本地開發環境搭建
Hubot是Github開發並開源的chatbot,將其部署到運維環境中後,可通過編寫自定義的指令碼,將之前的web端、shell命令等人工進行的運維工作,通過聊天工具交給機器人來代替。
搭建hubot本地開發環境
安裝Node.js:
下載Node.js LTS的Binary版本:https://nodejs.org/en/download/current/。將binary壓縮包解壓縮到安裝目錄NODE_INSTALL_PATH
中。 設定環境變數NODE_HOME
指向到解壓縮後的目錄,並設定PATH環境變數包含NODE_HOME
。
在NODE_INSTALL_PATH
中建立一個npm_cache
目錄,使用下面的命令將npm_cache
的路徑變更到這個目錄中:
npm config set cache NODE_INSTALL_PATH/npm_cache --global
node -v v10.14.1 npm -v 6.4.1
使用npm安裝hubot generator生成我們自己本地的hubot:
npm install -g yo generator-hubot
mkdir myhubot cd myhubot yo hubot
接下來會是互動的初始化hubot,這裡略過。
Hubot需要使用redis來持久化資料,這裡是本地開發環境,編輯external-scripts.json
刪除hubot-redis-brain
,在本地不使用redis。
刪除external-scripts.json
中的hubot-heroku-keepalive
。
執行bin/hubot
進入:
bin/hubot wing> help usage: history exit, \q - close shell and exit help, \? - print this usage clear, \c - clear the terminal screenhu
編寫第一個hubot指令碼
hubot使用CoffeScript開發自定義機器人指令碼,先檢視一下本地myhubot的目錄結構:
bin/#hubot執行指令碼 node_modules/#引用的包檔案 scripts/#自定義機器人指令碼 external-scripts.json#引用的外部指令碼 package.json#專案全域性配置資訊
module.exports = (robot) -> robot.hear /hello/i, (res) -> res.send "hello everyone." robot.respond /hi/i, (res) -> res.reply "hi everyone."
hear和respond的區別,hear監聽任何訊息,respond只監聽群組中傳送給機器人的訊息,即需要指定機器人名稱,參見下面的觸發指令碼的方式。 send和reply的區別,send會將訊息傳送到群組中,而reply會將訊息回覆給具體的人,如@
。
wing> hello wing> hello everyone. wing> wing hi wing> hi everyone.
參考
- ofollow,noindex" target="_blank">HUBOT DOCUMENTATION
- CoffeeScript