Spaces:
Runtime error
Runtime error
Kaveh
commited on
Create handlers.py
Browse files- bot/handlers.py +44 -0
bot/handlers.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from telegram import Update
|
| 2 |
+
from telegram.ext import CallbackContext
|
| 3 |
+
from bot.fetcher import fetch_messages
|
| 4 |
+
from bot.responder import send_summary
|
| 5 |
+
from summarizer.summarize import summarize_text
|
| 6 |
+
import re
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
+
|
| 9 |
+
def handle_message(update: Update, context: CallbackContext):
|
| 10 |
+
message_text = update.message.text.lower()
|
| 11 |
+
chat_id = update.effective_chat.id
|
| 12 |
+
|
| 13 |
+
# بررسی اینکه ربات تگ شده یا نه
|
| 14 |
+
if context.bot.username.lower() not in message_text:
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
# استخراج تعداد پیام یا بازه زمانی
|
| 18 |
+
num_messages = 50 # مقدار پیشفرض
|
| 19 |
+
time_limit = None
|
| 20 |
+
|
| 21 |
+
# بررسی عدد در پیام (مثلاً "100 پیام آخر")
|
| 22 |
+
count_match = re.search(r'(\d+)\s*(پیام|تا پیام)', message_text)
|
| 23 |
+
if count_match:
|
| 24 |
+
num_messages = int(count_match.group(1))
|
| 25 |
+
|
| 26 |
+
# بررسی بازه زمانی (مثلاً "در 2 ساعت اخیر")
|
| 27 |
+
time_match = re.search(r'(\d+)\s*ساعت', message_text)
|
| 28 |
+
if time_match:
|
| 29 |
+
hours = int(time_match.group(1))
|
| 30 |
+
time_limit = datetime.now() - timedelta(hours=hours)
|
| 31 |
+
|
| 32 |
+
# گرفتن پیامها
|
| 33 |
+
messages = fetch_messages(chat_id, num_messages, time_limit)
|
| 34 |
+
|
| 35 |
+
if not messages:
|
| 36 |
+
context.bot.send_message(chat_id=chat_id, text="هیچ پیامی برای خلاصهسازی پیدا نشد.")
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
# ساخت متن کامل برای خلاصه
|
| 40 |
+
full_text = "\n".join(messages)
|
| 41 |
+
summary = summarize_text(full_text)
|
| 42 |
+
|
| 43 |
+
# ارسال خلاصه
|
| 44 |
+
send_summary(context.bot, chat_id, summary)
|