hubot-slackをHerokuで動かせる方法


[hubot-slack](https://github.com/slackhq/hubot-slack)

```bash:
$ npm i -g hubot hubot-slack yo generator-hubot coffee-script
$ mkdir -p /path/to/hubot
$ cd /path/to/hellobot
$ yo hellobot
$ npm i hubot-slack -S
$ vim Procfile
web: bin/hubot --adapter slack
$ ./bin/hubot
```

あとは、これを`Heroku`に`push`すればいいのですが、Freeで使うためには、幾つかの工夫が必要になります。

```bash:
# heroku create
$ heroku login
$ heroku create
$ heroku git:remote -a ${appname}

# 念のためスケールなどを最小にする
$ heroku ps:scale web=1
$ heroku addons:add redistogo:nano
$ heroku config:set HUBOT_SLACK_TOKEN=xoxb-000000000-XXXXXXXXXXXXX

# process-schedulerアドオンの追加
# https://devcenter.heroku.com/articles/process-scheduler
$ heroku addons:add process-scheduler
$ heroku addons:open process-scheduler
# 範囲指定して0を入力するのですが、"1:00-6:00"で良いと思います。0の時間を寝かせます

# keepaliveをインストール
$ npm i hubot-heroku-keepalive -S
$ vim ./external-scripts.json
[ "hubot-heroku-keepalive" ]

# keepaliveの設定
# https://devcenter.heroku.com/articles/scheduler
$ heroku config:add HUBOT_HEROKU_WAKEUP_TIME=8:00
$ heroku config:add HUBOT_HEROKU_SLEEP_TIME=24:00
$ heroku config:add TZ='Asia/Tokyo'
$ heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s | grep web-url | cut -d= -f2)
$ heroku addons:create scheduler:standard
$ heroku addons:open scheduler
# 以下を追加します
# curl ${HUBOT_HEROKU_KEEPALIVE_URL}heroku/keepalive

# heroku push
$ git add -A
$ git push heroku master
```

## プラグインの追加

基本的には、`npm i ${plugin} -S`した後に、ファイルを編集します。

```json:external-scripts.json 
[
  "hubot-heroku-keepalive",
  "hubot-shipit",
  "${plugin}"
]
```

## Werckerで自動デプロイと通知

```bash:
deploy:
  steps:
    - heroku-deploy:
         key: $HEROKU_KEY
         user: $HEROKU_USER
         app-name: $HEROKU_APP_NAME
  after-steps:
    - wantedly/pretty-slack-notify:
         webhook_url: $SLACK_WEBHOOK_URL
         channel: private
```

## スクリプトの書き方

```coffee:scripts/test.coffee
module.exports = (robot) ->

   robot.respond /@syui/i, (msg) ->
     img_url = "https://pbs.twimg.com/profile_images/458970057993756672/L46E-72D_bigger.png"
     user_name = "user : syui"
     user_profile = "
     blog : https://syui.github.io
     \nqiita : https://qiita.com/syui"
     msg.send "#{img_url}\n#{user_name}\n\n"
     ,"#{user_profile}"


  # robot.respond /open the (.*) doors/i, (msg) ->
  #   doorType = msg.match[1]
  #   if doorType is "pod bay"
  #     msg.reply "I'm afraid I can't let you do that."
  #   else
  #     msg.reply "Opening #{doorType} doors"
  #
  # robot.respond /I like pie/i, (msg) ->
  #   msg.emote "makes a fmsghly baked pie"
  #
  # lulz = ['lol', 'rofl', 'lmao']
  #
  # robot.respond /lulz/i, (msg) ->
  #   msg.send msg.random lulz
  #
  # robot.topic (msg) ->
  #   msg.send "#{msg.message.text}? That's a Paddlin'"
  #
  #
  # enterReplies = ['Hi', 'Target Acquired', 'Firing', 'Hello friend.', 'Gotcha', 'I see you']
  # leaveReplies = ['Are you still there?', 'Target lost', 'Searching']
  #
  # robot.enter (msg) ->
  #   msg.send msg.random enterReplies
  # robot.leave (msg) ->
  #   msg.send msg.random leaveReplies
  #
  # answer = process.env.HUBOT_ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING
  #
  # robot.respond /what is the answer to the ultimate question of life/, (msg) ->
  #   unless answer?
  #     msg.send "Missing HUBOT_ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING in environment: please set and try again"
  #     return
  #   msg.send "#{answer}, but what is the question?"
  #
  # robot.respond /you are a little slow/, (msg) ->
  #   setTimeout () ->
  #     msg.send "Who you calling 'slow'?"
  #   , 60 * 1000
  #
  # annoyIntervalId = null
  #
  # robot.respond /annoy me/, (msg) ->
  #   if annoyIntervalId
  #     msg.send "AAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIHHHHHHHHHH"
  #     return
  #
  #   msg.send "Hey, want to respond the most annoying sound in the world?"
  #   annoyIntervalId = setInterval () ->
  #     msg.send "AAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIHHHHHHHHHH"
  #   , 1000
  #
  # robot.respond /unannoy me/, (msg) ->
  #   if annoyIntervalId
  #     msg.send "GUYS, GUYS, GUYS!"
  #     clearInterval(annoyIntervalId)
  #     annoyIntervalId = null
  #   else
  #     msg.send "Not annoying you right now, am I?"
  #
  #
  # robot.router.post '/hubot/chatsecrets/:room', (req, msg) ->
  #   room   = req.params.room
  #   data   = JSON.parse req.body.payload
  #   secret = data.secret
  #
  #   robot.messageRoom room, "I have a secret: #{secret}"
  #
  #   msg.send 'OK'
  #
  # robot.error (err, msg) ->
  #   robot.logger.error "DOES NOT COMPUTE"
  #
  #   if msg?
  #     msg.reply "DOES NOT COMPUTE"
  #
  # robot.respond /have a soda/i, (msg) ->
  #   # Get number of sodas had (coerced to a number).
  #   sodasHad = robot.brain.get('totalSodas') * 1 or 0
  #
  #   if sodasHad > 4
  #     msg.reply "I'm too fizzy.."
  #
  #   else
  #     msg.reply 'Sure!'
  #
  #     robot.brain.set 'totalSodas', sodasHad+1
  #
  # robot.respond /sleep it off/i, (msg) ->
  #   robot.brain.set 'totalSodas', 0
  #   msg.reply 'zzzzz'
```

```bash:
> @hellobot @syui

![画像](url)
blog : [url](url)
qiita : [url](url)
```