diff --git a/transcription_bot/handlers.py b/transcription_bot/handlers.py index 3038b3e..58cf4d5 100644 --- a/transcription_bot/handlers.py +++ b/transcription_bot/handlers.py @@ -23,8 +23,38 @@ from utils.texts import ( # 🔌 Импортируем централизованную функцию сбора статистики из main from utils.stats_logger import log_menu_stats -logger = logging.getLogger(__name__) -router = Router() +# ========================================================= +# СИСТЕМА ОТПРАВКИ ОШИБОК НА ПОЧТУ +# ========================================================= +global _last_transcription_email_time +_last_transcription_email_time = 0.0 + +def _transcription_log_and_notify_email(action: str, error_details: str): + global _last_transcription_email_time + current_time = time.time() + timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + + if current_time - _last_transcription_email_time < 10.0: + return + _last_transcription_email_time = current_time + + try: + import smtplib + from email.mime.text import MIMEText + msg = MIMEMultipart() + msg['From'] = str(getattr(config.config, 'DEFAULT_EMAIL_FROM', 'bot@noreply.com')) + to_emails = getattr(config.config, 'ALERTS_SUPPORT_EMAILS', ['admin@example.com']) + msg['To'] = ', '.join(to_emails) if isinstance(to_emails, list) else str(to_emails) + msg['Subject'] = f'TrueConf Bot Error: Транскрипция - {action}' + body = f'Обнаружена ошибка в транскрипции.\nВремя: {timestamp}\nДействие: {action}\nДетали:\n{error_details}' + msg.attach(MIMEText(body, 'plain', 'utf-8')) + with smtplib.SMTP(getattr(config.config, 'SMTP_SERVER', 'localhost'), int(getattr(config.config, 'SMTP_PORT', 25))) as server: + server.send_message(msg) + except Exception: + pass + +import time +from datetime import datetime # ✅ Полный список разрешенных расширений (Аудио, Видео, Проф. форматы) ALLOWED_EXTENSIONS = [ @@ -50,6 +80,7 @@ def get_user_email_sync(login: str) -> str: return str(entries[0].mail.value) except Exception as e: logger.error(f"Ошибка поиска email в AD: {e}") + _transcription_log_and_notify_email("Поиск email в AD", str(e)) return DEFAULT_REQUESTER @router.message() @@ -114,13 +145,16 @@ async def transcription_handler(msg: Message): log_menu_stats(user_id, "Speech-to-Text", f"Отправка встречи на расшифровку ({ext})") await msg.answer(transcription_success_received(user_email), parse_mode="html") else: + _transcription_log_and_notify_email(f"API транскрипции вернул статус {response.status_code}", str(response.text[:500])) await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html") except Exception as e: logger.error(f"Transcription Upload Error: {e}") + _transcription_log_and_notify_email("Загрузка/отправка файла на транскрипцию", str(e)) await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html") return # --- ЗАГЛУШКА НА НЕИЗВЕСТНЫЙ ТЕКСТ / СТАНДАРТНАЯ ОШИБКА ВВОДА --- await msg.answer(UNKNOWN_MAIN_CMD_TEXT, parse_mode="html") +