LoginSignup
3
1

More than 3 years have passed since last update.

【分報】Togglで現在進行中のタスクをGASで取得してSlackに送信する

Posted at

何をやるか

在宅勤務で何してるかわからないってならないために、Slackで分報を始めてみました。

どうせやるなら振り返りがやりやすいようにしたいのでTogglでレコーディングしながら
その内容をSlackに流すようにします。

Slackの分報についてはこちらを
社内にSlack上での分報を導入しないかと提案してみた

Togglはタイムトラッキングツールですが、概要はこちらを見てみてください。
時間管理が苦手な人にもおすすめ!「Toggl」の使い方まとめ。チームの状況把握にも活用!

概要

  1. Togglにて今やってる事を計測
  2. GASからスケジュール機能でToggl APIを利用して現在進行中のタスクを取得
  3. SpreadsheetのCurrentシートから前回取得したタスクのIDを比較する
  4. 前回と変わってたらSlackのincoming webhookで自分の分報チャンネルに投稿

準備

  1. TogglからAPIキーを取得
  2. Slackから自分の分報チャンネルのincoming webhookのURLを取得
  3. 新しくSpreadsheetを作成し Current という名前のシートを作成
  4. スクリプトエディタを開き、以下のソースコードを貼り、APIキーとWebHookを適時変更
  5. タイマーアイコンから適切な時間を選んでcurrentを実行

ソースコード


var Toggl = {
  BASIC_AUTH: '【ご自身のAPIキー】:api_token',

  get: function(path){
    var url = 'https://www.toggl.com/api/v8' + path;
    var options = {
      'method' : 'GET',
      'headers': {"Authorization" : "Basic " + Utilities.base64Encode(this.BASIC_AUTH)}
    }
    var response = UrlFetchApp.fetch(url, options);
    return JSON.parse(response);
  },
  getTimeEntries: function(){
    var path = '/time_entries'
    return this.get(path)
  },
  getTimeEntry: function(id) {
    var path = '/time_entries/' + id
    return this.get(path);
  },
  getTimeEntryCurrent: function(id) {
    var path = '/time_entries/current'
    return this.get(path);
  },
  getProject: function(id) {
    var path = '/projects/' + id
    return this.get(path);
  }
}

var Slack = {  
  post: function(message){
    var url = '【ご自身の分報チャンネルのincoming webhookのURL】';
    var options = {
      'method': 'POST',
      'contentType': 'application/json',
      'payload': JSON.stringify({'text': message})
    }
    var response = UrlFetchApp.fetch(url, options);
    return response;
  },
}

function current() {
  var currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Current");
  var current = Toggl.getTimeEntryCurrent()['data']
  if (current == null) {
    return
  }

  var currentId = currentSheet.getRange(2, 1).getValue()
  var projectName = current.pid ? Toggl.getProject(current.pid).data.name : "未分類"

  if (currentId != current['id']) {
    currentSheet.getRange(2, 1, 1, 2)
      .setValues([[current['id'], current['description']]])
    Slack.post(formatTextCurrent(projectName, current))
  }
}


function formatTextCurrent(projectName, timeEntry) {
  return '' + projectName + '】 *' + timeEntry.description + '*'
}

参考にしたQiita

時間管理ツール【Toggl】を【GAS】と連携して【Slack】に分報を通知してみた。

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