Complete mail signal automation with Telegram notifications and content analysis
This commit is contained in:
38
lib/notifications.ts
Normal file
38
lib/notifications.ts
Normal 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 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user