Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/image caveat #190

Merged
merged 20 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"projects": {
"default": "checkmate-373101"
}
}
"default": "checkmate-373101",
"prod": "checkmate-373101",
"uat": "checkmate-uat"
},
"targets": {},
"etags": {}
}
28 changes: 24 additions & 4 deletions .github/workflows/firebase-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: Deploy Cloud Functions

on:
workflow_dispatch:
push:
branches:
- main
- develop
paths:
- 'functions/**'
- "functions/**"

jobs:
build_and_deploy:
runs-on: ubuntu-latest
Expand All @@ -22,15 +25,32 @@ jobs:
${{ runner.os }}-node-
- name: Create env file
run: |
echo "${{ secrets.ENV_FILE }}" > .env
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "${{ secrets.ENV_FILE }}" > .env
else
echo "${{ secrets.ENV_FILE_NONPROD }}" > .env
fi
working-directory: functions
- name: Build Cloud Functions
run: npm ci
working-directory: functions
- name: Create SA key
run: echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}' > $HOME/gcloud.json
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}' > $HOME/gcloud.json
else
echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT_NONPROD }}' > $HOME/gcloud.json
fi
- name: Install Firebase CLI
run: npm install -g firebase-tools
- name: Set Firebase project
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
firebase use prod
else
firebase use uat
fi
working-directory: functions
- name: Deploy Cloud Functions
run: export GOOGLE_APPLICATION_CREDENTIALS=$HOME/gcloud.json && npm run deploy
working-directory: functions
working-directory: functions
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Start docker compose for testing
run: docker-compose -f docker-compose-base.yml up -d
- name: Wait for emulator to start
run: timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:15001/checkmate-373101/asia-southeast1/healthcheck)" != "200" ]]; do sleep 1; done' || false
run: timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:15001/checkmate-uat/asia-southeast1/healthcheck)" != "200" ]]; do sleep 1; done' || false
- name: Install newman and lodash
run: npm ci
working-directory: integration-tests
Expand Down
5 changes: 5 additions & 0 deletions functions/.env.checkmate-uat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NUM_SHARDS_INSTANCE_COUNT=5
NUM_SHARDS_VOTE_COUNT=5
GRAPH_API_VERSION=v15.0
ENVIRONMENT=UAT
SIMILARITY_THRESHOLD=0.85
2 changes: 1 addition & 1 deletion functions/.secret.local.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WHATSAPP_TOKEN=WHATSAPP_TOKEN
TELEGRAM_CHECKER_BOT_TOKEN=TELEGRAM_CHECKER_BOT_TOKEN
TELEGRAM_REPORT_BOT_TOKEN=TELEGRAM_REPORT_BOT_TOKEN
VERIFY_TOKEN=VERIFY_TOKEN
BITLY_TOKEN=BITLY_TOKEN
OPENAI_API_KEY=OPEN_API_KEY
TYPESENSE_TOKEN=TYPESENSE_TOKEN
ML_SERVER_TOKEN=ML_SERVER_TOKEN
TELEGRAM_BOT_TOKEN=TELEGRAM_BOT_TOKEN
Expand Down
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "",
"serve": "firebase emulators:start",
"serve": "firebase emulators:start --project checkmate-uat",
"shell": "firebase functions:shell",
"start": "npm run shell",
"predeploy": "npm run build",
Expand Down
2 changes: 1 addition & 1 deletion functions/src/definitions/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ main.use("/api", app)

const apiHandler = onRequest(
{
secrets: ["TELEGRAM_BOT_TOKEN"],
secrets: ["TELEGRAM_CHECKER_BOT_TOKEN"],
},
main
)
Expand Down
4 changes: 2 additions & 2 deletions functions/src/definitions/api/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CHECKER1_PHONE_NUMBER: string = String(process.env.CHECKER1_PHONE_NUMBER)

