LoginSignup
10
2

More than 1 year has passed since last update.

はじめに

LINE Notifyを使用してスケジュールを通知するBOTを作成しました。
IMG_5272_1.PNG

GoogleAppsScript(GAS)にて1日1回通知プログラムを実行します。そのプログラムでは、Googleカレンダーからその日のスケジュールを取得して、LINE Notifyに通知します。
SoWkIImgAStDuNBFpq_FINKiA2WEJYxAB2XHqBLJG1UpKZ1yDdsfuTFPyrvitVwqUofNOd99PbbYIIg6IYu51Mm6FkrTyBYnvMLhxvFz2wqKt5r35FGpyeloYvFp2rA1Mc36eQW3YcCnp4PN5zKqAEzPS-lJ1Hk1Ub9IiPg6ms5AFfv-hWf--IMPQPLALWfwUde1_i50r-M1HRXIKd7x52Q.png

LINE Notify

GoogleAppsScript

  1. GoogleAppsScriptを開いて、以下のスクリプトを実装します。LINE_NOTIFY_TOKENにLINE Notifyで発行したトークンを指定する。
main.gs
/**
 * スケジュール通知BOT
 */
const LINE_NOTIFY_TOKEN = '*****'; // LINE NOTIFY用のアクセストークン 
const WEEKDAY = ["", "", "", "", "", "", ""];

/**
 * メイン処理
 */
function main() {
    try {
        let nowDt = new Date();
        let dt = Utilities.formatDate(nowDt, 'Asia/Tokyo', `MM/dd(${WEEKDAY[nowDt.getDay()]})`);
        let message = `\n今日の予定だよ!!\n\n--- ${dt} ----\n`;

        let userMessage = '';
        let calendarList = CalendarApp.getAllCalendars();
        for (let i in calendarList) {
            let calendar = calendarList[i];
            let eventMessage = '';
            let eventList = calendar.getEventsForDay(nowDt);
            for (let j in eventList) {
                let event = eventList[j];
                eventMessage += `${getEventTime(event.getStartTime())} - ${getEventTime(event.getEndTime())} ${event.getTitle()}\n`;
            }
            if (0 < eventMessage.length) {
                userMessage += `\n< ${calendar.getName()} >\n`;
                userMessage += eventMessage;
            }
        }
        if (0 < userMessage.length) {
            message += userMessage;
        } else {
            message += '今日の予定はありません。';
        }
        sendLineNotify(message);

    } catch (e) {
        console.error(e.stack);
    }
}

/**
 * イベント時刻を取得する
 * @param {String} str 
 */
function getEventTime(str) {
    return Utilities.formatDate(str, 'Asia/Tokyo', 'HH:mm');
}

/**
 * LINEにメッセージを送信する
 * @param {String} message メッセージ 
 */
function sendLineNotify(message) {
    let url = 'https://notify-api.line.me/api/notify';
    let options = {
        'method': 'post',
        'headers': {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Authorization': `Bearer ${LINE_NOTIFY_TOKEN}`
        },
        'payload': `message=${message}`
    };
    let response = UrlFetchApp.fetch(url, options);
    return JSON.parse(response.getContentText('UTF-8'));
}

CalendarApp.getAllCalendars()でGoogleカレンダーの情報を取得することができます。calendar.getEventsForDay()でカレンダーの予定を取得することができます。

参考リンク

さいごに

ソースコードをGitHubに公開しています。

以上です。

10
2
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
10
2