feat: implement flexible mail-to-user mapping for telegram notifications
This commit is contained in:
@@ -13,44 +13,45 @@ export async function POST(req: NextRequest) {
|
||||
const data = await req.json();
|
||||
|
||||
// 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 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();
|
||||
const user = users.find(u => u.email.toLowerCase() === recipient);
|
||||
const targetChatId = user?.telegramId;
|
||||
// 1. Find which USER_X owns this mail address via MAIL_email=USER_X mapping
|
||||
const envKey = `MAIL_${aliciMail}`;
|
||||
const ownerUserKey = process.env[envKey]; // e.g., "USER_0"
|
||||
|
||||
if (targetChatId && process.env.TELEGRAM_BOT_TOKEN) {
|
||||
const message = `🔔 *Yeni Mail!*\n\n*Kimden:* ${sender}\n*Konu:* ${subject}`;
|
||||
if (ownerUserKey) {
|
||||
// 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, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
chat_id: targetChatId,
|
||||
text: message,
|
||||
parse_mode: "Markdown",
|
||||
}),
|
||||
});
|
||||
const telegramUrl = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
console.error(`[Mail Webhook] Telegram API hatası: ${res.status} ${errorText}`);
|
||||
} else {
|
||||
console.log(`[Mail Webhook] Telegram bildirimi gönderildi: ${recipient}`);
|
||||
const res = await fetch(telegramUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
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 {
|
||||
if (!targetChatId) {
|
||||
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!");
|
||||
}
|
||||
console.log(`[Webhook] Sahibi bilinmeyen veya eşleşmeyen mail: ${aliciMail}`);
|
||||
}
|
||||
|
||||
return NextResponse.json({ status: "ok" });
|
||||
|
||||
Reference in New Issue
Block a user