Skip to content

Commit

Permalink
Merge pull request #175 from CheckMateSG/174-possibility-of-messages-…
Browse files Browse the repository at this point in the history
…being-sent-but-not-delivered

174 possibility of messages being sent but not delivered
  • Loading branch information
sarge1989 authored Oct 22, 2023
2 parents 1036cea + 63c2f24 commit b021b1b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 84 deletions.
6 changes: 1 addition & 5 deletions functions/.env.local.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
HASHIDS_SALT=SALT
8 changes: 4 additions & 4 deletions functions/.secret.local.test
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
WHATSAPP_USERS_WABA_ID=WHATSAPP_TEST_USER_WABA_ID
WHATSAPP_CHECKERS_WABA_ID=WHATSAPP_TEST_CHECKER_WABA_ID
21 changes: 3 additions & 18 deletions functions/src/definitions/common/sendWhatsappMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
114 changes: 57 additions & 57 deletions functions/src/definitions/webhookHandlers/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}`
Expand Down

0 comments on commit b021b1b

Please sign in to comment.