Inside PRESSBLOG

PRESSBLOG / Instagram / SNS マーケティングとか

Heroku+HubotでSlack連携するbotを作る

heroku+hubotでslack連携するbotを作成したのでメモ

環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.1
BuildVersion:   15B42

手順

Node.jsをインストール

$ brew install node

redisをインストール

$ brew install redis

Hubotのジェネレーターをインストール

$ npm install -g yo grunt-cli bower

Hubotをインストール

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

? Owner technuma <technuma@gmail.com>
? Bot name hubot
? 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|                            


> ws@0.4.31 install /Users/technuma/heroku/hubot/node_modules/hubot-slack/node_modules/slack-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)

CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
hubot-maps@0.0.2 node_modules/hubot-maps

hubot-help@0.1.3 node_modules/hubot-help

hubot-shipit@0.2.0 node_modules/hubot-shipit

hubot-diagnostics@0.0.1 node_modules/hubot-diagnostics

hubot-pugme@0.1.0 node_modules/hubot-pugme

hubot-rules@0.1.1 node_modules/hubot-rules

hubot-google-images@0.2.6 node_modules/hubot-google-images

hubot-google-translate@0.2.0 node_modules/hubot-google-translate

hubot-heroku-keepalive@1.0.2 node_modules/hubot-heroku-keepalive

hubot-redis-brain@0.0.3 node_modules/hubot-redis-brain
└── redis@0.8.4

hubot-scripts@2.16.2 node_modules/hubot-scripts
└── redis@0.8.4

hubot@2.18.0 node_modules/hubot
├── optparse@1.0.4
├── log@1.4.0
├── cline@0.8.2
├── scoped-http-client@0.11.0
├── async@0.9.2
├── coffee-script@1.6.3
├── chalk@1.1.1 (escape-string-regexp@1.0.4, supports-color@2.0.0, ansi-styles@2.1.0, strip-ansi@3.0.0, has-ansi@2.0.0)
├── connect-multiparty@1.2.5 (qs@2.2.5, type-is@1.5.7, on-finished@2.1.1, multiparty@3.3.2)
└── express@3.21.2 (escape-html@1.0.2, merge-descriptors@1.0.0, cookie@0.1.3, cookie-signature@1.0.6, utils-merge@1.0.0, fresh@0.3.0, methods@1.1.2, range-parser@1.0.3, vary@1.0.1, basic-auth@1.0.3, parseurl@1.3.1, etag@1.7.0, content-type@1.0.1, content-disposition@0.5.0, commander@2.6.0, depd@1.0.1, debug@2.2.0, proxy-addr@1.0.10, mkdirp@0.5.1, send@0.13.0, connect@2.30.2)

hubot-slack@3.4.2 node_modules/hubot-slack
└── slack-client@1.4.1 (log@1.4.0, coffee-script@1.9.3, ws@0.4.31)

Hubotを起動

