LoginSignup
3
2

More than 3 years have passed since last update.

複数のGoogleカレンダーの予定をLINEに毎朝通知する

Posted at

やりたいこと

複数のGoogleカレンダーの予定をLINEに毎朝通知します.

具体的には,

  • 毎朝
  • 複数のアカウントカレンダーの予定を
  • 当日分
  • LINEのグループに

通知するようにします.

背景

「予定通知をLINEでできたら便利だな」と思い,この記事『家族のGoogleカレンダーの予定をLINEに通知する』を参考にして導入しました.

しかし実際に使ってみると,「週単位じゃなくて当日単位の予定通知の方が欲しい!」となったため,実装してみました.

所要時間

スクリプトもコピペであれば,30分で設定できると思います.

手順

この記事『家族のGoogleカレンダーの予定をLINEに通知する』をご覧ください.

概略だけ記すと,

  1. 1つのGoogleカレンダーで複数アカウントのカレンダーを確認できるようにする.
  2. LINE Notifyの設定をする.
  3. GoogleAppsScriptを利用する.

です.

変更点

家族のGoogleカレンダーの予定をLINEに通知する』と違う部分は,GoogleAppsScriptのコード内容です.

当日分の予定通知の場合は,以下のコードをコピペします.
この際に2点だけ修正をしてください.

  • tokenの設定(1行目)
    • LINE Notifyで作成したアクセストークンを設定します.
  • calendarTitleMapの設定(4行目あたり)
    • 個人アカウント(gmailアドレス)とカレンダーの名称を対応づけます.
daily_calendar.gs
var token = "作成したトークン"; // TODO: ここに発行したアクセストークンを記載

// TODO: ここにカレンダー名を設定
var calendarTitleMap = {
  "hoge@gmail.com" : "hogeの予定",
  "huga@gmail.com": "hugaの予定",
  "ja.japanese#holiday@group.v.calendar.google.com": "日本の祝日",
};

function notifydaily() {

  var calendars = CalendarApp.getAllCalendars();
  var dt = new Date()
  var message = "今日の予定\n\n";
  dt.setDate(dt.getDate());
  message += Utilities.formatDate(dt, 'JST', 'yyyy/MM/dd') + "\n"; 
  var dayText = "";
  for(j in calendars) {
    var calendar = calendars[j];

    var calendarName = calendarTitleMap[calendar.getId()]
    if ( calendarName == undefined ) {
      continue;
    }

    var events = calendar.getEventsForDay(dt);
    if( events.length == 0 ) {
      continue;
    }

    dayText += "< " + calendarName + " >\n";
    for(j in events) {
      dayText += toDayText(events[j]);
    }
    dayText += "\n"
  }

  if ( dayText == "") {
    dayText += "予定はありません\n\n";
  }
  message += dayText;
  sendToLine(message);
}

function sendToLine(message){
  var options =
   {
     "method"  : "post",
     "headers" : {"Authorization" : "Bearer "+ token},
     "payload" : "message=" + message
   };

   UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
}

function toDayText(event) {
  return toTimeText(event.getStartTime()) + ' - ' + toTimeText(event.getEndTime()) + " " + event.getTitle() + '\n';
}

function toTimeText(str){
  return Utilities.formatDate(str, 'JST', 'HH:mm');
}

これ以降の設定も参考記事『家族のGoogleカレンダーの予定をLINEに通知する』通りです.

毎朝通知を導入してみたら,予定ブッチが無くなりました.よかったです.

参考

『家族のGoogleカレンダーの予定をLINEに通知する』
https://qiita.com/monoqlock/items/55bf4860003ab1405a7f

『Googleカレンダーの予定を毎朝LINEに通知する』
https://blog.piyo.tech/posts/2018-04-17-line-notify/

『LINE Notify API Document』
https://notify-bot.line.me/doc/ja/

『Google Apps Script > Calendar Service』
https://developers.google.com/apps-script/reference/calendar/

3
2
1

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
2