Spaces:
Runtime error
Runtime error
File size: 1,616 Bytes
f32f0d2 |
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 |
from telegram import Update
from telegram.ext import CallbackContext
from bot.fetcher import fetch_messages
from bot.responder import send_summary
from summarizer.summarize import summarize_text
import re
from datetime import datetime, timedelta
def handle_message(update: Update, context: CallbackContext):
message_text = update.message.text.lower()
chat_id = update.effective_chat.id
# بررسی اینکه ربات تگ شده یا نه
if context.bot.username.lower() not in message_text:
return
# استخراج تعداد پیام یا بازه زمانی
num_messages = 50 # مقدار پیشفرض
time_limit = None
# بررسی عدد در پیام (مثلاً "100 پیام آخر")
count_match = re.search(r'(\d+)\s*(پیام|تا پیام)', message_text)
if count_match:
num_messages = int(count_match.group(1))
# بررسی بازه زمانی (مثلاً "در 2 ساعت اخیر")
time_match = re.search(r'(\d+)\s*ساعت', message_text)
if time_match:
hours = int(time_match.group(1))
time_limit = datetime.now() - timedelta(hours=hours)
# گرفتن پیامها
messages = fetch_messages(chat_id, num_messages, time_limit)
if not messages:
context.bot.send_message(chat_id=chat_id, text="هیچ پیامی برای خلاصهسازی پیدا نشد.")
return
# ساخت متن کامل برای خلاصه
full_text = "\n".join(messages)
summary = summarize_text(full_text)
# ارسال خلاصه
send_summary(context.bot, chat_id, summary)
|