$&nbsp;bin/hubot
hubot> [Fri Jan 22 2016 03:58:58 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 Jan 22 2016 03:58:58 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Fri Jan 22 2016 03:58:58 GMT+0900 (JST)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis

hubot> 
hubot> ping
hubot> hubot ping
hubot> PONG

herokuにデプロイ

$&nbsp;git init
Initialized empty Git repository in /Users/technuma/heroku/hubot/.git/
$&nbsp;git add .
$&nbsp;git commit -m "first comment"
[master (root-commit) cde1f4b] first comment
10 files changed, 385 insertions(+)
create mode 100644 .editorconfig
create mode 100644 .gitignore
create mode 100644 Procfile
create mode 100644 README.md
create mode 100755 bin/hubot
create mode 100644 bin/hubot.cmd
create mode 100644 external-scripts.json
create mode 100644 hubot-scripts.json
create mode 100644 package.json
create mode 100644 scripts/example.coffee
$&nbsp;heroku login
Enter your Heroku credentials.
Email: technuma@gmail.com
Password (typing will be hidden): 
▸    Authentication failure.
$&nbsp;heroku create hubot
Creating hubot... !!!
▸    Name is already taken
$&nbsp;heroku create hubot
Creating hubot... !!!
▸    Name is already taken
$&nbsp;heroku create hubot
Creating hubot... done, stack is cedar-14
https://hubot.herokuapp.com/ | https://git.heroku.com/hubot.git
$&nbsp;git remote -v
heroku  https://git.heroku.com/hubot.git (fetch)
heroku  https://git.heroku.com/hubot.git (push)
$&nbsp;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.92 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:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=true
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  0.10.x
remote:        engines.npm (package.json):   unspecified (use default)
remote:        
remote:        Resolving node version 0.10.x via semver.io...
remote:        Downloading and installing node 0.10.41...
remote:        Using default npm version: 1.4.29
remote: 
remote: -----> Restoring cache
remote:        Skipping cache restore (new runtime signature)
remote: 
remote: -----> Building dependencies
remote:        Pruning any extraneous modules
remote:        Installing node modules (package.json)
remote:        
remote:        > ws@0.4.31 install /tmp/build_5fec3af93363f82b53cc506ab67fabbb/node_modules/hubot-slack/node_modules/slack-client/node_modules/ws
remote:        > (node-gyp rebuild 2> builderror.log) || (exit 0)
remote:        
remote:        make: Entering directory `/tmp/build_5fec3af93363f82b53cc506ab67fabbb/node_modules/hubot-slack/node_modules/slack-client/node_modules/ws/build'
remote:        CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
remote:        SOLINK_MODULE(target) Release/obj.target/bufferutil.node
remote:        SOLINK_MODULE(target) Release/obj.target/bufferutil.node: Finished
remote:        COPY Release/bufferutil.node
remote:        CXX(target) Release/obj.target/validation/src/validation.o
remote:        SOLINK_MODULE(target) Release/obj.target/validation.node
remote:        SOLINK_MODULE(target) Release/obj.target/validation.node: Finished
remote:        COPY Release/validation.node
remote:        make: Leaving directory `/tmp/build_5fec3af93363f82b53cc506ab67fabbb/node_modules/hubot-slack/node_modules/slack-client/node_modules/ws/build'
remote:        hubot-maps@0.0.2 node_modules/hubot-maps
remote:        
remote:        hubot-help@0.1.3 node_modules/hubot-help
remote:        
remote:        hubot-shipit@0.2.0 node_modules/hubot-shipit
remote:        
remote:        hubot-diagnostics@0.0.1 node_modules/hubot-diagnostics
remote:        
remote:        hubot-pugme@0.1.0 node_modules/hubot-pugme
remote:        
remote:        hubot-rules@0.1.1 node_modules/hubot-rules
remote:        
remote:        hubot-google-images@0.2.6 node_modules/hubot-google-images
remote:        
remote:        hubot-google-translate@0.2.0 node_modules/hubot-google-translate
remote:        
remote:        hubot-heroku-keepalive@1.0.2 node_modules/hubot-heroku-keepalive
remote:        
remote:        hubot-redis-brain@0.0.3 node_modules/hubot-redis-brain
remote:        └── redis@0.8.4
remote:        
remote:        hubot@2.18.0 node_modules/hubot
remote:        ├── optparse@1.0.4
remote:        ├── log@1.4.0
remote:        ├── cline@0.8.2
remote:        ├── scoped-http-client@0.11.0
remote:        ├── async@0.9.2
remote:        ├── chalk@1.1.1 (escape-string-regexp@1.0.4, supports-color@2.0.0, ansi-styles@2.1.0, has-ansi@2.0.0, strip-ansi@3.0.0)
remote:        ├── coffee-script@1.6.3
remote:        ├── connect-multiparty@1.2.5 (qs@2.2.5, on-finished@2.1.1, type-is@1.5.7, multiparty@3.3.2)
remote:        └── express@3.21.2 (escape-html@1.0.2, merge-descriptors@1.0.0, cookie@0.1.3, utils-merge@1.0.0, cookie-signature@1.0.6, methods@1.1.2, fresh@0.3.0, range-parser@1.0.3, basic-auth@1.0.3, vary@1.0.1, parseurl@1.3.1, content-type@1.0.1, etag@1.7.0, content-disposition@0.5.0, commander@2.6.0, depd@1.0.1, debug@2.2.0, proxy-addr@1.0.10, mkdirp@0.5.1, send@0.13.0, connect@2.30.2)
remote:        
remote:        hubot-scripts@2.16.2 node_modules/hubot-scripts
remote:        └── redis@0.8.4
remote:        
remote:        hubot-slack@3.4.2 node_modules/hubot-slack
remote:        └── slack-client@1.4.1 (log@1.4.0, coffee-script@1.9.3, ws@0.4.31)
remote: 
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (nothing to cache)
remote: 
remote: -----> Build succeeded!
remote:        ├── hubot@2.18.0
remote:        ├── hubot-diagnostics@0.0.1
remote:        ├── hubot-google-images@0.2.6
remote:        ├── hubot-google-translate@0.2.0
remote:        ├── hubot-help@0.1.3
remote:        ├── hubot-heroku-keepalive@1.0.2
remote:        ├── hubot-maps@0.0.2
remote:        ├── hubot-pugme@0.1.0
remote:        ├── hubot-redis-brain@0.0.3
remote:        ├── hubot-rules@0.1.1
remote:        ├── hubot-scripts@2.16.2
remote:        ├── hubot-shipit@0.2.0
remote:        └── hubot-slack@3.4.2
remote:        
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing... done, 7.9MB
remote: -----> Launching...
remote:        Released v3
remote:        https://hubot.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/hubot.git
* [new branch]      master -> master

HerokuのURLを確認

$&nbsp;heroku open&nbsp;
Opening hubot... done

設定をセット

$&nbsp;heroku config:set HUBOT_SLACK_TOKEN=【トークン】 HUBOT_SLACK_TEAM=【チーム名】 HUBOT_SLACK_BOTNAME=hubot
Setting config vars and restarting hubot... done
HUBOT_SLACK_BOTNAME: hubot
HUBOT_SLACK_TEAM:    チーム名
HUBOT_SLACK_TOKEN:   トークン
$&nbsp;heroku apps:info
=== hubot
Dynos:         web: 1
Git URL:       git@heroku.com:hubot.git
Owner:         technuma@gmail.com
Region:        us
Repo Size:     9 kB
Slug Size:     8 MB
Stack:         cedar-14
Web URL:       https://hubot.herokuapp.com/

HUBOT_HEROKU_KEEPALIVE_URL を設定
※これを設定しないと正常に動作しないので注意!

$&nbsp;heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=https://hubot.herokuapp.com/
Setting config vars and restarting hubot... done
HUBOT_HEROKU_KEEPALIVE_URL: https://hubot.herokuapp.com/