From 00894751bdf21cfa674ef6c5f519d467a0e0b9db Mon Sep 17 00:00:00 2001 From: AyrisAI Date: Thu, 14 May 2026 18:42:03 +0300 Subject: [PATCH] Complete mail signal automation with Telegram notifications and content analysis --- .../2026-05-14T13_30_23_576Z-debug-0.log | 18 +++++++++ .../2026-05-14T13_30_35_429Z-debug-0.log | 17 +++++++++ .../2026-05-14T13_31_06_094Z-debug-0.log | 17 +++++++++ .../2026-05-14T13_56_06_958Z-debug-0.log | 17 +++++++++ .npm/_update-notifier-last-checked | 0 app/api/webhooks/mail-signal/route.ts | 31 ++++++++++----- lib/notifications.ts | 38 +++++++++++++++++++ scratch/check_logs.ts | 30 +++++++++++++++ scratch/test_db.ts | 20 ++++++++++ 9 files changed, 179 insertions(+), 9 deletions(-) create mode 100644 .npm/_logs/2026-05-14T13_30_23_576Z-debug-0.log create mode 100644 .npm/_logs/2026-05-14T13_30_35_429Z-debug-0.log create mode 100644 .npm/_logs/2026-05-14T13_31_06_094Z-debug-0.log create mode 100644 .npm/_logs/2026-05-14T13_56_06_958Z-debug-0.log create mode 100644 .npm/_update-notifier-last-checked create mode 100644 lib/notifications.ts create mode 100644 scratch/check_logs.ts create mode 100644 scratch/test_db.ts diff --git a/.npm/_logs/2026-05-14T13_30_23_576Z-debug-0.log b/.npm/_logs/2026-05-14T13_30_23_576Z-debug-0.log new file mode 100644 index 0000000..e0e62bd --- /dev/null +++ b/.npm/_logs/2026-05-14T13_30_23_576Z-debug-0.log @@ -0,0 +1,18 @@ +0 verbose cli /opt/homebrew/Cellar/node@22/22.22.2/bin/node /opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/bin/npm-cli.js +1 info using npm@10.9.7 +2 info using node@v22.22.2 +3 silly config load:file:/opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/npmrc +4 silly config load:file:/Users/ayrisdev/Github/mailserver/.npmrc +5 silly config load:file:/opt/homebrew/etc/npmrc +6 verbose title npm exec prisma migrate dev --name add_notification_logs +7 verbose argv "exec" "--" "prisma" "migrate" "dev" "--name" "add_notification_logs" +8 verbose logfile logs-max:10 dir:/Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_30_23_576Z- +9 verbose logfile /Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_30_23_576Z-debug-0.log +10 silly logfile done cleaning log files +11 http fetch GET https://registry.npmjs.org/npm attempt 1 failed with ENOTFOUND +12 verbose cwd /Users/ayrisdev/Github/mailserver +13 verbose os Darwin 24.6.0 +14 verbose node v22.22.2 +15 verbose npm v10.9.7 +16 verbose exit 1 +17 verbose code 1 diff --git a/.npm/_logs/2026-05-14T13_30_35_429Z-debug-0.log b/.npm/_logs/2026-05-14T13_30_35_429Z-debug-0.log new file mode 100644 index 0000000..864f75f --- /dev/null +++ b/.npm/_logs/2026-05-14T13_30_35_429Z-debug-0.log @@ -0,0 +1,17 @@ +0 verbose cli /opt/homebrew/Cellar/node@22/22.22.2/bin/node /opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/bin/npm-cli.js +1 info using npm@10.9.7 +2 info using node@v22.22.2 +3 silly config load:file:/opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/npmrc +4 silly config load:file:/Users/ayrisdev/Github/mailserver/.npmrc +5 silly config load:file:/opt/homebrew/etc/npmrc +6 verbose title npm exec prisma migrate dev --name add_notification_logs +7 verbose argv "exec" "--" "prisma" "migrate" "dev" "--name" "add_notification_logs" +8 verbose logfile logs-max:10 dir:/Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_30_35_429Z- +9 verbose logfile /Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_30_35_429Z-debug-0.log +10 silly logfile done cleaning log files +11 verbose cwd /Users/ayrisdev/Github/mailserver +12 verbose os Darwin 24.6.0 +13 verbose node v22.22.2 +14 verbose npm v10.9.7 +15 verbose exit 1 +16 verbose code 1 diff --git a/.npm/_logs/2026-05-14T13_31_06_094Z-debug-0.log b/.npm/_logs/2026-05-14T13_31_06_094Z-debug-0.log new file mode 100644 index 0000000..c5aa408 --- /dev/null +++ b/.npm/_logs/2026-05-14T13_31_06_094Z-debug-0.log @@ -0,0 +1,17 @@ +0 verbose cli /opt/homebrew/Cellar/node@22/22.22.2/bin/node /opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/bin/npm-cli.js +1 info using npm@10.9.7 +2 info using node@v22.22.2 +3 silly config load:file:/opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/npmrc +4 silly config load:file:/Users/ayrisdev/Github/mailserver/.npmrc +5 silly config load:file:/opt/homebrew/etc/npmrc +6 verbose title npm exec prisma db push +7 verbose argv "exec" "--" "prisma" "db" "push" +8 verbose logfile logs-max:10 dir:/Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_31_06_094Z- +9 verbose logfile /Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_31_06_094Z-debug-0.log +10 silly logfile done cleaning log files +11 verbose cwd /Users/ayrisdev/Github/mailserver +12 verbose os Darwin 24.6.0 +13 verbose node v22.22.2 +14 verbose npm v10.9.7 +15 verbose exit 1 +16 verbose code 1 diff --git a/.npm/_logs/2026-05-14T13_56_06_958Z-debug-0.log b/.npm/_logs/2026-05-14T13_56_06_958Z-debug-0.log new file mode 100644 index 0000000..bc7cb6e --- /dev/null +++ b/.npm/_logs/2026-05-14T13_56_06_958Z-debug-0.log @@ -0,0 +1,17 @@ +0 verbose cli /opt/homebrew/Cellar/node@22/22.22.2/bin/node /opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/bin/npm-cli.js +1 info using npm@10.9.7 +2 info using node@v22.22.2 +3 silly config load:file:/opt/homebrew/Cellar/node@22/22.22.2/lib/node_modules/npm/npmrc +4 silly config load:file:/Users/ayrisdev/Github/mailserver/.npmrc +5 silly config load:file:/opt/homebrew/etc/npmrc +6 verbose title npm exec tsx scratch/check_logs.ts +7 verbose argv "exec" "--" "tsx" "scratch/check_logs.ts" +8 verbose logfile logs-max:10 dir:/Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_56_06_958Z- +9 verbose logfile /Users/ayrisdev/Github/mailserver/.npm/_logs/2026-05-14T13_56_06_958Z-debug-0.log +10 silly logfile done cleaning log files +11 verbose cwd /Users/ayrisdev/Github/mailserver +12 verbose os Darwin 24.6.0 +13 verbose node v22.22.2 +14 verbose npm v10.9.7 +15 verbose exit 1 +16 verbose code 1 diff --git a/.npm/_update-notifier-last-checked b/.npm/_update-notifier-last-checked new file mode 100644 index 0000000..e69de29 diff --git a/app/api/webhooks/mail-signal/route.ts b/app/api/webhooks/mail-signal/route.ts index 233e07f..18d51ae 100644 --- a/app/api/webhooks/mail-signal/route.ts +++ b/app/api/webhooks/mail-signal/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { getLatestEmail } from '@/lib/mail'; +import { sendTelegramNotification } from '@/lib/notifications'; // Bu kısım normalde .env içinde olmalı const WEBHOOK_SECRET = 'besiktasK1903*'; @@ -44,35 +45,47 @@ export async function POST(request: Request) { // 3. İçerik Analizi (BMW, Penti vb.) let processed = false; + let extraInfo = ""; const content = (mailData.subject + " " + mailData.text).toLowerCase(); if (content.includes("bmw") || content.includes("tamir")) { - console.log("🚗 BMW/Tamir içerikli mail tespit edildi!"); - // Burada bildirim fırlatılacak + console.log("🚗 [Signal] BMW/Tamir içerikli mail tespit edildi!"); + extraInfo = "🚗 BMW/Tamir İlgili İçerik"; processed = true; } if (content.includes("penti") || content.includes("sipariş")) { - console.log("🛍️ Penti/Sipariş içerikli mail tespit edildi!"); + console.log("🛍️ [Signal] Penti/Sipariş içerikli mail tespit edildi!"); + extraInfo = "🛍️ Penti/Sipariş İlgili İçerik"; processed = true; } - // 4. Bildirim Logu + // 4. Bildirim Gönder (Telegram) + const notificationResult = await sendTelegramNotification( + mapping.userId, + to, + mailData.from, + mailData.subject, + extraInfo + ); + + // 5. Bildirim Logu await prisma.notificationLog.create({ data: { mailbox: to, sender: mailData.from, subject: mailData.subject, - status: processed ? "SENT" : "SKIPPED", + status: notificationResult.status, userId: mapping.userId, - error: processed ? null : "Kritik anahtar kelime bulunamadı" + error: notificationResult.error || (processed ? null : "Anahtar kelime eşleşmedi") } }); - return NextResponse.json({ - success: true, + return NextResponse.json({ + success: true, processed, - subject: mailData.subject + notification: notificationResult.status, + subject: mailData.subject }); } catch (error: any) { diff --git a/lib/notifications.ts b/lib/notifications.ts new file mode 100644 index 0000000..be2863b --- /dev/null +++ b/lib/notifications.ts @@ -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 }; + } +} diff --git a/scratch/check_logs.ts b/scratch/check_logs.ts new file mode 100644 index 0000000..1032cfd --- /dev/null +++ b/scratch/check_logs.ts @@ -0,0 +1,30 @@ +import { prisma } from "../lib/prisma"; + +async function checkDb() { + try { + console.log("--- Notification Logs ---"); + const nLogs = await prisma.notificationLog.findMany({ + orderBy: { createdAt: 'desc' }, + take: 5 + }); + console.log(JSON.stringify(nLogs, null, 2)); + + console.log("\n--- System Logs ---"); + const sLogs = await prisma.systemLog.findMany({ + orderBy: { createdAt: 'desc' }, + take: 5 + }); + console.log(JSON.stringify(sLogs, null, 2)); + + console.log("\n--- Mailbox Mappings ---"); + const mappings = await prisma.mailboxMapping.findMany(); + console.log(JSON.stringify(mappings, null, 2)); + + } catch (error) { + console.error("DB Check failed:", error); + } finally { + await prisma.$disconnect(); + } +} + +checkDb(); diff --git a/scratch/test_db.ts b/scratch/test_db.ts new file mode 100644 index 0000000..9e058d1 --- /dev/null +++ b/scratch/test_db.ts @@ -0,0 +1,20 @@ +import { Client } from 'pg'; +import 'dotenv/config'; + +async function testConnection() { + const connectionString = process.env.DATABASE_URL; + const client = new Client({ connectionString }); + + try { + console.log("Connecting to:", connectionString?.split('@')[1]); + await client.connect(); + console.log("Connected successfully!"); + const res = await client.query('SELECT NOW()'); + console.log("Current time from DB:", res.rows[0]); + await client.end(); + } catch (err) { + console.error("Connection error:", err.message); + } +} + +testConnection();