очистка текста от эмодзи перед озвучкой, добавление 'Конец сообщения'
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 42s
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 42s
This commit is contained in:
parent
d1e4008233
commit
50e7cea1f5
2 changed files with 22 additions and 5 deletions
26
app/bot.py
26
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'👤 <b>{user.full_name}</b>\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'🔊 Длительность: {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:
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
python-telegram-bot>=21,<22
|
||||
edge-tts>=7,<8
|
||||
mutagen>=1.47,<2
|
||||
emoji>=2,<3
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue