diff --git a/functions/.env.local.test b/functions/.env.local.test index ddd6408f..541462fd 100644 --- a/functions/.env.local.test +++ b/functions/.env.local.test @@ -4,8 +4,6 @@ GRAPH_API_VERSION=v15.0 TEST_SERVER_URL=http://resultserver:12345 ENVIRONMENT=SIT SPREADSHEET_ID=SPREADSHEET_ID -WHATSAPP_TEST_USER_BOT_PHONE_NUMBER_ID=WHATSAPP_TEST_USER_BOT_PHONE_NUMBER_ID -WHATSAPP_TEST_CHECKER_BOT_PHONE_NUMBER_ID=WHATSAPP_TEST_CHECKER_BOT_PHONE_NUMBER_ID CHECKER1_PHONE_NUMBER=6511111111 SIMILARITY_THRESHOLD=0.85 TYPESENSE_HOST=typesense-server @@ -14,6 +12,4 @@ TYPESENSE_PROTOCOL=http EMBEDDER_HOST=http://embedder-server:12344 TELEGRAM_REPORT_CHANNEL_ID=111111111 TEST_IMAGE_URL=TEST -HASHIDS_SALT=SALT -WHATSAPP_TEST_USERS_WABA_ID=WHATSAPP_TEST_USER_WABA_ID -WHATSAPP_TEST_CHECKERS_WABA_ID=WHATSAPP_TEST_CHECKER_WABA_ID \ No newline at end of file +HASHIDS_SALT=SALT \ No newline at end of file diff --git a/functions/.secret.local.test b/functions/.secret.local.test index 46910414..82cfc551 100644 --- a/functions/.secret.local.test +++ b/functions/.secret.local.test @@ -1,5 +1,5 @@ -WHATSAPP_USER_BOT_PHONE_NUMBER_ID=WHATSAPP_USER_BOT_PHONE_NUMBER_ID -WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID=WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID +WHATSAPP_USER_BOT_PHONE_NUMBER_ID=WHATSAPP_TEST_USER_BOT_PHONE_NUMBER_ID +WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID=WHATSAPP_TEST_CHECKER_BOT_PHONE_NUMBER_ID WHATSAPP_TOKEN=WHATSAPP_TOKEN TELEGRAM_CHECKER_BOT_TOKEN=TELEGRAM_CHECKER_BOT_TOKEN TELEGRAM_REPORT_BOT_TOKEN=TELEGRAM_REPORT_BOT_TOKEN @@ -8,5 +8,5 @@ BITLY_TOKEN=BITLY_TOKEN TYPESENSE_TOKEN=TYPESENSE_TOKEN ML_SERVER_TOKEN=ML_SERVER_TOKEN TELEGRAM_BOT_TOKEN=TELEGRAM_BOT_TOKEN -WHATSAPP_USERS_WABA_ID=WHATSAPP_USERS_WABA_ID -WHATSAPP_CHECKERS_WABA_ID=WHATSAPP_CHECKERS_WABA_ID \ No newline at end of file +WHATSAPP_USERS_WABA_ID=WHATSAPP_TEST_USER_WABA_ID +WHATSAPP_CHECKERS_WABA_ID=WHATSAPP_TEST_CHECKER_WABA_ID \ No newline at end of file diff --git a/functions/src/definitions/common/sendWhatsappMessage.ts b/functions/src/definitions/common/sendWhatsappMessage.ts index 4f9f1a7c..aefffbc0 100644 --- a/functions/src/definitions/common/sendWhatsappMessage.ts +++ b/functions/src/definitions/common/sendWhatsappMessage.ts @@ -4,13 +4,6 @@ import { defineString } from "firebase-functions/params" import { WhatsappButton } from "../../types" const graphApiVersion = defineString("GRAPH_API_VERSION") -const runtimeEnvironment = defineString("ENVIRONMENT") -const testUserPhoneNumberId = defineString( - "WHATSAPP_TEST_USER_BOT_PHONE_NUMBER_ID" -) -const testCheckerPhoneNumberId = defineString( - "WHATSAPP_TEST_CHECKER_BOT_PHONE_NUMBER_ID" -) const graphApiUrl = process.env["TEST_SERVER_URL"] || "https://graph.facebook.com" //only exists in integration test environment @@ -327,18 +320,10 @@ async function markWhatsappMessageAsRead(bot: string, messageId: string) { async function callWhatsappSendMessageApi(data: any, bot: string) { const token = process.env.WHATSAPP_TOKEN let phoneNumberId - if (runtimeEnvironment.value() !== "PROD") { - if (bot == "factChecker") { - phoneNumberId = testCheckerPhoneNumberId.value() - } else { - phoneNumberId = testUserPhoneNumberId.value() - } + if (bot == "factChecker") { + phoneNumberId = process.env.WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID } else { - if (bot == "factChecker") { - phoneNumberId = process.env.WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID - } else { - phoneNumberId = process.env.WHATSAPP_USER_BOT_PHONE_NUMBER_ID - } + phoneNumberId = process.env.WHATSAPP_USER_BOT_PHONE_NUMBER_ID } const response = await axios({ method: "POST", // Required, HTTP method, a string, e.g. POST, GET diff --git a/functions/src/definitions/webhookHandlers/whatsapp.ts b/functions/src/definitions/webhookHandlers/whatsapp.ts index 261fd53e..d88fc2fa 100644 --- a/functions/src/definitions/webhookHandlers/whatsapp.ts +++ b/functions/src/definitions/webhookHandlers/whatsapp.ts @@ -6,17 +6,8 @@ import { handleSpecialCommands } from "./specialCommands" import { publishToTopic } from "../common/pubsub" import { onRequest } from "firebase-functions/v2/https" import { checkMessageId } from "../common/utils" -import { user } from "firebase-functions/v1/auth" const runtimeEnvironment = defineString("ENVIRONMENT") -const testUserPhoneNumberId = defineString( - "WHATSAPP_TEST_USER_BOT_PHONE_NUMBER_ID" -) -const testCheckerPhoneNumberId = defineString( - "WHATSAPP_TEST_CHECKER_BOT_PHONE_NUMBER_ID" -) -const testUserWABAId = defineString("WHATSAPP_TEST_USERS_WABA_ID") -const testCheckerWABAId = defineString("WHATSAPP_TEST_CHECKERS_WABA_ID") if (!admin.apps.length) { admin.initializeApp() @@ -26,73 +17,82 @@ const app = express() // Accepts POST requests at /webhook endpoint app.post("/whatsapp", async (req, res) => { if (req.body.object) { - if ( - req.body.entry && - req.body.entry[0].id && - req.body.entry[0].changes && - req.body.entry[0].changes[0] && - req.body.entry[0].changes[0].value.messages && - req.body.entry[0].changes[0].value.messages[0] - ) { + if (req?.body?.entry?.[0]?.changes?.[0]?.value) { let value = req.body.entry[0].changes[0].value let phoneNumberId = value.metadata.phone_number_id - let message = value.messages[0] - let type = message.type let wabaID = req.body.entry[0].id let checkerPhoneNumberId let userPhoneNumberId let checkerWabaId let userWabaId - if (runtimeEnvironment.value() === "PROD") { - checkerPhoneNumberId = process.env.WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID - userPhoneNumberId = process.env.WHATSAPP_USER_BOT_PHONE_NUMBER_ID - checkerWabaId = process.env.WHATSAPP_CHECKERS_WABA_ID - userWabaId = process.env.WHATSAPP_USERS_WABA_ID - } else { - checkerPhoneNumberId = testCheckerPhoneNumberId.value() - userPhoneNumberId = testUserPhoneNumberId.value() - checkerWabaId = testCheckerWABAId.value() - userWabaId = testUserWABAId.value() - } + checkerPhoneNumberId = process.env.WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID + userPhoneNumberId = process.env.WHATSAPP_USER_BOT_PHONE_NUMBER_ID + checkerWabaId = process.env.WHATSAPP_CHECKERS_WABA_ID + userWabaId = process.env.WHATSAPP_USERS_WABA_ID if ( (phoneNumberId === checkerPhoneNumberId && wabaID === checkerWabaId) || (phoneNumberId === userPhoneNumberId && wabaID === userWabaId) ) { - if ( - type == "text" && - message.text.body.startsWith("/") && - runtimeEnvironment.value() !== "PROD" - ) { - //handle db commands - await handleSpecialCommands(message) - } else { - if (message?.id) { - //if message has been processed before, don't even put it in queue. - if (await checkMessageId(message.id)) { - functions.logger.warn(`message ${message.id} already processed`) + if (value?.messages?.[0]) { + let message = value.messages[0] + let type = message.type + if ( + type == "text" && + message.text.body.startsWith("/") && + runtimeEnvironment.value() !== "PROD" + ) { + //handle db commands + await handleSpecialCommands(message) + } else { + if (message?.id) { + //if message has been processed before, don't even put it in queue. + if (await checkMessageId(message.id)) { + functions.logger.warn(`message ${message.id} already processed`) + res.sendStatus(200) + return + } + } else { + functions.logger.error(`message ${message.id} has no id`) res.sendStatus(200) return } - } else { - functions.logger.error(`message ${message.id} has no id`) - res.sendStatus(200) - return - } - if ( - (type == "button" || type == "interactive" || type == "text") && - phoneNumberId === checkerPhoneNumberId - ) { - //put into checker queue - await publishToTopic("checkerEvents", message) + if ( + (type == "button" || type == "interactive" || type == "text") && + phoneNumberId === checkerPhoneNumberId + ) { + //put into checker queue + await publishToTopic("checkerEvents", message) + } + if (phoneNumberId === userPhoneNumberId) { + //put into user queue + await publishToTopic("userEvents", message) + } } - if (phoneNumberId === userPhoneNumberId) { - //put into user queue - await publishToTopic("userEvents", message) + res.sendStatus(200) + } else if (value?.statuses?.[0]) { + let status = value.statuses[0] + let bot = phoneNumberId === checkerPhoneNumberId ? "checker" : "user" + if (status.status === "failed") { + const errorObj = { + messageId: status.id, + timestamp: status.timestamp, + recipientId: status.recipient_id, + errors: status.errors, + displayPhoneNumber: value.metadata.displayPhoneNumber, + bot: bot, + } + functions.logger.error( + `Error sending message ${status.id} to ${status.recipient_id} from ${bot} bot`, + errorObj + ) } + res.sendStatus(200) + } else { + functions.logger.log(`Not a message or status update`) + res.sendStatus(200) } - res.sendStatus(200) } else { functions.logger.warn( `Unexpected message source from phoneNumberId ${phoneNumberId}`