2024-06-05 08:52:41 +02:00
|
|
|
import { db } from "@link-stack/bridge-common";
|
2024-04-30 13:13:49 +02:00
|
|
|
|
|
|
|
|
interface SendWhatsappMessageTaskOptions {
|
2024-05-16 18:22:10 +02:00
|
|
|
token: string;
|
2024-07-18 11:08:01 +02:00
|
|
|
to: string;
|
2024-04-30 13:13:49 +02:00
|
|
|
message: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendWhatsappMessageTask = async ({
|
|
|
|
|
message,
|
2024-07-18 11:08:01 +02:00
|
|
|
to,
|
2024-05-16 18:22:10 +02:00
|
|
|
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`;
|
2024-07-18 11:08:01 +02:00
|
|
|
const params = { message, phoneNumber: to };
|
|
|
|
|
try {
|
|
|
|
|
const result = await fetch(url, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify(params),
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error({ error });
|
|
|
|
|
throw new Error("Failed to send message");
|
|
|
|
|
}
|
2024-05-16 18:22:10 +02:00
|
|
|
};
|
2024-04-30 13:13:49 +02:00
|
|
|
|
|
|
|
|
export default sendWhatsappMessageTask;
|