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

50 lines
999 B
TypeScript
Raw Normal View History

2024-06-05 08:52:41 +02:00
import { db, getWorkerUtils } from "@link-stack/bridge-common";
2024-04-30 13:13:49 +02:00
interface ReceiveWhatsappMessageTaskOptions {
2024-05-17 09:20:00 +02:00
token: string;
2024-07-18 11:08:01 +02:00
to: string;
from: string;
messageId: string;
sentAt: string;
2024-05-17 09:20:00 +02:00
message: string;
2024-07-18 11:08:01 +02:00
attachment?: string;
filename?: string;
mimeType?: string;
2024-04-30 13:13:49 +02:00
}
const receiveWhatsappMessageTask = async ({
2024-05-16 18:22:10 +02:00
token,
2024-07-18 11:08:01 +02:00
to,
from,
messageId,
sentAt,
2024-04-30 13:13:49 +02:00
message,
2024-07-18 11:08:01 +02:00
attachment,
filename,
mimeType,
2024-05-16 18:22:10 +02:00
}: ReceiveWhatsappMessageTaskOptions): Promise<void> => {
2024-07-18 11:08:01 +02:00
console.log({ token, to, from });
2024-05-17 09:20:00 +02:00
const worker = await getWorkerUtils();
const row = await db
2024-05-16 18:22:10 +02:00
.selectFrom("WhatsappBot")
.selectAll()
2024-05-17 09:20:00 +02:00
.where("id", "=", token)
2024-05-16 18:22:10 +02:00
.executeTakeFirstOrThrow();
2024-05-17 09:20:00 +02:00
const backendId = row.id;
const payload = {
2024-07-18 11:08:01 +02:00
to,
from,
message_id: messageId,
sent_at: sentAt,
2023-02-13 12:41:30 +00:00
message,
attachment,
filename,
2024-07-18 11:08:01 +02:00
mime_type: mimeType,
2023-02-13 12:41:30 +00:00
};
2024-07-18 11:08:01 +02:00
await worker.addJob("common/notify-webhooks", { backendId, payload });
2023-02-13 12:41:30 +00:00
};
2024-07-18 11:08:01 +02:00
export default receiveWhatsappMessageTask;