add whatsapp modules

This commit is contained in:
ItsMalma
2025-12-01 18:22:37 +07:00
parent 1068ad9246
commit 393b65830c
16 changed files with 297 additions and 15 deletions

View File

@@ -0,0 +1,3 @@
export abstract class AbstractWhatsAppService {
public abstract sendMessage(to: string, message: string): Promise<void>;
}

View File

@@ -0,0 +1,29 @@
import { WhatsAppError } from "@/common/errors/whatsapp.error";
import { AbstractWhatsAppService } from "@/common/services/whatsapp-service/abstract.whatsapp-service";
import { whatsAppBusinessConfig } from "@/configs/whatsapp-business.config";
export class MetaWhatsAppService extends AbstractWhatsAppService {
public async sendMessage(to: string, message: string): Promise<void> {
const response = await fetch(
`https://graph.facebook.com/v22.0/${whatsAppBusinessConfig.phoneNumberId}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${whatsAppBusinessConfig.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
messaging_product: "whatsapp",
to,
type: "text",
text: { body: message },
}),
},
);
if (!response.ok) {
console.error(await response.json());
throw new WhatsAppError("Failed to send WhatsApp message");
}
}
}