Skip to content

Commit

Permalink
Merge pull request #413 from bettersg/feat/welcome-on-chat-group-entry
Browse files Browse the repository at this point in the history
Automate welcome message
  • Loading branch information
sarge1989 authored Aug 17, 2024
2 parents b78b3e6 + 0be898e commit c20017a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion functions/src/definitions/webhookHandlers/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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)

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 =
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -285,6 +301,7 @@ const webhookHandlerV2 = onRequest(
"WHATSAPP_USERS_WABA_ID",
"TELEGRAM_WEBHOOK_TOKEN",
"TYPEFORM_SECRET_TOKEN",
"TELEGRAM_ADMIN_BOT_TOKEN",
],
},
app
Expand Down
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 }

0 comments on commit c20017a

Please sign in to comment.