link-stack/apps/bridge-worker/tasks/facebook/receive-facebook-message.ts

32 lines
839 B
TypeScript
Raw Normal View History

2024-04-30 11:39:16 +02:00
import { db, getWorkerUtils } from "bridge-common";
interface ReceiveFacebookMessageTaskOptions {
message: any;
}
const receiveFacebookMessageTask = async ({
message,
}: ReceiveFacebookMessageTaskOptions): Promise<void> => {
const worker = await getWorkerUtils();
for (const entry of message.entry) {
for (const messaging of entry.messaging) {
const pageId = messaging.recipient.id;
const row = await db
.selectFrom("FacebookBot")
.selectAll()
.where("pageId", "=", pageId)
.executeTakeFirstOrThrow();
2024-04-30 13:13:49 +02:00
const backendId = row.id;
const payload = {
text: messaging.message.text,
recipient: messaging.sender.id,
};
2024-04-30 11:39:16 +02:00
2024-04-30 13:13:49 +02:00
await worker.addJob("common/notify-webhooks", { backendId, payload });
2024-04-30 11:39:16 +02:00
}
}
2024-04-21 16:59:50 +02:00
};
export default receiveFacebookMessageTask;