feat: init AI email summarization bot
- Multi-account IMAP email polling with UID tracking - DeepSeek API integration with JSON Mode structured output - Telegram notification with formatted MarkdownV2 message - YAML config with dataclass-based type validation - Graceful shutdown on SIGINT/SIGTERM - 60s default polling interval
This commit is contained in:
42
src/summarizer.py
Normal file
42
src/summarizer.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import logging
|
||||
from src.config import Config
|
||||
from src.email_client import fetch_unseen_emails, mark_as_seen
|
||||
from src.ai_client import summarize_email
|
||||
from src.tg_bot import send_message, format_summary
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def process_all(cfg: Config):
|
||||
for acct in cfg.email_accounts:
|
||||
try:
|
||||
_process_account(cfg, acct)
|
||||
except Exception as e:
|
||||
logger.error(f"处理邮箱 {acct.username} 时出错: {e}", exc_info=True)
|
||||
|
||||
|
||||
def _process_account(cfg: Config, acct):
|
||||
logger.info(f"检查邮箱: {acct.username}")
|
||||
emails = fetch_unseen_emails(acct)
|
||||
|
||||
if not emails:
|
||||
logger.info(f" 没有新邮件")
|
||||
return
|
||||
|
||||
logger.info(f" 发现 {len(emails)} 封新邮件")
|
||||
seen_uids = []
|
||||
|
||||
for mail in emails:
|
||||
try:
|
||||
logger.info(f" 正在摘要: {mail.subject}")
|
||||
summary = summarize_email(cfg.ai, mail.subject, mail.sender, mail.body)
|
||||
text = format_summary(summary)
|
||||
send_message(cfg.telegram, text)
|
||||
seen_uids.append(mail.uid)
|
||||
logger.info(f" 已发送到 Telegram")
|
||||
except Exception as e:
|
||||
logger.error(f" 处理邮件 '{mail.subject}' 失败: {e}", exc_info=True)
|
||||
seen_uids.append(mail.uid)
|
||||
|
||||
if seen_uids:
|
||||
mark_as_seen(acct, seen_uids)
|
||||
Reference in New Issue
Block a user