LoginSignup
1
1

More than 3 years have passed since last update.

EC2上でhubotをslackとchatworkの両方で動かす

Last updated at Posted at 2020-03-05

まずはhubotを動かすために必要なものをインストール

$ sudo curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash

実行時の出力に環境変数の設定処理とnvmのロード処理行うコマンドが出力されるので、これを.bash_profileに追記
追記後に読み込み

$ vi .bash_profile

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

$ source .bash_profile

nvm(node version manager)のインストールと、npm(node package manager)から必要なパッケージをインストール

$ nvm install 9.4.0
$ npm install -g hubot coffee-script yo generator-hubot

hubot用ディレクトリを作成し初回起動
起動時に色々聞かれますが例が出るのでその通りに設定していきます

$ mkdir hubot
$ cd hubot
$ yo hubot --adapter slack

? Owner hogehoge
? Bot name hogehoge
? Description A simple helpful robot for your Company

slackとchatwork用のパッケージ、永続化のためのforeverパッケージをインストール

$ npm install hubot-slack hubot-chatwork
$ npm install -g forever

起動スクリプトを作成して実行

start_slack.sh
#!/bin/bash

export HUBOT_SLACK_TOKEN="hogehoge"
export PORT="8091"

forever -w start -c coffee node_modules/.bin/hubot --adapter slack
start_chatwork.sh
#!/bin/bash

export HUBOT_CHATWORK_TOKEN="hogehoge"
export HUBOT_CHATWORK_ROOMS="123456789"
export HUBOT_CHATWORK_API_RATE="500"
export PORT="8092"

forever -w start -c coffee node_modules/.bin/hubot --adapter chatwork

起動後こうなればOK

$ forever list
info:    Forever processes running
data:        uid  command script                                     forever pid  id logfile                          uptime      
data:    [0] ysEB coffee  node_modules/.bin/hubot --adapter slack    4075    4085    /home/ec2-user/.forever/ysEB.log 0:0:0:5.739 
data:    [1] sEKt coffee  node_modules/.bin/hubot --adapter chatwork 4108    4118    /home/ec2-user/.forever/sEKt.log 0:0:0:3.077 

エラー例1

chatworkとslackを同時に動かす場合、ポートを別にしないと競合して後に起動した方が↓のようなエラーを吐いて落ちるので起動時に指定する

ERROR Error: listen EADDRINUSE 0.0.0
.0:8080

※プロセスを確認し、いくつも動いてしまっているようならキルする

$ ps aux | grep node
$ killall node

エラー例2

↓のようなエラーが出る場合は監視対象が多すぎる?

events.js:137
      throw er; // Unhandled 'error' event
      ^
Error: watch /home/hogehoge/hubot/node_modules/@slack/client/lib/data-store/message-handlers/bots.js ENOSPC

下記コマンドを実行した後に立ち上げ直したら出なくなった
参考 : https://qiita.com/mmmpa/items/12859d2c9ea5afc11751

$ echo fs.inotify.max_user_watches=582222 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
1
1
0

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
1
1