ボット・クローラー Advent Calendar 2016 17日目の記事です。
今更ですが Slack ボットを導入してみます。
npm 導入
$ git clone https://github.com/creationix/nvm.git ~/.nvm $ vi ~/.bashrc $ echo "source ~/.nvm/nvm.sh" >> ~/.bashrc $ source ~/.bashrc $ nvm install iojs $ nvm --version 0.32.1 $ npm --version 2.14.3
redis 導入
# yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm # yum -y install redis # systemctl enable redis # systemctl list-unit-files -t service | grep redis redis-sentinel.service disabled redis.service enabled # systemctl start redis # systemctl status redis ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sun 2016-12-11 12:31:47 JST; 6s ago Main PID: 19184 (redis-server) ...... # redis-cli -v redis-cli 2.8.19
Redis 疎通確認
# redis-cli ping PONG
Hubot 導入
$ npm install -g hubot coffee-script $ npm install -g generator-hubot $ npm install -g yo
Hubot 設定
$ cd $ mkdir hubotdir ← 名前とか適当で良い $ chmod 777 hubotdir $ cd hubotdir $ yo hubot ? ========================================================================== We're constantly looking for ways to make yo better! May we anonymously report usage statistics to improve the tool over time? More info: https://github.com/yeoman/insight & http://yeoman.io ========================================================================== Yes _____________________________ / \ //\ | Extracting input for | ////\ _____ | self-replication process | //////\ /_____\ \ / ======= |[^_/\_]| /---------------------------- | | _|___@@__|__ +===+/ /// \_\ | |_\ /// HUBOT/\\ |___/\// / \\ \ / +---+ \____/ | | | //| +===+ \// |xx| ? Owner kobake <kobake@users.sourceforge.net> ? Bot name kobarobo ← ロボット名を入力(今回は「kobarobo」にした) ? Description jikken ← 適当に入力(今回は「jikken」にした) ? Bot adapter slack ←「slack」と入力 ....
これでまずは初期化完了。
Slack 側設定
https://slack.com/apps で各種アプリ連携が行えます。
今回は検索欄に「hubot」と入力、それにマッチする「Hubot」が見つかるのでそれを選択します。
「Install」を選択。
名前は適当で良いです。「Add Hubot Integration」を推します。
次の画面で HUBOT_SLACK_TOKEN というものが取得できます。これをこの後の設定で使います。
Hubot 起動
起動用スクリプトを作る
~/hubotdir/start.sh (新規作成。名前は適当)
#!/bin/sh export HUBOT_SLACK_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ← ここはさきほど取得したもの。 ./bin/hubot --adapter slack
起動
$ cd ~/hubotdir $ rm hubot-scripts.json ← いらないらしいので消す $ chmod a+x start.sh $ ./start.sh
まずは example.coffee をいじると良いと思う
僕 CoffeeScript よくわからないですけど見よう見まねでまぁだいたいなんとかなります。
~/hubotdir/scripts/example.coffee
module.exports = (robot) -> robot.hear /はい/i, (res) -> res.send "はいじゃないが" robot.hear /ごめん/i, (res) -> res.send "いいよ"
※このスクリプトを書き換えたら start.sh を実行しなおすこと。
結果