Complete mail signal automation with Telegram notifications and content analysis

This commit is contained in:
AyrisAI
2026-05-14 18:42:03 +03:00
parent f71da406d5
commit 00894751bd
9 changed files with 179 additions and 9 deletions

38
lib/notifications.ts Normal file
View File

@@ -0,0 +1,38 @@
import { prisma } from "./prisma";
export async function sendTelegramNotification(userId: string, aliciMail: string, sender: string, subject: string, extraInfo: string = "") {
const user = await prisma.user.findUnique({
where: { id: userId },
});
if (!user || !user.telegramId || !process.env.TELEGRAM_BOT_TOKEN) {
console.warn(`[Telegram] Skipped for user ${userId}. Token or ChatId missing.`);
return { status: "FAILED", error: "Missing token or chatId" };
}
const message = `🔔 *Yeni Mail Geldi!*\n\n📧 *Alıcı:* ${aliciMail}\n👤 *Gönderen:* ${sender}\n📝 *Konu:* ${subject}${extraInfo ? `\n\n💡 *Analiz:* ${extraInfo}` : ""}`;
const telegramUrl = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
try {
const res = await fetch(telegramUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: user.telegramId,
text: message,
parse_mode: "Markdown",
}),
});
if (!res.ok) {
const errorText = await res.text();
console.error(`[Telegram] API Error: ${res.status} ${errorText}`);
return { status: "FAILED", error: `API Error: ${res.status}` };
}
return { status: "SENT", error: null };
} catch (err: any) {
console.error(`[Telegram] Network Error: ${err.message}`);
return { status: "FAILED", error: err.message };
}
}