自動讀取 Gmail newsletter、用 Azure OpenAI 摘要後寄回給自己。
Gmail (IMAP) → 抓取連結內容 → Azure OpenAI 摘要 → Gmail (SMTP) 寄回 → 刪除原信
pip install -r requirements.txt設定拆成兩份,密鑰不進版控:
config.json:可提交到 Git。放 OpenAI 型號、senders 等非敏感設定。config.secrets.json:不提交(已在.gitignore)。放 Gmail 帳密、API Key 等。
第一次使用請:
- 複製
config.secrets.example.json為config.secrets.json - 在
config.secrets.json填入你的 Gmail、OpenAI/Azure API key 等
| 欄位 | 說明 |
|---|---|
gmail.user |
你的 Gmail 帳號 |
gmail.app_password |
Gmail 應用程式密碼(16碼) |
openai.api_key |
Azure OpenAI 或 OpenAI API Key |
openai.azure.endpoint |
Azure OpenAI endpoint URL |
| 欄位 | 說明 |
|---|---|
openai.provider |
"azure" 或 "openai" |
openai.model |
模型名稱 |
openai.azure.deployment_name |
Azure 部署名稱 |
openai.azure.api_version |
API 版本 |
senders |
Newsletter 寄件者清單 |
- Google 帳號 → 安全性 → 開啟兩步驟驗證
- 搜尋「應用程式密碼」→ 產生 16 碼密碼
- 填入
config.secrets.json的gmail.app_password
編輯 config.json 的 senders 陣列:
"senders": [
{
"name": "TechCrunch",
"email": "[email protected]",
"enabled": true
},
{
"name": "暫時停用",
"email": "[email protected]",
"enabled": false
}
]python main.py# 每天早上 8 點執行
0 8 * * * cd /path/to/newsletter_bot && python main.py >> bot.log 2>&1newsletter_bot/
├── config.json # 一般設定(可提交)
├── config.secrets.json # 密鑰(勿提交,從 config.secrets.example.json 複製)
├── config.secrets.example.json # 密鑰範本
├── config.py # 讀取設定
├── gmail_client.py # Gmail IMAP 讀取/刪除
├── scraper.py # 網頁內容抓取
├── openai_client.py # Azure OpenAI 摘要
├── email_sender.py # Gmail SMTP 寄信
├── main.py # 主程式
├── requirements.txt
└── .gitignore