From b0139b6caba352eb4b92d7bb6109c6325b2039d3 Mon Sep 17 00:00:00 2001 From: AyrisAI Date: Thu, 14 May 2026 22:31:46 +0300 Subject: [PATCH] Integrate WhatsApp Gateway for mail notifications --- app/api/webhooks/mail-signal/route.ts | 10 +++++++- lib/whatsapp.ts | 35 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lib/whatsapp.ts diff --git a/app/api/webhooks/mail-signal/route.ts b/app/api/webhooks/mail-signal/route.ts index ce9197b..8254995 100644 --- a/app/api/webhooks/mail-signal/route.ts +++ b/app/api/webhooks/mail-signal/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { sendTelegramNotification } from '@/lib/notifications'; +import { sendWA } from '@/lib/whatsapp'; // Bu kısım normalde .env içinde olmalı const WEBHOOK_SECRET = process.env.WEBHOOK_SIGNAL_SECRET || 'besiktasK1903*'; @@ -74,7 +75,14 @@ export async function POST(request: Request) { "" // Analiz bilgisini kaldırdık ); - // 4. Bildirim Logu + // 4. Bildirim Gönder (WhatsApp) + // Şu an için varsayılan numaraya gönderiyoruz, ilerde User modeline alan eklenebilir. + const waNumber = process.env.DEFAULT_WHATSAPP_NUMBER || '905543765103'; + const waMessage = `📩 *Yeni E-posta*\n\n*Gönderen:* ${mailData.from}\n*Konu:* ${mailData.subject}\n*Alıcı:* ${to}\n\n_AyrisMail Central_`; + + await sendWA(waNumber, waMessage); + + // 5. Bildirim Logu await prisma.notificationLog.create({ data: { mailbox: to, diff --git a/lib/whatsapp.ts b/lib/whatsapp.ts new file mode 100644 index 0000000..49862aa --- /dev/null +++ b/lib/whatsapp.ts @@ -0,0 +1,35 @@ +/** + * 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' }; + } +}