From b90d30316ed81c2a88ecb3bf31772ec4288eac63 Mon Sep 17 00:00:00 2001 From: John Costa Date: Thu, 10 Jul 2025 09:50:50 +0100 Subject: [PATCH] feat: whitelist --- src/index.ts | 5 ++++- src/telegram/index.ts | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 21e5e72..df79a15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ import { setupTelegram } from "./telegram"; -setupTelegram({ bossChatId: "1502730007" }); +setupTelegram({ + bossChatId: "1502730007", + whitelist: new Set(["Johncosta27"]), +}); diff --git a/src/telegram/index.ts b/src/telegram/index.ts index 86666c7..eaffe5a 100644 --- a/src/telegram/index.ts +++ b/src/telegram/index.ts @@ -42,9 +42,10 @@ const createChat = () => { type TelegramOptions = { bossChatId: string; + whitelist: Set; }; -export const setupTelegram = ({ bossChatId }: TelegramOptions) => { +export const setupTelegram = ({ bossChatId, whitelist }: TelegramOptions) => { console.log("Setting up telegram bot"); const bot = new TelegramBot(process.env.TELEGRAM_TOKEN ?? "", { @@ -53,16 +54,33 @@ export const setupTelegram = ({ bossChatId }: TelegramOptions) => { const chats = createChat(); - bot.on("message", async (msg) => { + bot.on("message", async (msg, meta) => { + console.log(msg); + + if (meta.type !== "text") { + // TODO: gracefully handle messages like this. + return; + } + + const fromUsername = msg.from?.username; + if (fromUsername == null) { + console.log("Cannot handle a message without a user."); + return; + } + const text = msg.text; if (text == null) { - console.log("Cannot handle messages without text"); + console.log("Cannot handle messages without text."); + return; + } + + if (!whitelist.has(fromUsername)) { + bot.sendMessage(msg.chat.id, "Sorry, you are not part of the whitelist."); return; } chats.addMessage(msg.chat.id, text); - // TODO: make BOT only respond to Rio. const messages = await getResponse( { async createTasks({ response, name, date }) {