使用Docker构建hubot + slack

由于使用Hubot在docker上构建了一个用于Slack的机器人,所以有以下备忘录。

Dockerfile 的中文释义:Docker 文件

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中指定了白名单列表,只有在HUBOT_SLACK_CHANNELS中设置的#test频道才被接受。

设定文件

在与Dockerfile相同的目录下创建

["redis-brain.coffee"]
{
  "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"
  }
}

脚本 (ju3 ben3)

在拥有Dockerfile的目录中创建一个名为scripts/的文件夹,并将测试脚本放置其中。

# 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和其他文件。

顺便提一下,如果使用Docker,Redis也可以轻松准备好。

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