61
58

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.

dockerでhubot + slack

Last updated at Posted at 2014-09-30

Slack 用の bot を Hubotdocker を使ってつくったのでメモ

Dockerfile

Dockerfile
FROM node:latest

MAINTAINER nabe

RUN npm install -g hubot coffee-script
RUN npm install hubot-slack --save

RUN hubot --create bot

ADD hubot-scripts.json /bot/hubot-scripts.json
ADD package.json /bot/package.json
ADD scripts/hello.coffee /bot/scripts/

ENV REDIS_URL redis://localhost:6379

ENV HUBOT_NAME @bot
ENV PORT 9999

ENV HUBOT_SLACK_TOKEN <yourtoken>
ENV HUBOT_SLACK_TEAM <yourteam>
ENV HUBOT_SLACK_BOTNAME bot
ENV HUBOT_SLACK_CHANNELMODE whitelist
ENV HUBOT_SLACK_CHANNELS test

EXPOSE 9999

CMD cd /bot; bin/hubot --adapter slack

HUBOT_SLACK_TOKEN は Slack から取得します

ちなみに、HUBOT_SLACK_CHANNELMODE で whitelist を指定しているので、HUBOT_SLACK_CHANNELS で設定した #test チャンネルしか受け付けなくなっています

設定ファイル

Dockerfile と同じディレクトリに作成します

hubot-scripts.json
["redis-brain.coffee"]
package.json
{
  "name": "hosted-hubot",
  "version": "2.7.1",
  "private": true,

  "author": "GitHub Inc.",

  "keywords": [
    "github",
    "hubot",
    "campfire",
    "bot"
  ],

  "description": "A simple helpful robot for your Company",

  "licenses": [{
    "type": "MIT",
    "url": "https://github.com/github/hubot/raw/master/LICENSE"
  }],

  "repository" : {
    "type": "git",
    "url": "https://github.com/github/hubot.git"
  },

  "dependencies": {
    "hubot":         ">= 2.6.0 < 3.0.0",
    "hubot-scripts": ">= 2.5.0 < 3.0.0",
    "hubot-slack":   ">= 2.2.0"
  },

  "engines": {
    "node": ">= 0.8.x",
    "npm":  ">= 1.1.x"
  }
}

scripts

Dockerfile のあるディレクトリに scripts/ を作成して、テスト用のスクリプトを設置します

hello.coffee
# Description:
#   Test
#
# Commands:
#   hubot hello - Say "Hi"

module.exports = (robot) ->
  robot.respond /HELLO$/i, (msg) ->
    # robot.brain.set msg.message.user.name, "test"
    msg.send "Hi"

動かす

あとは、Dockerfile のあるディレクトリで以下を実行すればコンテナが起動して Slack のメッセージに反応するようになります

docker build -t bot .
docker run -d -p 9999:9999 --env REDIS_URL=redis://<host>:6379 bot

Redis の URL を指定すれば、メッセージを保存したりできます(コメントアウトしているやつを参考に)

Redis 使わない場合は Dockerfile などをよしなに修正してください

ちなみに、Redis も docker を使えば簡単に用意できます

docker run -d -p 6379:6379 -v <dir> redis redis-server --appendonly yes
61
58
1

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
61
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?