5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

slack用hubot作成時のメモ

Last updated at Posted at 2017-11-16

とりあえず、メンションしたらランダムで何かを返してくれるbotでも作る。

開発環境の準備

vagrant

  config.vm.box = "centos/7"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = 2
  end

node v8指定

curl -sL https://rpm.nodesource.com/setup_8.x | bash -
yum install -y nodejs gcc-c++ make
npm install -g jsfmt sails node-dev pm2 
npm install -g hubot hubot-slack yo generator-hubot coffee-script grunt-cli bower coffe-script
npm update

herokuも準備

npm install -g heroku-cli

アプリ用のユーザを作成

rootで作ると権限関係で怒られるっぽい

useradd huboter
su - huboter

アプリ作成

テンプレ作る

mkdir rbot
cd rbot
yo hubot
... (適当に入力)
Bot adapter: (campfire) slack
... (適当に入力) 

スクリプト作成

rbot.coffee
random = (n) -> Math.floor(Math.random() * n)
module.exports = (robot) ->
  robot.hear /@rbot/i, (msg) ->
    words = [
     'なぁに?',
     '見てるぞ',
     'いくら出す?',
     '誠意',
     '誠意だよ誠意'
    ]
    result = words[random(5)]
    msg.send "#{result}"

slack側設定

app - hubot を追加し名前(rbot)などをセット。APIトークンを控えておく。

herokuへ

heroku login
heroku create rbot
heroku git:remote -a rbot
git init
git add .
git commit -m "first commit"
git push heroku master

と、そのままpushしようとしたら下記のエラー

[huboter@localhost rbot]$ git push heroku master
Counting objects: 20, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (20/20), 17.90 KiB | 0 bytes/s, done.
Total 20 (delta 1), 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_VERBOSE=false
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...
remote:        Downloading and installing node 0.10.48...
remote:        Detected package-lock.json: defaulting npm to version 5.x.x
remote:        Bootstrapping npm 5.x.x (replacing 2.15.1)...
remote: 
remote: /tmp/build_869d142be7596a803ffd10a84edac8f4/.heroku/node/lib/node_modules/npm/lib/utils/unsupported.js:27
remote:         console.error(`a bug known to break npm. Please update to at least ${r
remote:                       ^
remote: SyntaxError: Unexpected token ILLEGAL
remote:     at Module._compile (module.js:439:25)
remote:     at Object.Module._extensions..js (module.js:474:10)

なにやら、package.jsonで指定されているnodeとnpmのバージョンを何とかしようとしてエラーになっている模様。
んじゃとりあえず現状の環境のnodeとnpmのバージョン指定しておけばいいんじゃね?と下記の様に修正したら解消。

package.json抜粋(元)
  "engines": {
    "node": "0.10.x"
  }
package.json抜粋(修正版)
  "engines": {
    "node": "8.9.1",
    "npm": "5.5.1"
  }

slackと繋ぐための環境変数を指定する(heroku側のsettingでも良い)

heroku config:set HUBOT_SLACK_TOKEN=(控えておいたAPIトークン)
heroku config:set HUBOT_SLACK_BOTNAME=rbot
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?