今更ながら社内用Slackbotを作ろうと思いたち、RubyistのはしくれとしてRubotyで作ってみました。
色々試行錯誤がありましたが結論だけ書いていきます。
(そもそも僕がHerokuをちゃんと使いこなせていないことが原因の可能性もあります)
RubotyをSlackで動かすにはruboty作成者r7kamuraさんの以下の記事通りに進めていけばpingを返すくらいのBotは数分で作れます。
さて。
とりあえずHeroku上でRubotyが起動したものの、それだけでは物足りないので自分でプラグインを書きたいと思いますね?
では、herokuからコードをCloneします。SettingsページにリポジトリのURLが載っています。
git clone https://git.heroku.com/ramen-police.git
heroku git:remote --app ramen-police
git push heroku master -f
と、ここでcloneしたリポジトリの中身を見ると、なんと空です。
とりあえずbundle initしてGemfileに以下を追記します。
(Gemfileに記載したruboty-ramen-policeというgemが社内用に作ったRubotyプラグインです。
この辺の実装方法もちょっと一癖あったのですが、別記事でまとめようかなと思っています。)
source "https://rubygems.org"
gem "ruboty"
gem "ruboty-slack"
gem "ruboty-ramen-police", git: "https://gist.github.com/08d6c7858d0eb66ef952f7e0ba2de8b5.git", require: "ramen-police"
↑ではやっていませんが、GemfileにHerokuで稼働するRubyのバージョンも記入しておいたほうが良いかもしれません。
ここでハマったのですが、
そのままherokuにpushすると、だいたい以下のようなWarningが出ます。
heroku logs
remote: ###### WARNING:
remote: You have not declared a Ruby version in your Gemfile.
remote: To set your Ruby version add this line to your Gemfile:
remote: ruby '2.2.4'
remote: # See https://devcenter.heroku.com/articles/ruby-versions for more information.
remote:
remote: ###### WARNING:
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote: https://devcenter.heroku.com/articles/ruby-default-web-server
remote:
1つ目は先程書いたとおり、GemfileにRubyバージョンが指定されていないことによるワーニング。
問題は2つ目で、Procfileがないとpushした後にbotのプロセスが起動しません。
cloneしたリポジトリにはProcfileが無いので、自分で用意する必要があります。
(中身は https://github.com/r7kamura/ruboty-template より)
bot: bundle exec ruboty
Procfileを追加してherokuにpushしてあげます。
で完了。
今回作ったのはお遊びbotですが、いずれはChatOps的なものを作っていけたらなーと思っています。
Rubotyについてはこの記事にとても詳しくまとまっています。
http://qiita.com/tbpgr/items/39d93a0a33ec99e37da1