feat: implement flexible mail-to-user mapping for telegram notifications

This commit is contained in:
AyrisAI
2026-05-14 13:53:29 +03:00
parent cc65a2bd72
commit eb8c591f85

View File

@@ -13,44 +13,45 @@ export async function POST(req: NextRequest) {
const data = await req.json(); const data = await req.json();
// Extract basic info from the incoming payload // Extract basic info from the incoming payload
const recipient = (data.to || data.rcpt || "").toLowerCase(); const aliciMail = (data.to || data.rcpt || "").toLowerCase().trim();
const sender = data.from || "Bilinmiyor"; const sender = data.from || "Bilinmiyor";
const subject = data.subject || "(Konu Yok)"; const subject = data.subject || "(Konu Yok)";
console.log(`[Mail Webhook] Yeni mail geldi: ${sender} -> ${recipient}`); console.log(`[Mail Webhook] Yeni mail geldi: ${sender} -> ${aliciMail}`);
const users = getUsers(); // 1. Find which USER_X owns this mail address via MAIL_email=USER_X mapping
const user = users.find(u => u.email.toLowerCase() === recipient); const envKey = `MAIL_${aliciMail}`;
const targetChatId = user?.telegramId; const ownerUserKey = process.env[envKey]; // e.g., "USER_0"
if (targetChatId && process.env.TELEGRAM_BOT_TOKEN) { if (ownerUserKey) {
const message = `🔔 *Yeni Mail!*\n\n*Kimden:* ${sender}\n*Konu:* ${subject}`; // 2. Get that USER's Telegram ID (e.g., USER_0_TELEGRAM_ID)
const tgIdKey = `${ownerUserKey}_TELEGRAM_ID`;
const targetChatId = process.env[tgIdKey];
const telegramUrl = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`; if (targetChatId && process.env.TELEGRAM_BOT_TOKEN) {
const message = `🔔 *Yeni Mail Geldi!*\n\n📧 *Alıcı:* ${aliciMail}\n👤 *Gönderen:* ${sender}\n📝 *Konu:* ${subject}`;
const res = await fetch(telegramUrl, { const telegramUrl = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: targetChatId,
text: message,
parse_mode: "Markdown",
}),
});
if (!res.ok) { const res = await fetch(telegramUrl, {
const errorText = await res.text(); method: "POST",
console.error(`[Mail Webhook] Telegram API hatası: ${res.status} ${errorText}`); headers: { "Content-Type": "application/json" },
} else { body: JSON.stringify({
console.log(`[Mail Webhook] Telegram bildirimi gönderildi: ${recipient}`); chat_id: targetChatId,
text: message,
parse_mode: "Markdown",
}),
});
if (!res.ok) {
const errorText = await res.text();
console.error(`[Mail Webhook] Telegram API hatası: ${res.status} ${errorText}`);
} else {
console.log(`[Webhook] Bildirim ${ownerUserKey} kullanıcısına (ID: ${targetChatId}) gönderildi.`);
}
} }
} else { } else {
if (!targetChatId) { console.log(`[Webhook] Sahibi bilinmeyen veya eşleşmeyen mail: ${aliciMail}`);
console.log(`[Mail Webhook] Alıcı için eşleşen Telegram ID bulunamadı: ${recipient}`);
}
if (!process.env.TELEGRAM_BOT_TOKEN) {
console.error("[Mail Webhook] TELEGRAM_BOT_TOKEN ayarlı değil!");
}
} }
return NextResponse.json({ status: "ok" }); return NextResponse.json({ status: "ok" });