diff --git a/app/bot.py b/app/bot.py index 58f87b8..55c1cbc 100644 --- a/app/bot.py +++ b/app/bot.py @@ -2,10 +2,12 @@ import asyncio import json import logging import os +import re import tempfile from datetime import datetime import edge_tts +import emoji from mutagen.mp3 import MP3 from telegram import Bot, Update from telegram.ext import Application, CommandHandler, MessageHandler, filters @@ -62,6 +64,16 @@ def get_ogg_duration(path: str) -> int: return 0 +def clean_text(text: str) -> str: + text = emoji.replace_emoji(text, '') + text = re.sub(r'[ \t]+', ' ', text) + text = re.sub(r'\n{2,}', '\n', text) + text = text.strip() + if text: + text += '\nКонец сообщения.' + return text + + admin_chat_id = load_admin_chat_id() # Separate Bot instance for sending admin notifications @@ -112,13 +124,17 @@ async def handle_user_message(update: Update, context): """Receive text/caption → TTS → reply voice → forward to admin.""" global admin_chat_id user = update.effective_user - text = update.message.text or update.message.caption + raw_text = update.message.text or update.message.caption - if not text: + if not raw_text: # Media without caption — silently ignore return - logger.info(f"User {user.full_name} (id={user.id}): {text[:80]}") + text = clean_text(raw_text) + if not text: + return + + logger.info(f"User {user.full_name} (id={user.id}): {raw_text[:80]}") # Track stats stats = load_stats() @@ -149,7 +165,7 @@ async def handle_user_message(update: Update, context): caption = ( f'👤 {user.full_name}\n' f'🆔 {user.id}\n' - f'📝 {text}\n' + f'📝 {raw_text}\n' f'⏰ {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n' f'🔊 Длительность: {duration // 60}:{duration % 60:02d}' ) @@ -162,7 +178,7 @@ async def handle_user_message(update: Update, context): caption=caption, parse_mode='HTML', ) - logger.info(f"Admin notified: {user.full_name} → {text[:40]}") + logger.info(f"Admin notified: {user.full_name} → {raw_text[:40]}") except Exception as e: logger.error(f"Failed to notify admin: {e}") else: diff --git a/app/requirements.txt b/app/requirements.txt index f58b560..c60c4eb 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -1,3 +1,4 @@ python-telegram-bot>=21,<22 edge-tts>=7,<8 mutagen>=1.47,<2 +emoji>=2,<3