From 7f3a70c396b498a8b10553d11cf78c3ea9a872b1 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 19 Apr 2021 14:51:34 +0300 Subject: Should be ready now --- src/tg.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/tg.ts') 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 => { + 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 }; -- cgit v1.3