app.post("/", async (req, res) => {
const initData = req.body // Assuming you send initData in the body of your requests
const botToken = String(process.env.TELEGRAM_BOT_TOKEN) // Replace with your bot token
const botToken = String(process.env.TELEGRAM_CHECKER_BOT_TOKEN) // Replace with your bot token

// Extract the data from initData (convert from query string format)
const params = new URLSearchParams(initData)
Expand Down Expand Up @@ -99,7 +99,7 @@ main.use("/telegramAuth", app)

const telegramAuthHandler = onRequest(
{
secrets: ["TELEGRAM_BOT_TOKEN"],
secrets: ["TELEGRAM_CHECKER_BOT_TOKEN"],
},
main
)
Expand Down
20 changes: 12 additions & 8 deletions functions/src/definitions/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const USER_BOT_RESPONSES = {
UNTRUE: `{{thanks}}{{matched}}{{methodology}}*untrue*.❌
UNTRUE: `{{thanks}}{{matched}}{{methodology}}*untrue*.❌{{image_caveat}}

Please do not spread it further⛔️⛔️

Thank you for keeping Singapore informed!`,

MISLEADING: `{{thanks}}{{matched}}{{methodology}}*presented in a misleading or unbalanced way*, even though some elements within could be true!⚠️
MISLEADING: `{{thanks}}{{matched}}{{methodology}}*presented in a misleading or unbalanced way*, even though some elements within could be true!⚠️{{image_caveat}}

Please take it with a pinch of salt and think twice before spreading it further🚧🚧.

Thank you for keeping Singapore informed!`,

ACCURATE: `{{thanks}}{{matched}}{{methodology}}*accurate*.✅
ACCURATE: `{{thanks}}{{matched}}{{methodology}}*accurate*.✅{{image_caveat}}

Thank you for keeping Singapore informed!`,

Expand All @@ -23,7 +23,7 @@ You can try sending the message in again, or report the error via our form at ht
"Hello! 👋 Thanks for sending this in! Our *CheckMates🕵🏻 will review this* and *provide the results soon*.",

//button
SCAM: `{{thanks}}{{matched}}{{methodology}}*a scam*!🚫
SCAM: `{{thanks}}{{matched}}{{methodology}}*a scam*!🚫{{image_caveat}}

We recommend you do not engage further⛔️⛔️

Expand All @@ -32,21 +32,21 @@ CheckMate will report suspicious messages to ScamShield (https://scamshield.org.
To avoid reporting this message, select "Don't report this" below.`,

//button
ILLICIT: `{{thanks}}{{matched}}{{methodology}}*suspicious*!🚨
ILLICIT: `{{thanks}}{{matched}}{{methodology}}*suspicious*!🚨{{image_caveat}}

We recommend you do not engage further⛔️⛔️

CheckMate will report suspicious messages to ScamShield (https://scamshield.org.sg) on your behalf.

To avoid reporting this message, select "Don't report this" below.`,

SPAM: `{{thanks}}{{matched}}{{methodology}}spam!🚧
SPAM: `{{thanks}}{{matched}}{{methodology}}spam!🚧{{image_caveat}}

It's likely harmless, but you should always make sure 🧐

Thank you for keeping Singapore safe!`,

LEGITIMATE: `{{thanks}}{{matched}}{{methodology}}*from a legitimate source*.✅
LEGITIMATE: `{{thanks}}{{matched}}{{methodology}}*from a legitimate source*.✅{{image_caveat}}

Thank you for keeping Singapore safe!`,

Expand All @@ -62,6 +62,10 @@ Thank you for keeping Singapore safe!`,

THANKS_DELAYED: `Thanks for waiting! `,

IMAGE_CAVEAT: `

This assessment refers to any claims made within the captions. If there are no claims/captions, it refers to the image itself.`,

METHODOLOGY_HUMAN: `Our CheckMates have reviewed this message and think it's `,

METHODOLOGY_AUTO: `Based on pattern matching, our auto-classifier is confident that this message is `,
Expand Down
2 changes: 2 additions & 0 deletions functions/src/definitions/common/responseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ async function respondToInstance(
const isMachineCategorised = parentMessageSnap.get("isMachineCategorised")
const instanceCount = parentMessageSnap.get("instanceCount")
const responseCount = await getCount(parentMessageRef, "responses")
const isImage = data?.type === "image"

function getFinalResponseText(responseText: string) {
return responseText
Expand All @@ -646,6 +647,7 @@ async function respondToInstance(
? responses.METHODOLOGY_AUTO
: responses.METHODOLOGY_HUMAN
)
.replace("{{image_caveat}}", isImage ? responses.IMAGE_CAVEAT : "")
}

if (!isAssessed && !forceReply) {
Expand Down
34 changes: 24 additions & 10 deletions functions/src/definitions/eventHandlers/checkerHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,32 @@ async function onFactCheckerYes(
latestInstanceSnap.get("storageUrl")
)
if (temporaryUrl) {
res = await sendImageMessage(
"factChecker",
from,
temporaryUrl,
latestInstanceSnap.get("caption"),
null,
platform
)
if (!res) {
try {
res = await sendImageMessage(
"factChecker",
from,
temporaryUrl,
latestInstanceSnap.get("caption"),
null,
platform
)
if (!res) {
return
}
updateObj.sentMessageId = res.data.messages[0].id
} catch {
functions.logger.error(
`Problem sending message ${messageRef.id} to ${from}}`
)
await sendTextMessage(
"factChecker",
from,
"Sorry, an error occured",
null,
platform
)
return
}
updateObj.sentMessageId = res.data.messages[0].id
} else {
functions.logger.error(
`Problem creating URL while sending message ${messageRef.id} to ${from}}`
Expand Down
2 changes: 1 addition & 1 deletion functions/src/definitions/eventHandlers/userHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ async function newImageInstanceHandler({
machineCategory
)
}
if (extractedMessage) messageRef = db.collection("messages").doc()
messageRef = db.collection("messages").doc()
messageUpdateObj = {
machineCategory: machineCategory, //Can be "fake news" or "scam"
isMachineCategorised: isMachineAssessed,
Expand Down
4 changes: 2 additions & 2 deletions functions/src/definitions/webhookHandlers/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import express from "express"
import TelegramBot from "node-telegram-bot-api"
import { onRequest } from "firebase-functions/v2/https"

const TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
const TOKEN = String(process.env.TELEGRAM_CHECKER_BOT_TOKEN)
const bot = new TelegramBot(TOKEN)

const app = express()
Expand All @@ -23,7 +23,7 @@ app.post("/", async (req, res) => {

const telegramHandler = onRequest(
{
secrets: ["TELEGRAM_BOT_TOKEN"],
secrets: ["TELEGRAM_CHECKER_BOT_TOKEN"],
},
app
)
Expand Down
9 changes: 3 additions & 6 deletions functions/src/definitions/webhookHandlers/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Request, Response } from "express"
const runtimeEnvironment = defineString("ENVIRONMENT")

const webhookPath = process.env.WEBHOOK_PATH
const ingressSetting =
process.env.ENVIRONMENT === "PROD" ? "ALLOW_INTERNAL_AND_GCLB" : "ALLOW_ALL"

if (!admin.apps.length) {
admin.initializeApp()
Expand Down Expand Up @@ -139,11 +141,6 @@ const postHandler = async (req: Request, res: Response) => {
}
}

// Accepts POST requests at /webhook endpoint
// Note: TODO: Will delete after webhook is pointed to new endpoint and everything is stable
app.post("/whatsapp", postHandler)
app.get("/whatsapp", getHandler)

// Accepts POST requests at /{webhookPath} endpoint
app.post(`/${webhookPath}`, postHandler)
app.get(`/${webhookPath}`, getHandler)
Expand All @@ -159,7 +156,7 @@ app.post("/telegram", async (req, res) => {

const webhookHandlerV2 = onRequest(
{
ingressSettings: "ALLOW_INTERNAL_AND_GCLB",
ingressSettings: ingressSetting,
secrets: [
"WHATSAPP_USER_BOT_PHONE_NUMBER_ID",
"WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID",
Expand Down
Loading