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

35 lines
974 B
TypeScript
Raw Permalink Normal View History

2024-06-05 08:52:41 +02:00
import { db, getWorkerUtils } from "@link-stack/bridge-common";
2024-04-30 11:39:16 +02:00
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 = {
2024-07-18 11:08:01 +02:00
to: pageId,
from: messaging.sender.id,
sent_at: new Date(messaging.timestamp).toISOString(),
message: messaging.message.text,
message_id: messaging.message.mid,
2024-04-30 13:13:49 +02:00
};
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;