刺身タンポポ職人なう

文系女子大生がエンジニアとして就職してその後

Slack + Hubot + Heroku で松岡修造botを作ったよ

Slack に Hubot を入れる

https://slack.com/apps から Hubot を選択して、自分のグループに Install する。

f:id:tooooomin:20160103125832p:plain

猫の朝会グループにインストールしたよ

botの作成

hubot と coffee-script をインストール

$ npm install -g hubot coffee-script

yo と generator-hubot のインストール

$ npm install -g yo generator-hubot

hubot作成

$ mkdir -p shuzobot
$ cd shuzobot                                                              ⏎
$ yo hubot
                     _____________________________
                    /                             \
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             /
 ======= |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|

? Owner MY_EMAIL_ADRESS
? Bot name shuzobot
? Description A simple helpful robot for your Company
? Bot adapter (campfire) slackgot back false
? Bot adapter slack
   create bin/hubot
   create bin/hubot.cmd
   create Procfile
   create README.md
   create external-scripts.json
   create hubot-scripts.json
   create .gitignore
   create package.json
   create scripts/example.coffee
   create .editorconfig
                     _____________________________
 _____              /                             \
 \    \             |   Self-replication process   |
 |    |    _____    |          complete...         |
 |__\\|   /_____\   \     Good luck with that.    /
   |//+  |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|

~~~~~~~~~~~
~~~~~~~~~~~

なんか作ってくれた(^.^)

$ ls
Procfile              bin                   hubot-scripts.json    package.json
README.md             external-scripts.json node_modules          scripts

コマンドラインよい。なんかかわいい。

f:id:tooooomin:20160103130117p:plain

ローカルでの挙動確認

PONG答えてくれたらOK

$ bin/hubot
shuzobot> [Sun Jan 03 2016 00:36:39 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)`
[Sun Jan 03 2016 00:36:39 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379

shuzobot> shuzobot ping
shuzobot> PONG

Herokuへデプロイ

heroku toolbeltのインストール

↓ ここからダウンロードした

toolbelt.herokuapp.com

デプロイ

さっき作った shuzobot ディレクトリへ移動して git init

$ cd shuzobot
$ git init
$ git add .
$ git commit -m "first commit"

heroku へデプロイ!

$ heroku create 
Enter your Heroku credentials.
Email: hoge@gmail.com
Password (typing will be hidden):
Logged in as hoge@gmail.com

Creating sleepy-anchorage-5456... done, stack is cedar-14
https://xxxx.herokuapp.com/ | https://git.heroku.com/xxxx.git
Git remote heroku added

$ git push heroku master
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (14/14), 5.96 KiB | 0 bytes/s, done.
Total 14 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
~~~~~~~
~~~~~~~

herokuが動いているか確認

$ heroku open

ブラウザが起動されます

f:id:tooooomin:20160103132053p:plain

Heroku と Slack をごにょ

Heroku の設定

Slack の管理画面にある HUBOT_SLACK_TOKEN を Heroku の管理画面で設定

f:id:tooooomin:20160103133740p:plain

Heroku 管理画面で設定

f:id:tooooomin:20160103133419p:plain

HEROKU_URL=http://XXXXX.herokuapp.com //  $heroku open で起動したブラウザURL
HUBOT_SLACK_TOKEN=XXXXX // Slackの管理画面にある値
HUBOT_SLACK_TEAM=MY_TEAM // 猫の朝会
HUBOT_SLACK_BOTNAME=shuzobot // ボットの名前

Hubot の Script を編集してみる

$ vi scripts/hoge.coffee

マイナス発言にランダムに答えるスクリプトを追加

module.exports = (robot) ->

  robot.hear /疲れた/i, (msg) ->
    msg.send msg.random ["弱気になったとき、まず一ヵ月後の自分を想像してみる。それが自分の好きな姿だとしたら、そのために何をするべきかを考える。そうすれば、少なくともその日までは目的意識を保ち続けることができる", "独りで苦しんでるんだろう 辛いだろう? 暗いんだろう? じゃあエースをねらえを歌ってみろよ!!!", "みんな!!竹になろうよ 竹ってさあ台風が来てもしなやかじゃない。台風負けないんだよ雪が来てもね おもいっきりそれを跳ね除ける!!力強さがあるんだよそう、みんな!!!竹になろう!!!バンブー!!!", "僕が偉そうに話してることは全て、これまで僕ができなかったこと"]

  robot.hear /帰りたい/i, (msg) ->
    msg.send msg.random ["どうした。文句があるなら、自分でプレーしろ!", "人は完璧を求める。しかし、完璧だと思った時から全てがやり直しになる", "じゃんけんの必勝法は、強く握り締めたグーを出すこと", "苦しいか? 笑え!!"]

ローカルで確認してみる

$ bin/hubot

shuzobot> [Sun Jan 03 2016 12:44:02 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)`
[Sun Jan 03 2016 12:44:02 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379

shuzobot> shuzobot 疲れた
shuzobot> みんな!!竹になろうよ 竹ってさあ台風が来てもしなやかじゃない。台風負けないんだよ雪が来てもね おもいっきりそれを跳ね除ける!!力強さがあるんだよそう、みんな!!!竹になろう!!!バンブー!!!

OK!バンブー!!!

$ git push heroku master

heroku に push したら scripts が反映されるはず。 動かない ☹ ☹ ☹ ☹ ってときは

$ heroku logs

でログが見れるよ\( ⁰⊖⁰)/

bot をグループに招待して、つぶやけば、お返事くれる。 次はもっと生産性のあるもの作ろう( ꒪⌓꒪)

f:id:tooooomin:20160103133924p:plain