aboutsummaryrefslogtreecommitdiffstats
path: root/src/tg.ts
blob: 45c60c7502e7975d7f8493bc2ae718493882e877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Telegraf } from "telegraf";
import config from "./config";
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.tgAuth);

bot.command("/start", async (ctx) => {
  await addActiveChat(ctx.chat.id);
  await ctx.reply("HommaBot startattu. Uusia hommapäivityksiä viikon välein.");
});

bot.command("/stop", async (ctx) => {
  await removeActiveChat(ctx.chat.id);
  await ctx.reply("HommaBot pysäytetty!");
});

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 };