はじめに
Slackでまとめて当日の予定を送っていたのだが、使用していたGoogle Calendarアプリが2024/09/01で終了するらしいということを知った。
で、新しいGoogle Calendarでやり方を色々調べたがどうやら当日の予定を送る方法がみつからなかったので作った
GASのコード
※わかる人向けにとりあえずコード
/** * @param {string} calendarID * @param {Date} targetDate */ function getEvents_(calendarID, targetDate) { const events = CalendarApp.getCalendarById(calendarID).getEventsForDay(targetDate) return events } function createToday_() { const now = new Date() return new Date(now.getFullYear(), now.getMonth(), now.getDate()) } /** * @param {Date} targetDate */ function convertHHMM_(targetDate) { return Utilities.formatDate(targetDate, Session.getScriptTimeZone(), "HH:mm"); } /** * @param {CalendarApp.CalendarEvent[]} events */ function convertEvents_(events) { return events.map((event)=> `${event.getTitle()}: ${convertHHMM_(event.getStartTime())} - ${convertHHMM_(event.getEndTime())}`) } /** * @param {string} url * @param {string} title * @param {string[]} messages */ function sendSlack_(url, title, messages) { const sendMessage = `${title}\n\n ${messages.join("\n")}` const json = { "text": sendMessage }; const options = { "method": "post", "contentType": "application/json", "payload": JSON.stringify(json) }; UrlFetchApp.fetch(url, options); } function main() { // スクリプトプロパティに必要な情報を設定していること // https://developers.google.com/apps-script/guides/properties?hl=ja const scriptProperties = PropertiesService.getScriptProperties(); const slackWebhookUrl = scriptProperties.getProperty("SLACK_WEB_HOOK_URL"); const calendarID = scriptProperties.getProperty("GOOGLE_CALENDAR_ID"); const events = getEvents_(calendarID, createToday_()) const convertedEvents = convertEvents_(events) const title = convertedEvents.length ? "■本日の予定" : "■本日の予定はありません" sendSlack_(slackWebhookUrl, title, convertedEvents) }
手順概要
必要なもの
- 通知したいカレンダーのID
手順
- Slackでアプリ作る
- WebhookのURLを取得
- カレンダーのIDを取得
- GASを新規作成
- スクリプトプロパティにWebhookのURLとカレンダーIDを追加
- ↑のコードをGASに張り付ける
- mainを実行して動作確認
- 毎日実行するようにトリガーを設定
手順詳細
※雑に記載
Slackでアプリ作る、WebhookのURLを取得
アプリ作る api.slack.com
Bot User作る
- Incoming Webhooks を有効化してURLを作成
カレンダーのIDを取得
- Googleカレンダーを開く
- 通知したいカレンダー右クリック → 設定と共有
- 下の方にカレンダーIDとして表示されている
xxxxxx@group.calendar.google.com
GAS
- Googleドライブ → 新規 → その他 → Google Apps Script
- 直接開けるURLあった気がしますが見つからず
- ⚙️ → スクリプトプロパティ → 「SLACK_WEB_HOOK_URL」、「GOOGLE_CALENDAR_ID」を追加
- コードを張り付け
- mainを実行して動作確認
- ⏰→ トリガーを追加
- 毎日実行するようにトリガーを設定