Skip to content

Commit

Permalink
feat: add trash reminder
Browse files Browse the repository at this point in the history
  • Loading branch information
ematala committed Apr 27, 2023
1 parent df0566a commit 91d63e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TRASHID = 3;
26 changes: 20 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "dotenv/config";
import { Telegraf } from "telegraf";
import { message } from "telegraf/filters";
import { schedule } from "node-cron";
import { mapping } from "./data";
import { getTrash } from "./utils";
import { TRASHID } from "./constants";

if (!process.env.TELEGRAM_BOT_TOKEN)
throw new Error("TELEGRAM_BOT_TOKEN must be provided!");
Expand All @@ -11,7 +12,7 @@ const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);

bot.command("start", (ctx) =>
ctx.reply(
`Hi ${ctx.chat.type === "private" ? ctx.chat.first_name : "stranger"}`
`Hi ${ctx.chat.type === "private" && (ctx.chat.first_name ?? "stranger")}`
)
);

Expand Down Expand Up @@ -57,17 +58,30 @@ const remind = () =>
)
);

const remindTrash = () => {
const { roomie, done } = mapping.find(({ duty }) => duty.id === TRASHID)!;
if (done) return;
const message = [
"Hey, du musst den Müll rausbringen: \n",
...getTrash(),
].join("\n");
bot.telegram.sendMessage(roomie.id, message);
};

bot.command("remind", async (ctx) => {
await remind();
ctx.reply("Ich habe die anderen daran erinnert ihre Dienste zu machen.");
});
bot.on(message("text"), (ctx) => {});

// send out reminders every sunday at 6pm
schedule("0 18 * * sun", remind);

// send out trash reminders every tuesday at 6pm
schedule("0 18 * * tue", remindTrash);

// launch bot
bot.launch();

// enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));

// send out reminders every sunday at 6pm
schedule("0 18 * * sun", remind);

0 comments on commit 91d63e5

Please sign in to comment.