LoginSignup
2
1

More than 5 years have passed since last update.

BacklogAPIとHubotでSlackに課題通知を送る

Last updated at Posted at 2015-12-27

前回
http://qiita.com/manabukk/private/158a2dd00ffc36bf5b98

気づいたこと

new jobで追加したjobは一度Sleepしても保持される。
(11/25)

外部サービスとの連携

Backlog API V2 を使って、課題を Slack に連携させる
http://qiita.com/matsuoshi/items/eb8479df3c10cf43269a
を参考に(ていうか丸コピ)してbacklogの更新を取得する。

ローカルで取得には成功していることを確認した。
herokuにpushしてみて様子を見る。

あと、
Hubot導入とhubot-script作成までやってみる
http://qiita.com/katsew/items/ce5795f2a945e7a46e59

Hubot:概要〜インストール〜実用的なモノが作れるまで
http://qiita.com/Kta-M/items/d7e0f371e40b4cefc38a

あたりを参考にcoffee scriptの勉強をしようかな。
(12/10)

BacklogAPI

Heroku上では動作してくれなかった...
どうやらcronモジュールがうまく動作していないようなので、ネット上のサイトをいくつかあたってみたところどうやら文法が間違っていたみたい。

Hubotで定期タスクを動かす
http://qiita.com/mats116/items/0164b37ffaa90f03f2a0

最終的には以下の様に実装した。

backlog.coffee

# Description:#   Backlog to Slack## Commands:#   None

cron = require('cron').CronJob

intervalMinutes = 10
buf_time = intervalMinutes*60*1000
backlogUrl = 'https://****.backlog.jp/'
apiKey = '****'
userName = '****'

module.exports = (robot) ->
    new cron "*/#{intervalMinutes} * * * *", () =>
        console.log "BacklogAPI start..."

        # APIアクセス
        robot.http("#{backlogUrl}api/v2/space/activities?apiKey=#{apiKey}")
        .get() (err, res, body) ->

            # 前回cron実行時刻
            now = new Date().currentTime.getHours()
            if 3 < now and now < 9
                buf_time = 9*60*60*1000
            else
                buf_time = intervalMinutes*60*1000
            lastTime = new Date - buf_time


            for feed in JSON.parse body
                # とりあえず出力
                console.log "since : #{lastTime}"
                # console.log "feed : #{feed}"

                # 前回cron実行時刻より古いものは処理しない
                if new Date(feed.created) < lastTime
                    continue

                console.log "feed created User : #{feed.createdUser.name}"
                if feed.createdUser.name == "#{userName}"
                    console.log "...skip send"
                    continue

                switch feed.type
                    when 1
                        label = '課題追加'
                    when 2
                        label = '課題更新'
                    when 3
                        label = '課題コメント'
                    else
                        # 課題関連以外はスルー
                        continue

                # 投稿メッセージを整形
                url = "#{backlogUrl}view/#{feed.project.projectKey}-#{feed.content.key_id}"
                if feed.content.comment?.id?
                    url += "#comment-#{feed.content.comment.id}"
                message = "[#{feed.project.projectKey}] #{label} - #{feed.content.summary} by #{feed.createdUser.name} #{url}"

                # Slack に投稿
                robot.send {room: '#backlog'}, message
    , null, true, "Asia/Tokyo"

あと、Coffee scriptの参考サイトとしては
http://memo.sappari.org/coffeescript/coffeescript-langref
とか見たらいいかも。
(12/11)

次回

2
1
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
2
1