アナログ金木犀

つれづれなるまままにつれづれする

WakaTimeで前日のプログラミング時間の集計をGASを使ってslackに通知する

こんにちは。

年末年始に 『さくら荘のペットな彼女』を見て、主人公空太の以下の名言にいまだあてられております、kgmyshinです。

やばいよなあ、本気になるって。

帰ってきて、椎名が連載が決まったって聞いたとき、自分が否定されているような気がした。

心がくじけそうだった。本気だったから。後悔からも、悔しさからも、逃げも隠れも出来なかった。

でも、だから簡単なんだ。やるしかない!

この気持ちをぬぐうにはやるしかない! ダメでもダメでもやるしかない!

そのため、それなりにやる気はあったのですが、以下のツイートが回ってきてハッとしました。

自分は一日に何時間プログラミングをして、何時間本を読んで(インプットに費やして)、何時間アウトプットに費やしているのかということが無性に気になりました。

その中の 何時間プログラミングをして に関しては、年始にWakaTimeを導入することによってうまく集計できてました。

ただ、それでも毎日WakaTimeを見に行くのはしんどい。ということで、今回botを作ってslackに通知することにしました

WakaTimeにはslackインテグレーションが初めから用意されてますが、有料プランでのみ使用できます。ただAPIは無料ユーザーでも使えます。

Google Apps Scripts

はじめはhubotあたりで作ってHerokuやBluemixとかにあげる感じかなぁと思ってたのですが、どうやらGASで無料かつcron的なこともできるようなので下記らへんを参考に触ってみることにしました。

実装

まず WakaTime APIから前日の結果を取得して

  var date = new Date();
  date.setDate(date.getDate() - 1);
  var dateStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
  var apiKey = PropertiesService.getScriptProperties().getProperty("WAKATIME_API_KEY");
  var url = "https://wakatime.com/api/v1/users/@kgmyshin/durations?api_key=" + apiKey + "&date=" + dateStr;
  var urlFetchOption = {
    'method': 'get',
    'contentType' : 'application/json; charset=utf-8',
    'muteHttpExceptions' : true
  };
  var response = UrlFetchApp.fetch(url, urlFetchOption);
  var body = JSON.parse(response.getContentText());
  : 

前日の総プログラミング時間を算出して

  : 
  var body = JSON.parse(response.getContentText());
  
  var totalDuration = 0;
  for each(var item in body.data) {
    totalDuration = totalDuration + item.duration;
  }
  :

Slackにポストする。

  :
  var message = dateStr + "のプログラミングに費やした時間は " + Math.floor((totalDuration / 3600)) + "時間" + Math.floor(((totalDuration % 3600) / 60)) + "分 でした。"
  
  var token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
  var slackApp = SlackApp.create(token);
  slackApp.postMessage("#general", message, {username: "えま", icon_url: "http://i.imgur.com/TdUStmi.png"});

下記がコードの全容です。(25行)

function postSlackMessage() {
  var date = new Date();
  date.setDate(date.getDate() - 1);
  var dateStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
  var apiKey = PropertiesService.getScriptProperties().getProperty("WAKATIME_API_KEY");
  var url = "https://wakatime.com/api/v1/users/@kgmyshin/durations?api_key=" + apiKey + "&date=" + dateStr;
  var urlFetchOption = {
    'method': 'get',
    'contentType' : 'application/json; charset=utf-8',
    'muteHttpExceptions' : true
  };
  var response = UrlFetchApp.fetch(url, urlFetchOption);
  var body = JSON.parse(response.getContentText());
  
  var totalDuration = 0;
  for each(var item in body.data) {
    totalDuration = totalDuration + item.duration;
  }
  
  var message = dateStr + "のプログラミングに費やした時間は " + Math.floor((totalDuration / 3600)) + "時間" + Math.floor(((totalDuration % 3600) / 60)) + "分 でした。"
  
  var token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
  var slackApp = SlackApp.create(token);
  slackApp.postMessage("#general", message, {username: "えま", icon_url: "http://i.imgur.com/TdUStmi.png"});
}

これで下記のようにSlackに通知してくれます。

f:id:kgmyshin:20170111153448p:plain

Triggerの設定

Resources -> All your triggers を選択し、下記のように設定することで、毎日深夜1時に送信してくれるようになります。

f:id:kgmyshin:20170111154140p:plain

所感

楽。