slackにhubotを住まわせる記事は山のようにあるが、それを「hubotを自前サーバ」「それもFreeBSDに」という条件にすると記事数が激減する。
そんななか、四苦八苦してなんとか達成したのでここに採録。
なお、そっけなく不親切に見えるのは、ここまでたどり着くのに疲労困憊したから。
ではどうぞ。
jailの作成
フッツーに。
以下ではqjailを使っているがezjailでもなんでも、好きなのを使いたまえ。
1 |
sudo qjail create hubot -4 192.168.200.1 |
npmのインストール
まずjailをスタートさせて中に入ろうか。
そしてnpmのインストール。
1 2 |
sudo qjail console ex02 pkg install npm |
node, npmのバージョンを確認しておく。
問題なくインストールされているかの確認でもある。
1 2 |
node -v npm -v |
generatorのインストール
1 |
npm install -g yo generator-hubot |
hubot用ユーザの作成
jailの中でadduserし、お好きなユーザを作る。
以下では「bot」というユーザ。
1 |
adduser bot |
hubot用ユーザでhubotレポジトリ作成
1 2 3 4 |
su - bot mkdir hubot cd hubot yo hubot --adapter slack |
botホストの作業はいったんはここまで。
以降、slack側で。
slackにhubotのインストール
slackのteamメニューからApp&Integrations > Hubotをインストール
表示されるAPI Tokenを記録する。
hubotがslackへアクセスするためのトークン。
botホストで起動スクリプトの作成
再びbotホスト
/usr/local/etc/rc.d/hubotとして以下を作成。
試行錯誤で作ったから雑なスクリプトであることに注意。
それから実行権限付けるのを忘れないこと。
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 32 33 34 |
#! /bin/sh # # # PROVIDE: hubot # REQUIRE: DAEMON # KEYWORD: shutdown # $Id: hubot,v 1.3 2016/04/16 12:57:03 root Exp $ . /etc/rc.subr name=hubot rcvar=hubot_enable command=/usr/home/bot/hubot/bin/hubot command_args="--adapter slack" hubot_user=bot pidfile="/var/run/${name}.pid" procname=node start_cmd="${name}_start" hubot_start() { export HUBOT_SLACK_TOKEN=xoxb-xxxx-xxxxx export PATH="$PATH:/usr/home/bot/hubot/bin:/usr/local/bin" cd /usr/home/bot/hubot echo "starting hubot" /usr/sbin/daemon -p ${pidfile} -u ${hubot_user} ${command} ${command_args} } load_rc_config $name run_rc_command "$1" |
起動確認
/etc/rc.confにhubot_enable=YESを記載。
service -l でhubotがリストされることを確認したら、service hubot startで起動。
以下のような表示が出ればOK。
1 2 3 4 5 |
[Sat Apr 16 2016 21:55:52 GMT+0900 (JST)] INFO Connecting... [Sat Apr 16 2016 21:55:53 GMT+0900 (JST)] INFO Logged in as xxx of xxxxxx, but not yet connected [Sat Apr 16 2016 21:55:54 GMT+0900 (JST)] INFO Slack client now connected [Sat Apr 16 2016 21:55:55 GMT+0900 (JST)] 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)` [Sat Apr 16 2016 21:55:55 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379 |
ポートフォワーディングの設定。
前章の起動時メッセージ、「INFO hubot-redis-brain: Using default redis on localhost:6379」に注目。
6379でポート待ち受けている。
ご家庭のサーバで動かしている場合には、ルータでのポートフォワーディング設定を忘れずに。
なお2016/4/16現在、slackの側でサーバのIPアドレスを指定したりするような設定は不要。
slack側の確認
slackでbotを住まわせたいchannelにInvite。
botの名前の左側に在席マークが点いているのを確認し、「@<bot名> ping」とし、botからPONGと帰ってきたら成功。
もし起動時メッセージが不要であれば、以下のように/usr/sbin/daemonのオプションに-fを加えるとよい。
1 |
/usr/sbin/daemon -f -p ${pidfile} -u ..... |