очистка текста от эмодзи перед озвучкой, добавление 'Конец сообщения'
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 42s

This commit is contained in:
vrubel 2026-06-20 08:07:24 +00:00
parent d1e4008233
commit 50e7cea1f5
2 changed files with 22 additions and 5 deletions

View file

@ -2,10 +2,12 @@ import asyncio
import json import json
import logging import logging
import os import os
import re
import tempfile import tempfile
from datetime import datetime from datetime import datetime
import edge_tts import edge_tts
import emoji
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
from telegram import Bot, Update from telegram import Bot, Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters from telegram.ext import Application, CommandHandler, MessageHandler, filters
@ -62,6 +64,16 @@ def get_ogg_duration(path: str) -> int:
return 0 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() admin_chat_id = load_admin_chat_id()
# Separate Bot instance for sending admin notifications # 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.""" """Receive text/caption → TTS → reply voice → forward to admin."""
global admin_chat_id global admin_chat_id
user = update.effective_user 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 # Media without caption — silently ignore
return 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 # Track stats
stats = load_stats() stats = load_stats()
@ -149,7 +165,7 @@ async def handle_user_message(update: Update, context):
caption = ( caption = (
f'👤 <b>{user.full_name}</b>\n' f'👤 <b>{user.full_name}</b>\n'
f'🆔 <code>{user.id}</code>\n' f'🆔 <code>{user.id}</code>\n'
f'📝 {text}\n' f'📝 {raw_text}\n'
f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n' f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n'
f'🔊 Длительность: {duration // 60}:{duration % 60:02d}' f'🔊 Длительность: {duration // 60}:{duration % 60:02d}'
) )
@ -162,7 +178,7 @@ async def handle_user_message(update: Update, context):
caption=caption, caption=caption,
parse_mode='HTML', 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: except Exception as e:
logger.error(f"Failed to notify admin: {e}") logger.error(f"Failed to notify admin: {e}")
else: else:

View file

@ -1,3 +1,4 @@
python-telegram-bot>=21,<22 python-telegram-bot>=21,<22
edge-tts>=7,<8 edge-tts>=7,<8
mutagen>=1.47,<2 mutagen>=1.47,<2
emoji>=2,<3