36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
/**
|
|
* lib/whatsapp.ts
|
|
* Centralized service to send WhatsApp messages via AyrisTech WhatsApp Worker.
|
|
*/
|
|
|
|
export async function sendWA(to: string, message: string, userId: string = 'mustafa_yildiz') {
|
|
try {
|
|
const workerUrl = process.env.WHATSAPP_WORKER_URL;
|
|
const secret = process.env.WHATSAPP_SECRET;
|
|
|
|
if (!workerUrl || !secret) {
|
|
console.error('[WhatsApp] Missing environment variables (URL or Secret)');
|
|
return { success: false, error: 'Config missing' };
|
|
}
|
|
|
|
const response = await fetch(`${workerUrl}/send-message`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
secret,
|
|
userId,
|
|
to,
|
|
message
|
|
}),
|
|
});
|
|
|
|
const data = await response.json();
|
|
if (!response.ok) throw new Error(data.error || 'Mesaj gönderilemedi');
|
|
|
|
return { success: true, data };
|
|
} catch (error) {
|
|
console.error('WhatsApp Error:', error);
|
|
return { success: false, error: error instanceof Error ? error.message : 'Bilinmeyen hata' };
|
|
}
|
|
}
|