見出し画像

【コード付き】タスクのリマインドメールをNotion×GASで自動化してみた

期日が近いタスクの担当者へリマインドメールを自動送信するロボットです。今回はNotionを使いましたが、スプレッドシートで代替することも可能です。

概要

機能

  • 期日が今日or明日or明後日のタスクの担当者にリマインドメールを始業時に自動送信

    • タイトル:「タスク名」|「期日」

    • 本文:タスクの期日は迫っています。「タスクページへのリンク」

Notionの構成

  • タスクデータベース

    • プロパティ名(プロパティの種類)

      • タスク名(タイトル)

      • 期日(日付)

      • 担当者(リレーション to 社員管理データベース)

      • ステータス(ステータス)

        • Not started

        • Done

  • 社員管理データベース

    • プロパティ

      • 名前

      • 部署

      • メールアドレス

メール送信までの流れ

  1. 始業時にプログラム作動

  2. Notionの「タスクデータベース」から「期日」が今日or明日or明後日のタスクを抽出

  3. タスクごとに以下を実行

    1. 「担当者(リレーション to 社員管理データベース)」からその社員のページを取得

    2. その社員のメールアドレスを取得

    3. タイトルと本文をセットしてリマインドメール送信

ソースコード

notion.gs(こちらのファイルを上に配置してください。)

1行目のtokenにご自身のAPIトークンを入れてください

const token = NOTION_API_TOKEN;
const headers = {
  "Notion-Version": "2022-06-28",
  "Authorization": "Bearer " + token,
  "Content-Type": "application/json"
}

const queryDb = (dbId, query) => {
  const url = "https://api.notion.com/v1/databases/" + dbId + "/query";
  const options = {
    headers: headers,
    method: "post",
    payload: JSON.stringify(query)
  }

  const response = JSON.parse(UrlFetchApp.fetch(url, options));
  return response["results"];
}

const getPage = (pageId) => {
  const url = "https://api.notion.com/v1/pages/" + pageId;
  const options = {
    headers: headers,
    method: "get"
  }

  const response = JSON.parse(UrlFetchApp.fetch(url, options));
  return response;
}

todoReminder.gs

dbidにお使いのデータベースのIDを入れてください。

function todoReminder() {
  const today = new Date();
  const dayAfterTomorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 2);
  const strToday = Utilities.formatDate(today, "JST", "yyyy-MM-dd");
  const strDayAfterTomorrow = Utilities.formatDate(dayAfterTomorrow, "JST", "yyyy-MM-dd");
  const query = {
    "filter": {
      "and":[
        {
          "property": "期日",
          "date": {
            "on_or_before": strDayAfterTomorrow
          }
        },
        {
          "property": "期日",
          "date": {
            "on_or_after": strToday
          }
        },
        {
          "property": "ステータス",
          "status": {
            "equals": "Not started"
          }
        }
      ]
    }
  }
  const dbId = NOTION_DB_ID;
  const taskList = queryDb(dbId, query);
  Logger.log(taskList);

  for (task of taskList) {
    const asignee = task["properties"]["担当者"]["relation"]["id"];
    const asigneePage = getPage(asignee);
    const address = asigneePage["properties"]["メールアドレス"]["email"];
    const taskName = task["properties"]["タスク名"]["title"][0]["text"]["content"];
    const due =  task["properties"]["期日"]["date"]["start"];

    const subject = taskName + "|" + due;
    const body = 'タスクの期日が迫っています\n' + task["url"];

    GmailApp.sendEmail(address, subject, body);
  }
}

動画で実際の流れを見る

カスタマイズも可能

お客様のご要望に沿ったカスタマイズも承ります。
また、その他の業務を自動化することもお手伝いさせていただきます。
ご相談は無料で行っております。下記のお問い合わせフォームから、お気軽にお申し込みください。


いいなと思ったら応援しよう!