0
0

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 3 years have passed since last update.

GAS で Slack に Google Calendar の「予定までの日数」を通知する

0
Posted at

Motivation

  • 何番煎じかは知らないが作ってみたかった
  • これは作業メモです

作業

  1. Slack の Webhook URL を取得
    1. Google Apps Script 作成 & Google Calendar API 有効化

1. Slack の Webhook URL を取得

  • 投げるだけなので Webhook の token を取得する

1.1. app 作成

1.2 webhook URL 取得

  • app 選択
  • サイドメニュー Features/Incoming Webhooks
  • Activate Incoming Webhooks を ON
  • Add New Webhook to Workspace を選択、チャンネルを選択
  • Webhook URL をメモ
    • https://hooks.slack.com/services/T*****/******/**************************

2. Google Apps Script 作成 & Google Calendar API 有効化

2.1 プロジェクト作成

2.2 スクリプト

  • ド初心者なので動けば OK
  • 以下は置き換え
    • WEBHOOK_URL
    • CALENDAR_NAME
    • CALENDAR_ID
    • CALENDAR_THRESHOLD
      • 単位:[日]
main.gs
let webhook_url = "WEBHOOK_URL";

let upcoming_threshold = {
  "CALENDAR_NAME": ["CALENDAR_ID", CALENDAR_THRESHOLD],  
  "CALENDAR_NAME": ["CALENDAR_ID", CALENDAR_THRESHOLD],  
};

function main() {
  let today = new Date();
  let todayMS = today.getTime();
  let upcoming = "------------------------------";

  for (let calendar_name in upcoming_threshold) {
    let v = upcoming_threshold[calendar_name];
    let calendar_id = v[0];
    upcoming = upcoming + "\n" + "*" + calendar_name + "*";

    // day => milliseconds (hour * minutes * seconds * milli)
    threshold = v[1];
    thresholdMS = threshold * (24*60*60*1000);
    console.log("CalendarId: %s, Threshold: %d", calendar_id, thresholdMS);

    const params = {
      timeMin: (new Date()).toISOString(),
      singleEvents: true,
      orderBy: 'startTime'
    };
  
    try {
      const events = Calendar.Events.list(calendar_id, params).items;
    
      for (const event of events) {
        let eventDate = new Date(event.start.date);
        if (event.start.date === undefined) {
          eventDate = new Date(event.start.dateTime.slice(0, 10));
        }
        
        let eventDateMS = eventDate.getTime();
        if ((eventDateMS - todayMS) < thresholdMS) {
          let diffDate = Math.floor((eventDate - today) / 1000 / (24 * 60 * 60));
          upcoming = upcoming + "\n" + "* " + event.summary + " @ " + eventDate.toLocaleDateString("ja-JP") + " (_*+" + diffDate + "*_ days)";
        }
      }
    } catch (err) {
      // ignore
      console.error(err)
    }

    upcoming = upcoming + "\n";
  }

  console.log(upcoming);
  
  let options = {
    "Content-Type": "application/json",
    "method": "post",
    "payload": JSON.stringify({
      "text": upcoming
    })
  }
  UrlFetchApp.fetch(webhook_url, options);
}

2.3 サービス追加

  • サイドバー「サービス」から Google Calendar API
  • 追加

2.4 動作確認

  • 「このアプリは Google で確認されていません」と出たら「詳細」から「****(安全ではないページ)に移動」

2.5 定期実行

  • サイドバー「トリガー」
  • 実行する関数:main
  • 実行するデプロイ:Head
  • イベントのソース:時間主導型
  • 時間ベースのトリガーのタイプ:日付ベースのタイマー
  • 時刻:午前7~8時
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?