diff --git a/functions/src/definitions/eventHandlers/checkerHandlerTelegram.ts b/functions/src/definitions/eventHandlers/checkerHandlerTelegram.ts index d765624f..ea9431cd 100644 --- a/functions/src/definitions/eventHandlers/checkerHandlerTelegram.ts +++ b/functions/src/definitions/eventHandlers/checkerHandlerTelegram.ts @@ -8,7 +8,6 @@ import { getThresholds } from "../common/utils" import { CheckerData } from "../../types" import { message, callbackQuery } from "telegraf/filters" import { isNumeric } from "../common/utils" -import { send } from "process" const TOKEN = String(process.env.TELEGRAM_CHECKER_BOT_TOKEN) const ADMIN_BOT_TOKEN = String(process.env.TELEGRAM_ADMIN_BOT_TOKEN) diff --git a/functions/src/definitions/webhookHandlers/handler.ts b/functions/src/definitions/webhookHandlers/handler.ts index e5763991..ce7ec280 100644 --- a/functions/src/definitions/webhookHandlers/handler.ts +++ b/functions/src/definitions/webhookHandlers/handler.ts @@ -8,6 +8,7 @@ import { publishToTopic } from "../common/pubsub" import { onRequest } from "firebase-functions/v2/https" import { checkMessageId } from "../common/utils" import { Request, Response } from "express" +import { adminBotHandlerTelegram } from "./handlers/adminHandlerTelegram" import { AppEnv } from "../../appEnv" const runtimeEnvironment = defineString(AppEnv.ENVIRONMENT) @@ -15,6 +16,7 @@ const runtimeEnvironment = defineString(AppEnv.ENVIRONMENT) const webhookPathWhatsapp = process.env.WEBHOOK_PATH_WHATSAPP const webhookPathTelegram = process.env.WEBHOOK_PATH_TELEGRAM const webhookPathTypeform = process.env.WEBHOOK_PATH_TYPEFORM +const webhookPathTelegramAdmin = process.env.WEBHOOK_PATH_TELEGRAM_ADMIN const typeformSecretToken = process.env.TYPEFORM_SECRET_TOKEN const typeformURL = process.env.TYPEFORM_URL const ingressSetting = @@ -254,6 +256,20 @@ const postHandlerTypeform = async (req: CustomRequest, res: Response) => { } } +const postHandlerTelegramAdmin = async (req: Request, res: Response) => { + if ( + req.header("x-telegram-bot-api-secret-token") === + process.env.TELEGRAM_WEBHOOK_TOKEN + ) { + await adminBotHandlerTelegram(req.body) + } else { + functions.logger.warn( + "Telegram handler endpoint was called from unexpected source" + ) + } + res.sendStatus(200) +} + const verifySignature = function (receivedSignature: string, payload: string) { const hash = crypto .createHmac("sha256", typeformSecretToken as string) @@ -268,7 +284,7 @@ app.post(`/${webhookPathWhatsapp}`, postHandlerWhatsapp) app.get(`/${webhookPathWhatsapp}`, getHandlerWhatsapp) app.post(`/${webhookPathTelegram}`, postHandlerTelegram) - +app.post(`/${webhookPathTelegramAdmin}`, postHandlerTelegramAdmin) app.post(`/${webhookPathTypeform}`, postHandlerTypeform) // Accepts GET requests at the /webhook endpoint. You need this URL to setup webhook initially. @@ -285,6 +301,7 @@ const webhookHandlerV2 = onRequest( "WHATSAPP_USERS_WABA_ID", "TELEGRAM_WEBHOOK_TOKEN", "TYPEFORM_SECRET_TOKEN", + "TELEGRAM_ADMIN_BOT_TOKEN", ], }, app diff --git a/functions/src/definitions/webhookHandlers/handlers/adminHandlerTelegram.ts b/functions/src/definitions/webhookHandlers/handlers/adminHandlerTelegram.ts new file mode 100644 index 00000000..471f3a18 --- /dev/null +++ b/functions/src/definitions/webhookHandlers/handlers/adminHandlerTelegram.ts @@ -0,0 +1,61 @@ +import * as admin from "firebase-admin" +import { Telegraf } from "telegraf" +import { logger } from "firebase-functions/v2" +import { message } from "telegraf/filters" + +const ADMIN_BOT_TOKEN = String(process.env.TELEGRAM_ADMIN_BOT_TOKEN) +const CHECKERS_CHAT_ID = Number(process.env.CHECKERS_CHAT_ID) +const adminBot = new Telegraf(ADMIN_BOT_TOKEN) + +if (!admin.apps.length) { + admin.initializeApp() +} + +const db = admin.firestore() + +//check when new user joins chat +adminBot.on(message("new_chat_members"), async (ctx) => { + const chatId = ctx.chat.id + if (chatId === CHECKERS_CHAT_ID) { + //may want to check chatID in future + const newMembers = ctx.message.new_chat_members + const messagePromises = newMembers.map(async (member) => { + const checkerQueryRef = db + .collection("checkers") + .where("telegramId", "==", member.id) + const checkerQuerySnapshot = await checkerQueryRef.get() + if (checkerQuerySnapshot.empty) { + logger.log( + `Telegram username ${member.id} not found in checkers collection` + ) + return + } + const name = checkerQuerySnapshot.docs[0].get("name") ?? "Checker" + const username = member.username ? ` @${member.username}` : "" + const message = `Hi ${name}${username}, + +Welcome to the CheckMate comms channel 👋 + +This channel will be used to: +1. Inform CheckMates when updates/improvements are deployed to the bots + +2. Inform CheckMates of any downtime in the system + +3. Share relevant links from reputable news sources to aid fact checking. Note: Beyond this, CheckMates should not discuss what scores to assign, as this may make the collective outcome of CheckMates' votes biased. + +4. You can refer to bit.ly/checkmates-wiki for more tips on safe checking 😊 + +If you've any feedback or queries, you can share them in the chat too 🤗` + + return ctx.reply(message) + }) + await Promise.all(messagePromises) + } +}) + +// FIREBASE FUNCTIONS +const adminBotHandlerTelegram = async function (body: any) { + await adminBot.handleUpdate(body) +} + +export { adminBotHandlerTelegram }