From 5ece49a576236de36defc2df152d92b7e5bb18d8 Mon Sep 17 00:00:00 2001 From: Dream Hunter Date: Mon, 9 Sep 2024 20:59:12 +0800 Subject: [PATCH] feat: telegram Set manually to avoid implicit call in (#442) --- CHANGELOG.md | 4 ++++ frontend/package.json | 2 +- pages/package.json | 2 +- vitepress-docs/docs/en/cli.md | 2 ++ vitepress-docs/docs/zh/guide/cli/worker.md | 2 ++ vitepress-docs/package.json | 2 +- worker/package.json | 2 +- worker/src/constants.ts | 2 +- worker/src/telegram_api/telegram.ts | 7 ++++++- worker/src/types.d.ts | 1 + worker/src/utils.ts | 20 ++++++++++++++++++++ worker/wrangler.toml.template | 2 ++ 12 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a06f87e..44709fe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # CHANGE LOG +## main(v0.7.6) + +- feat: 支持提前设置 bot info, 降低 telegram 回调延迟 (#441) + ## v0.7.5 - fix: 修复 `name` 的校验检查 diff --git a/frontend/package.json b/frontend/package.json index 98b6d9f5..932bf66e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "cloudflare_temp_email", - "version": "0.7.5", + "version": "0.7.6", "private": true, "type": "module", "scripts": { diff --git a/pages/package.json b/pages/package.json index 34fc15ff..dfb407e7 100644 --- a/pages/package.json +++ b/pages/package.json @@ -1,6 +1,6 @@ { "name": "temp-email-pages", - "version": "0.7.5", + "version": "0.7.6", "description": "", "main": "index.js", "scripts": { diff --git a/vitepress-docs/docs/en/cli.md b/vitepress-docs/docs/en/cli.md index ac0e0a48..3e3589fa 100644 --- a/vitepress-docs/docs/en/cli.md +++ b/vitepress-docs/docs/en/cli.md @@ -124,6 +124,8 @@ ENABLE_AUTO_REPLY = false # CF_TURNSTILE_SECRET_KEY = "" # telegram bot # TG_MAX_ADDRESS = 5 +# telegram bot info, predefined bot info can reduce latency of the webhook +# TG_BOT_INFO = "{}" # global forward address list, if set, all emails will be forwarded to these addresses # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"] diff --git a/vitepress-docs/docs/zh/guide/cli/worker.md b/vitepress-docs/docs/zh/guide/cli/worker.md index 592254ed..0b06c67b 100644 --- a/vitepress-docs/docs/zh/guide/cli/worker.md +++ b/vitepress-docs/docs/zh/guide/cli/worker.md @@ -95,6 +95,8 @@ ENABLE_AUTO_REPLY = false # CF_TURNSTILE_SECRET_KEY = "" # telegram bot 最多绑定邮箱数量 # TG_MAX_ADDRESS = 5 +# telegram BOT_INFO,预定义的 BOT_INFO 可以降低 webhook 的延迟 +# TG_BOT_INFO = "{}" # 全局转发地址列表,如果不配置则不启用,启用后所有邮件都会转发到列表中的地址 # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"] diff --git a/vitepress-docs/package.json b/vitepress-docs/package.json index 0503ee00..a79d304f 100644 --- a/vitepress-docs/package.json +++ b/vitepress-docs/package.json @@ -1,7 +1,7 @@ { "name": "temp-mail-docs", "private": true, - "version": "0.7.5", + "version": "0.7.6", "type": "module", "devDependencies": { "@types/node": "^22.3.0", diff --git a/worker/package.json b/worker/package.json index 0c728bd4..d0c6a31e 100644 --- a/worker/package.json +++ b/worker/package.json @@ -1,6 +1,6 @@ { "name": "cloudflare_temp_email", - "version": "0.7.5", + "version": "0.7.6", "private": true, "type": "module", "scripts": { diff --git a/worker/src/constants.ts b/worker/src/constants.ts index 04f0f2a1..b0fbebc3 100644 --- a/worker/src/constants.ts +++ b/worker/src/constants.ts @@ -1,5 +1,5 @@ export const CONSTANTS = { - VERSION: 'v0.7.5', + VERSION: 'v0.7.6', // DB settings ADDRESS_BLOCK_LIST_KEY: 'address_block_list', diff --git a/worker/src/telegram_api/telegram.ts b/worker/src/telegram_api/telegram.ts index f4257d68..9667a433 100644 --- a/worker/src/telegram_api/telegram.ts +++ b/worker/src/telegram_api/telegram.ts @@ -4,11 +4,12 @@ import { Telegraf, Context as TgContext, Markup } from "telegraf"; import { callbackQuery } from "telegraf/filters"; import { CONSTANTS } from "../constants"; -import { getDomains, getStringValue } from '../utils'; +import { getDomains, getJsonObjectValue, getStringValue } from '../utils'; import { HonoCustomType } from "../types"; import { TelegramSettings } from "./settings"; import { bindTelegramAddress, deleteTelegramAddress, jwtListToAddressData, tgUserNewAddress, unbindTelegramAddress, unbindTelegramByAddress } from "./common"; import { commonParseMail } from "../common"; +import { UserFromGetMe } from "telegraf/types"; const COMMANDS = [ @@ -44,6 +45,10 @@ const COMMANDS = [ export function newTelegramBot(c: Context, token: string): Telegraf { const bot = new Telegraf(token); + const botInfo = getJsonObjectValue(c.env.TG_BOT_INFO); + if (botInfo) { + bot.botInfo = botInfo; + } bot.use(async (ctx, next) => { // check if in private chat diff --git a/worker/src/types.d.ts b/worker/src/types.d.ts index 59036c20..5a53f00d 100644 --- a/worker/src/types.d.ts +++ b/worker/src/types.d.ts @@ -60,6 +60,7 @@ export type Bindings = { // telegram config TELEGRAM_BOT_TOKEN: string TG_MAX_ADDRESS: number | undefined + TG_BOT_INFO: string | object | undefined } type JwtPayload = { diff --git a/worker/src/utils.ts b/worker/src/utils.ts index cc145ffa..68814bb9 100644 --- a/worker/src/utils.ts +++ b/worker/src/utils.ts @@ -2,6 +2,26 @@ import { Context } from "hono"; import { createMimeMessage } from "mimetext"; import { HonoCustomType, UserRole } from "./types"; +export const getJsonObjectValue = ( + value: string | any +): T | null => { + if (value == undefined || value == null) { + return null; + } + if (typeof value === "object") { + return value as T; + } + if (typeof value !== "string") { + return null; + } + try { + return JSON.parse(value) as T; + } catch (e) { + console.error(`GetJsonValue: Failed to parse ${value}`, e); + } + return null; +} + export const getJsonSetting = async ( c: Context, key: string ): Promise => { diff --git a/worker/wrangler.toml.template b/worker/wrangler.toml.template index f56aedd4..f0f12c72 100644 --- a/worker/wrangler.toml.template +++ b/worker/wrangler.toml.template @@ -66,6 +66,8 @@ ENABLE_AUTO_REPLY = false # CF_TURNSTILE_SECRET_KEY = "" # telegram bot # TG_MAX_ADDRESS = 5 +# telegram bot info, predefined bot info can reduce latency of the webhook +# TG_BOT_INFO = "{}" # global forward address list, if set, all emails will be forwarded to these addresses # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]