-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
functions/src/definitions/webhookHandlers/handlers/adminHandlerTelegram.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |