Skip to content

Commit aa6273a

Browse files
authored
Merge pull request #29 from zpaeng/main
创建tg通知
2 parents 1b6c983 + a90098a commit aa6273a

8 files changed

Lines changed: 146 additions & 1 deletion

File tree

.github/workflows/deploy.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ jobs:
2828
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
2929
WEBHOOK_TYPE: ${{ vars.WEBHOOK_TYPE }}
3030
WEBHOOK_TEMPLATE: ${{ vars.WEBHOOK_TEMPLATE }}
31+
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
32+
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
33+
TELEGRAM_TEMPLATE: ${{ vars.TELEGRAM_TEMPLATE }}
34+
TELEGRAM_TYPE: ${{ vars.TELEGRAM_TYPE }}
3135

3236
steps:
3337
- name: 检出代码仓库 - Checkout repository

cloudflare-env.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ declare namespace Cloudflare {
1212
WEBHOOK_URL: "";
1313
WEBHOOK_TYPE: "";
1414
WEBHOOK_TEMPLATE: "";
15+
TELEGRAM_BOT_TOKEN: string;
16+
TELEGRAM_CHAT_ID: string;
17+
TELEGRAM_TEMPLATE: string;
18+
TELEGRAM_TYPE: string;
1519
JWT_MIN_TTL: "300";
1620
JWT_MAX_TTL: "6000";
1721
USERNAME: string;

docs/deploy.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@
5656
| `AUTO_DEL_TIME` || 自动删除过期邮件时间,单位秒 |
5757
| `JWT_MIN_TTL` || JWT 最小 TTL,默认300s |
5858
| `JWT_MAX_TTL` || JWT 最大 TTL,默认6000s |
59+
| `TELEGRAM_TEMPLATE` || Telegram 消息模板 |
60+
| `TELEGRAM_TYPE` || Telegram 发送的邮件类型 |
5961

6062
![](images/github_2.png)
6163

64+
6. 如需使用Telegram Bot通知,还需添加以下 Secrets:
65+
66+
| Secret 名称 | 必需 | 用途 |
67+
| ----------------------- | :--: | ----------------------------------------------------- |
68+
| `TELEGRAM_BOT_TOKEN` || Telegram Bot Token |
69+
| `TELEGRAM_CHAT_ID` || Telegram Chat ID |
70+
6271

6372

6473
**运行工作流**
@@ -153,4 +162,29 @@ WEBHOOK_TYPE=auth_code,auth_link,service_link,subscription_link,other_link
153162
注意 WebHook 模板 需要转义,下方是一个示例
154163
```
155164
{\"text\":{\"content\":\"{{fromName}} {{emailResult}}\"},\"msgtype\":\"text\",}
165+
```
166+
167+
## Telegram Bot 通知
168+
169+
`TELEGRAM_BOT_TOKEN` Telegram Bot Token
170+
171+
`TELEGRAM_CHAT_ID` Telegram Chat ID
172+
173+
`TELEGRAM_TYPE` Telegram 发送的邮件类型
174+
175+
多种类型使用英文逗号分隔,示例
176+
177+
```
178+
TELEGRAM_TYPE=auth_code,auth_link,service_link,subscription_link,other_link
179+
```
180+
181+
`TELEGRAM_TEMPLATE` Telegram 消息模板
182+
183+
模板支持的变量与WebHook相同,支持HTML格式,示例:
184+
```
185+
<b>新邮件通知</b>
186+
发件人: {fromName}
187+
标题: {title}
188+
类型: {emailType}
189+
结果: {emailResult}
156190
```

docs/deploy_en.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ Copy to GitHub Secrets as `D1_DATABASE_ID`
5656
| `AUTO_DEL_TIME` || Time for automatic deletion of expired emails, in seconds |
5757
| `JWT_MIN_TTL` || JWT minimum TTL, defaults to 300s |
5858
| `JWT_MAX_TTL` || JWT maximum TTL, defaults to 6000s |
59+
| `TELEGRAM_TEMPLATE` || Telegram message template |
60+
| `TELEGRAM_TYPE` || Email types for Telegram delivery |
5961

6062
![](images/github_2.png)
6163

64+
6. If you want to use Telegram Bot notifications, also add the following Secrets:
65+
66+
| Secret Name | Required | Purpose |
67+
| ----------------------- | :------: | ----------------------------------------------------- |
68+
| `TELEGRAM_BOT_TOKEN` || Telegram Bot Token |
69+
| `TELEGRAM_CHAT_ID` || Telegram Chat ID |
70+
6271
**Run Workflow**
6372
1. Then manually run the workflow on the Actions page
6473
2. For future updates, just click the Sync Upstream button
@@ -152,4 +161,29 @@ Template supported variables
152161
Note: WebHook template needs to be escaped, here's an example
153162
```
154163
{\"text\":{\"content\":\"{{fromName}} {{emailResult}}\"},\"msgtype\":\"text\",}
164+
```
165+
166+
## Telegram Bot Notification
167+
168+
`TELEGRAM_BOT_TOKEN` Telegram Bot Token
169+
170+
`TELEGRAM_CHAT_ID` Telegram Chat ID
171+
172+
`TELEGRAM_TYPE` Email types for Telegram delivery
173+
174+
Multiple types are separated by English commas, example
175+
176+
```
177+
TELEGRAM_TYPE=auth_code,auth_link,service_link,subscription_link,other_link
178+
```
179+
180+
`TELEGRAM_TEMPLATE` Telegram message template
181+
182+
Template supports the same variables as WebHook and supports HTML format, example:
183+
```
184+
<b>New Email Notification</b>
185+
From: {fromName}
186+
Subject: {title}
187+
Type: {emailType}
188+
Result: {emailResult}
155189
```

scripts/gen-config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const envVars = {
2020
WEBHOOK_URL: process.env.WEBHOOK_URL,
2121
WEBHOOK_TYPE: process.env.WEBHOOK_TYPE,
2222
WEBHOOK_TEMPLATE: process.env.WEBHOOK_TEMPLATE,
23+
TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN,
24+
TELEGRAM_CHAT_ID: process.env.TELEGRAM_CHAT_ID,
25+
TELEGRAM_TEMPLATE: process.env.TELEGRAM_TEMPLATE,
26+
TELEGRAM_TYPE: process.env.TELEGRAM_TYPE,
2327
};
2428

2529
const configPath = path.join(__dirname, '..', 'wrangler.jsonc');
@@ -29,7 +33,7 @@ const configContent = fs.readFileSync(configPath, 'utf-8');
2933
const config = JSON.parse(configContent);
3034

3135
if (config.vars) {
32-
['ENABLE_AI_EXTRACT', 'EXTRACT_MODEL', 'JWT_MIN_TTL', 'JWT_MAX_TTL', 'ENABLE_AUTO_DEL', 'AUTO_DEL_TYPE', 'AUTO_DEL_TIME', 'WEBHOOK_URL', 'WEBHOOK_TYPE', 'WEBHOOK_TEMPLATE']
36+
['ENABLE_AI_EXTRACT', 'EXTRACT_MODEL', 'JWT_MIN_TTL', 'JWT_MAX_TTL', 'ENABLE_AUTO_DEL', 'AUTO_DEL_TYPE', 'AUTO_DEL_TIME', 'WEBHOOK_URL', 'WEBHOOK_TYPE', 'WEBHOOK_TEMPLATE', 'TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID', 'TELEGRAM_TEMPLATE', 'TELEGRAM_TYPE']
3337
.forEach(key => {
3438
if (envVars[key]) config.vars[key] = envVars[key];
3539
});

src/lib/email/store.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import emailDB from "@/lib/db/email";
22
import extract from "./extract";
33
import sendWebhook from '@/lib/webhook/webhook'
4+
import sendTelegramMessage from '@/lib/telegram/telegram'
45
import PostalMime from "postal-mime";
56
import * as cheerio from 'cheerio';
67
import type { Email, NewEmail } from "@/types";
@@ -77,6 +78,15 @@ export default async function storeEmail(
7778
if (env.WEBHOOK_URL && env.WEBHOOK_TEMPLATE && env.WEBHOOK_TYPE.split(',').includes(emailData.emailType)) {
7879
await sendWebhook(replaceTemplateAdvanced(env.WEBHOOK_TEMPLATE, res), env.WEBHOOK_URL);
7980
}
81+
82+
// 发送到Telegram Bot
83+
if (env.TELEGRAM_BOT_TOKEN && env.TELEGRAM_CHAT_ID && env.TELEGRAM_TEMPLATE && env.TELEGRAM_TYPE && env.TELEGRAM_TYPE.split(',').includes(emailData.emailType)) {
84+
await sendTelegramMessage(
85+
replaceTemplateAdvanced(env.TELEGRAM_TEMPLATE, res),
86+
env.TELEGRAM_BOT_TOKEN,
87+
env.TELEGRAM_CHAT_ID
88+
);
89+
}
8090
console.log("Email stored successfully:", {
8191
id: res.id,
8292
messageId: emailData.messageId,

src/lib/telegram/telegram.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export interface TelegramMessage {
2+
chat_id: string;
3+
text: string;
4+
parse_mode?: 'HTML' | 'Markdown' | 'MarkdownV2';
5+
}
6+
7+
export default async function sendTelegramMessage(
8+
message: string,
9+
botToken: string,
10+
chatId: string
11+
): Promise<void> {
12+
if (!botToken || !chatId) {
13+
console.error('Telegram error: Bot token and chat ID are required');
14+
return;
15+
}
16+
17+
const telegramApiUrl = `https://api.telegram.org/bot${botToken}/sendMessage`;
18+
19+
const payload: TelegramMessage = {
20+
chat_id: chatId,
21+
text: message,
22+
parse_mode: 'HTML'
23+
};
24+
25+
try {
26+
const controller = new AbortController();
27+
const timeoutId = setTimeout(() => controller.abort(), 10000);
28+
29+
const response = await fetch(telegramApiUrl, {
30+
method: 'POST',
31+
headers: {
32+
'Content-Type': 'application/json',
33+
},
34+
body: JSON.stringify(payload),
35+
signal: controller.signal
36+
});
37+
38+
clearTimeout(timeoutId);
39+
40+
if (!response.ok) {
41+
const errorText = await response.text();
42+
console.error('Telegram API error:', response.status, errorText);
43+
return;
44+
}
45+
46+
const result = await response.json() as { message_id?: number };
47+
console.log('Telegram message sent successfully:', result.message_id);
48+
} catch (error) {
49+
console.error('Telegram error:', error);
50+
}
51+
}

wrangler.jsonc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
"WEBHOOK_URL": "",
2929
"WEBHOOK_TYPE": "",
3030
"WEBHOOK_TEMPLATE": "",
31+
"TELEGRAM_BOT_TOKEN": "",
32+
"TELEGRAM_CHAT_ID": "",
33+
"TELEGRAM_TEMPLATE": "",
34+
"TELEGRAM_TYPE": "",
3135
"JWT_MIN_TTL": "300",
3236
"JWT_MAX_TTL": "6000"
3337
},

0 commit comments

Comments
 (0)