LoginSignup
42
42

More than 5 years have passed since last update.

[Slack]Botkitをherokuの無料プランで動かす方法

Last updated at Posted at 2016-05-03

SlackのBotを作るためのフレームワークBotkitでbotを作成し、
herokuの無料プランで動かす方法です。

1.SlackでBotを作成する

下記リンクより、SlackのBot設定ページに遷移する。
https://my.slack.com/services/new/bot
Botの名前を設定し、Add integtationボタンを押下する。
bot1.png

API Tokenが発行されるので、メモしておく。
BotのIconや名前などもここで設定できる。
bot2.png

2.Botkitをダウンロードする

ローカル環境の適当なディレクトリにBotkitをダウンロードする。
git clone https://github.com/howdyai/botkit.git

botkitの依存パッケージをインストールする。
cd botkit
npm install

3.サンプルBotをローカル環境で動かす

下記コマンドを実行し、ステップ1でコピーしたAPI Tokenを使ってサンプルBotを動かす。
token=YOUR_API_TOKEN node bot.js
※YOUR_API_TOKENを各自のトークンで差し替える。

好きなチャンネルに入り、Botを招待すれば、Botの作成は完了。
/invite @test-bot

bot6.png

デフォルトでは以下のコマンドが使える。

@test-bot hello
@test-bot call me <Other Name> ※<Other Name>には好きなワードを入れる
@test-bot who am I
@test-bot shutdown
@test-bot uptime

テストが終わったら、ローカルで動かしているbotはctrl+cで止める。

4.GitHubにアップする

herokuでworkerを登録するために、ルートディレクトリにProcfileを作成してからコミットし、Githubにアップする。

Procfile
worker: node slack_bot.js

5.Herokuにアップする

Herokuにログインし、botkitをアップする。
heroku login
git push heroku master

Herokuの環境変数にAPI Tokenを追加する。
heroku config:add token=YOUR_API_TOKEN
※YOUR_API_TOKENを各自のトークンで差し替える。

6.workerを起動させる

herokuのDashboardでwebをoffにし、workerをonにする。
bot3.png

7.Heroku SchedulerでJobを登録する

Herokuの無料プランでは一定時間(30分間)何もアクセスがないと自動的にSleep状態になるため、Heroku Schedulerを使って、定期的にDynoを起こす必要がある。
まずはHeroku Schedulerをインストールする。
bot4.png

次に、Heroku SchedulerにJobを登録する。
FREQUENCYはHourlyにする。
bot5.png

(seq 22 23; seq 0 3; seq 10 18) | grep -qw $((10#date -u "+%H")) && curl http://xxxxxxx.herokuapp.com > /dev/null 2>&1
※上記コマンドはGMT表記なので、6-12時、19-27時の合計14時間稼働することになる。


<参考>
http://toach.click/slack-botkit/
http://qiita.com/kawanamiyuu/items/3193c74c888b816c6bd4

42
42
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
42
42