Integrate WhatsApp Gateway for mail notifications
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { prisma } from '@/lib/prisma';
|
import { prisma } from '@/lib/prisma';
|
||||||
import { sendTelegramNotification } from '@/lib/notifications';
|
import { sendTelegramNotification } from '@/lib/notifications';
|
||||||
|
import { sendWA } from '@/lib/whatsapp';
|
||||||
|
|
||||||
// Bu kısım normalde .env içinde olmalı
|
// Bu kısım normalde .env içinde olmalı
|
||||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SIGNAL_SECRET || 'besiktasK1903*';
|
const WEBHOOK_SECRET = process.env.WEBHOOK_SIGNAL_SECRET || 'besiktasK1903*';
|
||||||
@@ -74,7 +75,14 @@ export async function POST(request: Request) {
|
|||||||
"" // Analiz bilgisini kaldırdık
|
"" // 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({
|
await prisma.notificationLog.create({
|
||||||
data: {
|
data: {
|
||||||
mailbox: to,
|
mailbox: to,
|
||||||
|
|||||||
35
lib/whatsapp.ts
Normal file
35
lib/whatsapp.ts
Normal file
@@ -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' };
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user