skip to content
barorin&

GASでカウントダウン通知をSlackに投げてみよう

/ 2 min read

Table of Contents

はじめに

退職まであと何日か?名言を添えて教えてくれるスクリプトです。
あとはメイン関数を定期的に実行させるだけ!

内容

// メイン関数
function Countdown() {
// カウントダウンデータを取得
const json = fetchData();
const parsedJson = JSON.parse(json);
// 今日の日付を取得
let today = new Date();
today = Utilities.formatDate(today, "JST", "yyyy-MM-dd");
// 今日の日付に合致するデータを取得
const matchingQuote = parsedJson.filter((entry) => entry.date === today);
if (matchingQuote) {
const message = `退職まであと${matchingQuote[0].countdown}日!
${matchingQuote[0].famous_quote}
by ${matchingQuote[0].author}`;
// Slackに通知
// https://barorin-to.com/posts/gas-to-slack-function/ の記事を参照
sendSlackMessage(
"https://hooks.slack.com/services/hogehoge",
"退職まであとn日Bot",
":partying_face:",
message,
);
}
}
// カウントダウンデータ
function fetchData() {
// 実際はエクセルで作ってChatGPTにJSON化してもらいました。
const data = [
{
date: "2024-02-28",
countdown: "100",
famous_quote: "You will be singled out for promotion in your work.",
author: "cow",
},
// あとは好きなだけカウントダウンする
];
return JSON.stringify(data);
}

この記事を書いた人

barorinのプロフィール画像

barorin公認会計士

会計とITの二足のわらじで働く公認会計士です。Pythonを中心に、会計・監査の実務で実際に使っているコードや、Ubuntu・Raspberry Piの設定メモを書いています。