LoginSignup
25
26

More than 5 years have passed since last update.

slack と Hubot を連携するためにやること

Posted at

Hubot プロジェクトの準備 (Mac)

Hubot のインストール

shell
$ npm install -g yo generator-hubot

Hubot のプロジェクトを作成

shell
$ mkdir mybot(プロジェクト名)
$ cd mybot
$ yo hubot
# 今回は Slack と連携したいので Bot adapter には slack を指定

slack bot の用意

slack で適当なチームを用意する

slack で hubot を使用するための設定をする

  • Apps & Custom Integrations を選択

Apps & Custom Integrations.png

  • hubot を検索
    Search_hubot.png

  • 見つかったらインストールして、名前を設定 (今回は mybot)

    • API token が発行されるのでメモ
    • HUBOT_SLACK_TOKEN=xxx

Heroku の設定

ターミナルから Heroku を操作するための準備

$ heroku login

Heroku 上にアプリケーションを作成

shell
$ heroku create app_name
Creating app_name... done, stack is cedar-14
https://app_name.herokuapp.com/ | https://git.heroku.com/app_name.git
Git remote heroku added
# ちなみに heroku 上でユニークな名前じゃないといけないっぽい
$ git remote -v
heroku  https://git.heroku.com/app_name.git (fetch)
heroku  https://git.heroku.com/app_name.git (push)

Heroku の config を追加

shell
$ heroku config:add HUBOT_SLACK_TOKEN=xxx
# slack の token を設定する(上で発行された API token)
$ heroku config:add HUBOT_CHATWORK_ROOMS="room_id"
# slack で hubot を参加させたい room id を設定
$ heroku config:add TZ=Asia/Tokyo 
# タイムゾーンを日本に設定

Heroku が起きている時間をスケジューラで設定

keep-alive 設定

  • heroku は 30 分間アクセスがないと sleep するので hubot-heroku-keepalive を設定
shell
$ npm install hubot-heroku-keepalive --save
$ heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=https://app_name.herokuapp.com/
# Process Scheduler と合わせて wakeup time と sleep time を設定する
$ heroku config:add HUBOT_HEROKU_WAKEUP_TIME=7:00
$ heroku config:add HUBOT_HEROKU_SLEEP_TIME=24:00

Heroku へのデプロイ

bot がやること

  • 毎正時に時刻を知らせる機能を追加
script/notice.coffee
cronJob = require('cron').CronJob
module.exports = (robot) ->
  new cronJob('0 0 * * * *', () =>
    date     = new Date()
    hour     = date.getHours()
    envelope = room: process.env.HUBOT_CHATWORK_ROOMS
    robot.send envelope, "#{hour} 時です。"
  ).start()

デプロイ

  • heroku に push
$ git push heroku master

結果

  • こんな感じで決まった時間に決まったメッセージを post できる

notice.png

参考

25
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
26