fix: add debug logging to email alert function

This commit is contained in:
Денис Кривоченко
2026-07-31 01:16:29 +07:00
parent 6c466dc0f6
commit cecec601e0
+21 -3
View File
@@ -34,7 +34,10 @@ def _transcription_log_and_notify_email(action: str, error_details: str):
current_time = time.time() current_time = time.time()
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.info(f"📧 [EMAIL ALERT] Attempting to send alert for: {action}")
if current_time - _last_transcription_email_time < 10.0: if current_time - _last_transcription_email_time < 10.0:
logger.info(f"📧 [EMAIL ALERT] Cooldown active, skipping. Last: {_last_transcription_email_time}")
return return
_last_transcription_email_time = current_time _last_transcription_email_time = current_time
@@ -42,15 +45,29 @@ def _transcription_log_and_notify_email(action: str, error_details: str):
import smtplib import smtplib
from email.mime.text import MIMEText from email.mime.text import MIMEText
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = str(getattr(config, 'DEFAULT_EMAIL_FROM', 'bot@noreply.com'))
default_from = getattr(config, 'DEFAULT_EMAIL_FROM', 'bot@noreply.com')
smtp_server = getattr(config, 'SMTP_SERVER', 'localhost')
smtp_port = getattr(config, 'SMTP_PORT', 25)
to_emails = getattr(config, 'ALERTS_SUPPORT_EMAILS', ['admin@example.com']) to_emails = getattr(config, 'ALERTS_SUPPORT_EMAILS', ['admin@example.com'])
logger.info(f"📧 [EMAIL ALERT] From={default_from}, To={to_emails}, SMTP={smtp_server}:{smtp_port}")
msg['From'] = str(default_from)
msg['To'] = ', '.join(to_emails) if isinstance(to_emails, list) else str(to_emails) msg['To'] = ', '.join(to_emails) if isinstance(to_emails, list) else str(to_emails)
msg['Subject'] = f'TrueConf Bot Error: Транскрипция - {action}' msg['Subject'] = f'TrueConf Bot Error: Транскрипция - {action}'
body = f'Обнаружена ошибка в транскрипции.\nВремя: {timestamp}\nДействие: {action}\nДетали:\n{error_details}' body = f'Обнаружена ошибка в транскрипции.\nВремя: {timestamp}\nДействие: {action}\nДетали:\n{error_details}'
msg.attach(MIMEText(body, 'plain', 'utf-8')) msg.attach(MIMEText(body, 'plain', 'utf-8'))
with smtplib.SMTP(getattr(config, 'SMTP_SERVER', 'localhost'), int(getattr(config, 'SMTP_PORT', 25))) as server:
logger.info(f"📧 [EMAIL ALERT] Connecting to SMTP...")
with smtplib.SMTP(smtp_server, int(smtp_port)) as server:
logger.info(f"📧 [EMAIL ALERT] Sending message...")
server.send_message(msg) server.send_message(msg)
except Exception: logger.info(f"📧 [EMAIL ALERT] SUCCESS!")
except Exception as e:
logger.error(f"📧 [EMAIL ALERT] FAILED: {e}")
import traceback
logger.error(f"📧 [EMAIL ALERT] Traceback: {traceback.format_exc()}")
pass pass
import time import time
@@ -164,3 +181,4 @@ async def transcription_handler(msg: Message):