LoginSignup
12
9

More than 5 years have passed since last update.

いまさらherokuでhubotを動かしてSlackで会話できるようにした

Last updated at Posted at 2015-06-25

いまさらやってみた。いまさらね。

インストール

npm install hubot generator-hubot yo

botの原型を作る

最近は下記の様にやると怒られるらしい。

% hubot --create hnsta-bot
'hubot --create' is deprecated. Use the yeoman generator instead:
    npm install -g yo generator-hubot
    mkdir -p hnsta-bot
    yo hubot
See https://github.com/github/hubot/blob/master/docs/README.md for more details on getting started.

怒られたとおりに実行する。

% mkdir mybot
% cd mybot
% yo hubot

すると色々聞かれる。
だいたいデフォルトでOK。今回はSlackと繋ぎたいのでBot adapterだけSlackを入力した。

? ==========================================================================
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: XXXXX XXXXX <yhinoz@gmail.com>
? Bot name: mybot
? Description: A simple helpful robot for your Company
? Bot adapter: (campfire) slack
? Bot adapter: slack

ひとまず会話してみる

下記コマンドでhubotが起動する。

% bin/hubot

最初エラー吐かれるけど一旦無視。

% bin/hubot
mybot> [Fri May 29 2015 07:43:34 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)`
[Fri May 29 2015 07:43:34 GMT+0900 (JST)] INFO Using default redis on localhost:6379

mybot>
mybot> @mybot help
mybot adapter - Reply with the adapter
mybot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
mybot echo <text> - Reply back with <text>
mybot help - Displays all of the help commands that mybot knows about.
mybot help <query> - Displays all help commands that match <query>.
mybot image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
mybot map me <query> - Returns a map view of the area returned by `query`.
mybot mustache me <query> - Searches Google Images for the specified query and mustaches it.
mybot mustache me <url> - Adds a mustache to the specified URL.
mybot ping - Reply with pong
mybot pug bomb N - get N pugs
mybot pug me - Receive a pug
mybot the rules - Make sure mybot still knows the rules.
mybot time - Reply with current time
mybot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
mybot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
mybot youtube me <query> - Searches YouTube for the query and returns the video embed link.
ship it - Display a motivation squirrel
mybot>

botにhelpと話しかけることでヘルプが表示できた。

heroku上でhubotを動かして、Slackで会話する

まずherokuにログインする。

% heroku login
Enter your Heroku credentials.
Email: xxx@yyy.zzz
Password (typing will be hidden):
Authentication successful.

次に、作ったファイルをgitで管理するようにする。

% git init
% git add .
% git commit -m 'init'

とかしておく。
続いてherokuの設定に移る。
まずはアプリを作成する。

% heroku create YourAppName

作成したらhubotを動かす設定をする。

% heroku config:set HEROKU_URL=https://my-company-slackbot.herokuapp.com
% heroku config:set HUBOT_SLACK_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

一応タイムゾーンも設定しておく。

% heroku config:add TZ=Asia/Tokyo

設定が終わったらhubotのコードをデプロイする。

% git push heroku master

最後にbotを起動する。

% heroku ps:scale web=1

これでslackからbotに語りかけるとコマンドライン上でやっていたのと同じように返事をしてくれる。

参考

12
9
2

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
12
9