Hubot 聊天機器人串 Slack,並使用 GitLab CICD 自動佈署及啟動
先到自己的 Slack 安裝 Hubot App
https://hsuweni.slack.com/apps
給它一個名字
會得到一個 API Key
yeoman 是一個整合工具包 SCAFFOLDING TOOL 腳手架工具
全域安裝
npm install -g yo generator-hubot
一直無法成功…
算了,睡覺去….
結果一覺醒來,重新嘗試就可以安裝了
不過我習慣電腦不關機,但會把程式關閉,所以有可能是 terminal 重啟的關係
建立一個 catbot 專案
進專案資料夾
yo hubot --adapter=slack
使用在 Slack 網站申請的 API Key 執行以下指令
HUBOT\_SLACK\_TOKEN=xoxb-1299715080640-1559432386100-xxxxxxxxxx ./bin/hubot --adapter slack
要輸入關鍵字 help
才有效
簡單測試 ping,有回應 PONG 就是成功
提示有說明 image me 是請 catbot 送一張圖片過來,但也提示要你自己設定自訂的搜尋引擎,預設的 Google 不一定能一直提供
也可以叫聊天機器人給我地圖
另外一個預設指令 pug me
因為 Cabot
預設是使用 coffee script
照著搜尋到的教學加入一個 configcat.coffee
檔案
輸入以下
無效….
有可能是它引用的 API 位置已經掛掉了
不要用 Coffee Script,用 ES6 語法來寫
https://keestalkstech.com/2018/01/hubot-es6-promises/
一樣失敗…
有提示是說找不到 decode-html 這個模組
在專案中使用 npm 安裝
npm install decode-html
輸入關鍵字「norris」,有回應了,隨機返回一個笑話
把它搬到自己的 Raspberry Pi 4 主機上執行
HUBOT\_SLACK\_TOKEN=xoxb-1299715080640-1559432386100-xxxxxxxxxx ./bin/hubot --adapter slack
除了在專案中建立佈署主機的 SSH Key 外
也要建立一開始的 Slack Hubot Key
注意⚠️
因為佈署是透過 GitLab 的 CICD,所以要注意啟動機器人的語法
before_script:
- apt-get update -qq
- apt-get install -qq git
- 'which ssh-agent || ( apt-get install -qq openssh-client )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$PI4\_SSH\_PRIVATE\_KEY")
- mkdir -p ~/.ssh
- '\[\[ -f /.dockerenv \]\] && echo -e "Host \*\\n\\tStrictHostKeyChecking no\\n\\n" > ~/.ssh/config'
deploy_staging:
type: deploy
environment:
name: staging
url: http://220.132.xxx.xxx
script:
- ssh [email protected] -p10022 "cd /home/ben/projects/web &&
\\rm -R catbot/ && mkdir catbot && exit"
- ssh [email protected] -p10022 "cd /home/ben/projects/web/catbot &&
git clone [email protected]:dkben/catbot.git . && npm install && exit"
- ssh [email protected] -p10022 "cd /home/ben/projects/web/catbot &&
eval nohup HUBOT\_SLACK\_TOKEN=$HUBOT\_SLACK\_TOKEN ./bin/hubot --adapter slack &"
only:
- master
CICD 中有 passed 不代表機器人一定啟動成功
如果有要修改 CICD 的佈署腳本,最好還是本機先執行確認再 push
使用 GitLab CICD 佈署後,確定機器人有啟動
參考
https://chatbotslife.com/building-a-slackbot-with-hubot-85016dd1477e
http://springest.io/hubot-part-1-get-it-running-locally-in-slack
https://github.com/hubotio/hubot/blob/master/docs/scripting.md
https://www.loggly.com/blog/building-chatops-bot-slack-loggly-part-1/
https://chatbotslife.com/building-a-slackbot-with-hubot-85016dd1477e
http://springest.io/hubot-part-1-get-it-running-locally-in-slack
回上一頁