LoginSignup
10
10

More than 5 years have passed since last update.

hubot から、cloudbit に信号を送るサンプル

Last updated at Posted at 2014-11-01

IoT なデバイス、littleBitscloudbit を利用して、ChatOps 的なことをやってみます。

てことで

  1. slack で何かコマンドを入れる
  2. hubot に処理が渡る
  3. hubot から cloudbit へ信号を送る

というのを試してみました。

準備

  • slack と hubot を連携できる環境を作っておく
  • cloudbit のセットアップを済ませておく
    (設定はここから → http://control.littlebitscloud.cc/)
  • cloudbit の Device ID と AccessToken を取得しておく (cloudbit設定画面の "SETTING" メニュー内にあります)

サンプルコード

hubot にこんな感じのコードを追加

_DEVICE_ID = 'cloudbitのデバイスID'
_ACCESS_TOKEN = 'cloudbitのアクセストークン'

module.exports = (robot) ->
  robot.respond /cloudbit test/i, (msg) ->
    data = JSON.stringify
      percent: 100 # 信号の強さ
      duration_ms: 1000 # 何ミリ秒信号を送るか

    robot.http("https://api-http.littlebitscloud.cc/devices/#{_DEVICE_ID}/output")
    .header('Authorization', "Bearer #{_ACCESS_TOKEN}")
    .header('Accept', 'application/vnd.littlebits.v2+json')
    .header('Content-Type', 'application/json')
    .post(data) (err, res, body) ->
      msg.send 'ピコーン'

post するデータの percent や duration_ms は、信号の強さと長さの指定です。適当な数字にしときます。

試す

slack で

hubot cloudbit test

と入力すれば、slack 〜 hubot 経由で cloudbit に信号が送られます。

Jenkins とか New Relic から slack にアラートが流れる → cloudbit 経由で音が鳴り響く! とかできるんじゃないかなー

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