-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
185 lines (157 loc) · 7.7 KB
/
main.py
File metadata and controls
185 lines (157 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import logging
import requests
import re
from datetime import datetime
from dotenv import load_dotenv
from telegram import Update, InputFile, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler
# ======= تحميل القيم من ملف .env =======
load_dotenv()
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_TOKEN")
VT_API_KEY = os.getenv("VIRUSTOTAL_API_KEY")
# ======= إعداد اللوج =======
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO
)
# ======= دوال مساعدة =======
def get_user_log_file(user_id: int) -> str:
return f"logs_{user_id}.txt"
def save_log(user_id: int, url: str, malicious: int, harmless: int):
log_file = get_user_log_file(user_id)
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"[{datetime.now()}] URL: {url}, ضار={malicious}, آمن={harmless}\n")
def get_stats(user_id: int) -> dict:
log_file = get_user_log_file(user_id)
if not os.path.exists(log_file):
return {"total": 0, "malicious": 0, "harmless": 0}
malicious = harmless = 0
with open(log_file, "r", encoding="utf-8") as f:
for line in f:
if "ضار=" in line and "آمن=" in line:
try:
m = int(line.split("ضار=")[1].split(",")[0])
h = int(line.split("آمن=")[1].strip())
malicious += m
harmless += h
except:
continue
return {"total": malicious + harmless, "malicious": malicious, "harmless": harmless}
# ======= أوامر البوت =======
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
msg = (
"👋 مرحبا بك في بوت الفحص 🔍\n\n"
"الأوامر:\n"
"/scan + رابط 🔗 - فحص رابط\n"
"/export - تصدير تقريرك\n"
"/clear - مسح تقريرك\n"
"/stats - عرض إحصائياتك\n"
"/help - المساعدة\n"
"/about - حول البوت\n\n"
"📌 أو أرسل أي رابط مباشر ليتم فحصه ✅"
)
await update.message.reply_text(msg)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
msg = (
"ℹ️ المساعدة:\n\n"
"1️⃣ ارسل /scan ثم الرابط\n"
"مثال: /scan https://example.com\n\n"
"2️⃣ استعمل /export لتصدير تقريرك\n"
"3️⃣ استعمل /clear لمسح تقريرك\n"
"4️⃣ استعمل /stats لعرض إحصائياتك\n\n"
"📌 أو ببساطة أرسل أي رابط وسيتم فحصه تلقائيًا ✅"
)
await update.message.reply_text(msg)
async def about(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("🤖 بوت فحص الروابط باستخدام VirusTotal API.")
# ======= دالة الفحص =======
async def scan_link(update: Update, url: str):
user_id = update.effective_user.id
await update.message.reply_text(f"⏳ جاري فحص الرابط: {url}")
try:
headers = {"x-apikey": VT_API_KEY}
data = {"url": url}
response = requests.post("https://www.virustotal.com/api/v3/urls", headers=headers, data=data)
if response.status_code == 200:
res = response.json()
scan_id = res["data"]["id"]
result = requests.get(f"https://www.virustotal.com/api/v3/analyses/{scan_id}", headers=headers).json()
stats = result["data"]["attributes"]["stats"]
malicious = stats.get("malicious", 0)
harmless = stats.get("harmless", 0)
msg = f"🔍 النتيجة:\n🔴 ضار: {malicious}\n🟢 آمن: {harmless}"
keyboard = [
[InlineKeyboardButton("📂 تصدير تقريري", callback_data="export")],
[InlineKeyboardButton("🗑️ مسح تقريري", callback_data="clear")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(msg, reply_markup=reply_markup)
save_log(user_id, url, malicious, harmless)
else:
await update.message.reply_text("⚠️ خطأ أثناء الاتصال بـ VirusTotal.")
except Exception as e:
await update.message.reply_text(f"❌ خطأ: {e}")
# ======= الفحص التلقائي لأي رابط =======
async def auto_scan(update: Update, context: ContextTypes.DEFAULT_TYPE):
text = update.message.text.strip()
urls = re.findall(r"(https?://[^\s]+|www\.[^\s]+)", text)
for url in urls:
if url.startswith("www."):
url = "https://" + url
await scan_link(update, url)
# ======= تصدير التقرير =======
async def export_report(user_id: int, chat_id: int, context: ContextTypes.DEFAULT_TYPE):
log_file = get_user_log_file(user_id)
if not os.path.exists(log_file):
await context.bot.send_message(chat_id, "⚠️ لا يوجد تقارير لك حتى الآن.")
return
with open(log_file, "rb") as f:
await context.bot.send_document(chat_id, InputFile(f), filename="scan_report.txt")
# ======= مسح التقرير =======
async def clear_report(user_id: int, chat_id: int, context: ContextTypes.DEFAULT_TYPE):
log_file = get_user_log_file(user_id)
if os.path.exists(log_file):
os.remove(log_file)
await context.bot.send_message(chat_id, "🗑️ تم مسح تقريرك بنجاح.")
else:
await context.bot.send_message(chat_id, "⚠️ لا يوجد تقارير لمسحها.")
# ======= عرض الإحصائيات =======
async def stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
s = get_stats(user_id)
msg = f"📊 إحصائياتك:\n🔴 ضار: {s['malicious']}\n🟢 آمن: {s['harmless']}\n📌 الإجمالي: {s['total']}"
await update.message.reply_text(msg)
# ======= التعامل مع الأزرار =======
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
user_id = query.from_user.id
chat_id = query.message.chat_id
if query.data == "export":
await export_report(user_id, chat_id, context)
elif query.data == "clear":
await clear_report(user_id, chat_id, context)
# ======= تشغيل البوت =======
def main():
if not TELEGRAM_BOT_TOKEN:
raise ValueError("⚠️ لم يتم العثور على TELEGRAM_TOKEN")
if not VT_API_KEY:
raise ValueError("⚠️ لم يتم العثور على VT_API_KEY")
app = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
# أوامر البوت
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("help", help_command))
app.add_handler(CommandHandler("about", about))
app.add_handler(CommandHandler("scan", lambda u, c: scan_link(u, c.args[0]) if c.args else u.message.reply_text("⚠️ يجب إدخال رابط.")))
app.add_handler(CommandHandler("stats", stats))
app.add_handler(CommandHandler("export", lambda u, c: export_report(u.effective_user.id, u.effective_chat.id, c)))
app.add_handler(CommandHandler("clear", lambda u, c: clear_report(u.effective_user.id, u.effective_chat.id, c)))
# الأزرار
app.add_handler(CallbackQueryHandler(button_handler))
# الفحص التلقائي لأي رابط
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, auto_scan))
logging.info("🚀 Bot is running...")
app.run_polling()
if __name__ == "__main__":
main()