6
6

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.

MeshbluでオープンソースのIoTをはじめよう - Part4: Hubot with Slack on Docker

Posted at

これまでにRaspberry Piと環境センサーから計測したデータをMeshbluのMQTTブローカーにpublishしたあとはfreeboardのダッシュボードに表示しました。今回はHubotSlackを使ってインタラクティブなインタフェースを追加しようと思います。SlackはモダンなUIでWebhookや他のWebサービスとのインテグレーションがしやすいので便利に使えます。

HubotとSlackの設定

SlackのIntegrationページhttps://{teamdomain}.slack.com/services/newからHubotを選びAPI Tokenを作成しておきます。現在ではSlackとHubotのインテグレーションに必要な情報はこのAPI Tokenだけになりとても簡単です。

hubot-avodado.png

botには名前を付けてアイコンを変更することができます。今回はavodadoと名付けました。

プロジェクト

今回作成したプロジェクトのディレクトリ構成です。リポジトリはこちらです。

$ cd ~/node_apps
$ git clone https://github.com/IDCFChannel/docker-hubot-slack
$ cd docker-hubot-slack
$ tree
.
├── Dockerfile
├── README.md
├── docker-compose.yml
├── docker-compose.yml.default
├── redis
└── scripts
    └── hello.coffee

Dockerfile

ベースイメージはオフィシャルのio.jsを使います。Hubotの動作に必要な基本的なnpmはグルーバルにrootでインストールします。Dockerイメージの中にbotプロジェクトをYeomanで作成しておきます。コンテナのscriptsディレクトリはDockerホストのディレクトリをマウントして使います。

今回は単純にscriptsディレクトリ配下に直接CoffeeScriptのファイルを配置するだけで、external-scripts.jsonは使いません。

~/node_apps/docker-hubot-slack/scripts/Dockerfile
FROM iojs:2.3
MAINTAINER Masato Shimizu <ma6ato@gmail.com>

RUN mkdir -p /app
WORKDIR /app

RUN adduser --disabled-password --gecos '' --uid 1000 docker && \
    chown -R docker:docker /app

RUN npm install -g hubot coffee-script yo generator-hubot

USER docker
RUN yes | yo hubot --defaults && \
    npm install --save hubot-slack mqtt

docker-compose.yml

docker-compose.yml.defaultをリネームして使います。

$ mv docker-compose.yml.default docker-compose.yml

Dockerイメージの/appディレクトリにはYeomanを使ってbotを作ってあります。scriptsディレクトリをDockerホストから編集できるようにマウントします。環境変数にはテスト用にHUBOT_LOG_LEVELをデバッグレベルにして、HUBOT_SLACK_TOKENにはSlackのIntegrationページで取得したHubotのAPI Tokenを記入します。

また、Hubotの動作にはRedisが必要になるのでredisサービスを追加してnpmサービスにlinkします。

~/node_apps/docker-hubot-slack/scripts/docker-compose.yml
npm:
  build: .
  volumes:
    - ./scripts:/app/scripts
    - /etc/localtime:/etc/localtime:ro
  ports:
    - 8089:8089
  environment:
    - PORT=8089
    - REDIS_URL=redis://redis:6379
    - HUBOT_LOG_LEVEL=debug
    - HUBOT_SLACK_TOKEN=xxx
  links:
    - redis
  command: ./bin/hubot -a slack
redis:
  image: redis
  volumes:
    - ./redis:/data
    - /etc/localtime:/etc/localtime:ro

使い方

ビルドと実行

Dockerイメージをビルドします。npmサービスをビルドしてupします。

$ cd ~/node_apps/docker-hubot-slack
$ docker-compose build npm
$ docker-compose up npm

scripts/hello.coffee

helloとbotに発言すると、Hiと返答してくる単純なサンプルです。今回はこのhelloサンプルのbotスクリプトを実行してみます。

~/node_apps/docker-hubot-slack/scripts/hello.coffee
module.exports = (robot) ->
  robot.respond /HELLO$/i, (res) ->
    res.reply "Hi"

Slackにログインして、avocado helloと発言すると、masato: Hiと返答してくれます。

hello-hi.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?