link-stack/apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts
2024-05-16 18:22:10 +02:00

32 lines
780 B
TypeScript

import { db } from "bridge-common";
interface SendWhatsappMessageTaskOptions {
token: string;
recipient: string;
message: any;
}
const sendWhatsappMessageTask = async ({
message,
recipient,
token,
}: SendWhatsappMessageTaskOptions): Promise<void> => {
const bot = await db
.selectFrom("WhatsappBot")
.selectAll()
.where("token", "=", token)
.executeTakeFirstOrThrow();
const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`;
const params = { message, phoneNumber: recipient };
console.log({ params });
const result = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(params),
});
console.log({ result });
};
export default sendWhatsappMessageTask;