aboutsummaryrefslogtreecommitdiffstats
path: root/src/tg.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/tg.ts')
-rw-r--r--src/tg.ts15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/tg.ts b/src/tg.ts
index da8d02d..45c60c7 100644
--- a/src/tg.ts
+++ b/src/tg.ts
@@ -1,12 +1,12 @@
import { Telegraf } from "telegraf";
import config from "./config";
-import { addActiveChat, removeActiveChat } from "./db";
+import { addActiveChat, getActiveChats, removeActiveChat } from "./db";
import { tgMiddleware } from "./middleware";
console.log(`Setting up Telegram bot with token ${config.tgBotToken}`);
const bot = new Telegraf(config.tgBotToken);
-bot.use(tgMiddleware.auth);
+bot.use(tgMiddleware.tgAuth);
bot.command("/start", async (ctx) => {
await addActiveChat(ctx.chat.id);
@@ -18,4 +18,13 @@ bot.command("/stop", async (ctx) => {
await ctx.reply("HommaBot pysäytetty!");
});
-export { bot };
+const broadcastMessage = async (msg: string): Promise<void> => {
+ const activeChats = await getActiveChats();
+ const chatIds = activeChats.map(c => c.id);
+
+ await Promise.all(chatIds.map(async chatId =>
+ bot.telegram.sendMessage(chatId, msg),
+ ));
+};
+
+export { bot, broadcastMessage };