feat: telegram Set manually to avoid implicit call in (#442)

This commit is contained in:
Dream Hunter
2024-09-09 20:59:12 +08:00
committed by GitHub
parent de80857e2c
commit 5ece49a576
12 changed files with 42 additions and 6 deletions
+4
View File
@@ -1,6 +1,10 @@
<!-- markdownlint-disable-file MD004 MD024 MD034 MD036 --> <!-- markdownlint-disable-file MD004 MD024 MD034 MD036 -->
# CHANGE LOG # CHANGE LOG
## main(v0.7.6)
- feat: 支持提前设置 bot info, 降低 telegram 回调延迟 (#441)
## v0.7.5 ## v0.7.5
- fix: 修复 `name` 的校验检查 - fix: 修复 `name` 的校验检查
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "cloudflare_temp_email", "name": "cloudflare_temp_email",
"version": "0.7.5", "version": "0.7.6",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "temp-email-pages", "name": "temp-email-pages",
"version": "0.7.5", "version": "0.7.6",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
+2
View File
@@ -124,6 +124,8 @@ ENABLE_AUTO_REPLY = false
# CF_TURNSTILE_SECRET_KEY = "" # CF_TURNSTILE_SECRET_KEY = ""
# telegram bot # telegram bot
# TG_MAX_ADDRESS = 5 # 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 # global forward address list, if set, all emails will be forwarded to these addresses
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"] # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]
@@ -95,6 +95,8 @@ ENABLE_AUTO_REPLY = false
# CF_TURNSTILE_SECRET_KEY = "" # CF_TURNSTILE_SECRET_KEY = ""
# telegram bot 最多绑定邮箱数量 # telegram bot 最多绑定邮箱数量
# TG_MAX_ADDRESS = 5 # TG_MAX_ADDRESS = 5
# telegram BOT_INFO,预定义的 BOT_INFO 可以降低 webhook 的延迟
# TG_BOT_INFO = "{}"
# 全局转发地址列表,如果不配置则不启用,启用后所有邮件都会转发到列表中的地址 # 全局转发地址列表,如果不配置则不启用,启用后所有邮件都会转发到列表中的地址
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"] # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "temp-mail-docs", "name": "temp-mail-docs",
"private": true, "private": true,
"version": "0.7.5", "version": "0.7.6",
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"@types/node": "^22.3.0", "@types/node": "^22.3.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "cloudflare_temp_email", "name": "cloudflare_temp_email",
"version": "0.7.5", "version": "0.7.6",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
+1 -1
View File
@@ -1,5 +1,5 @@
export const CONSTANTS = { export const CONSTANTS = {
VERSION: 'v0.7.5', VERSION: 'v0.7.6',
// DB settings // DB settings
ADDRESS_BLOCK_LIST_KEY: 'address_block_list', ADDRESS_BLOCK_LIST_KEY: 'address_block_list',
+6 -1
View File
@@ -4,11 +4,12 @@ import { Telegraf, Context as TgContext, Markup } from "telegraf";
import { callbackQuery } from "telegraf/filters"; import { callbackQuery } from "telegraf/filters";
import { CONSTANTS } from "../constants"; import { CONSTANTS } from "../constants";
import { getDomains, getStringValue } from '../utils'; import { getDomains, getJsonObjectValue, getStringValue } from '../utils';
import { HonoCustomType } from "../types"; import { HonoCustomType } from "../types";
import { TelegramSettings } from "./settings"; import { TelegramSettings } from "./settings";
import { bindTelegramAddress, deleteTelegramAddress, jwtListToAddressData, tgUserNewAddress, unbindTelegramAddress, unbindTelegramByAddress } from "./common"; import { bindTelegramAddress, deleteTelegramAddress, jwtListToAddressData, tgUserNewAddress, unbindTelegramAddress, unbindTelegramByAddress } from "./common";
import { commonParseMail } from "../common"; import { commonParseMail } from "../common";
import { UserFromGetMe } from "telegraf/types";
const COMMANDS = [ const COMMANDS = [
@@ -44,6 +45,10 @@ const COMMANDS = [
export function newTelegramBot(c: Context<HonoCustomType>, token: string): Telegraf { export function newTelegramBot(c: Context<HonoCustomType>, token: string): Telegraf {
const bot = new Telegraf(token); const bot = new Telegraf(token);
const botInfo = getJsonObjectValue<UserFromGetMe>(c.env.TG_BOT_INFO);
if (botInfo) {
bot.botInfo = botInfo;
}
bot.use(async (ctx, next) => { bot.use(async (ctx, next) => {
// check if in private chat // check if in private chat
+1
View File
@@ -60,6 +60,7 @@ export type Bindings = {
// telegram config // telegram config
TELEGRAM_BOT_TOKEN: string TELEGRAM_BOT_TOKEN: string
TG_MAX_ADDRESS: number | undefined TG_MAX_ADDRESS: number | undefined
TG_BOT_INFO: string | object | undefined
} }
type JwtPayload = { type JwtPayload = {
+20
View File
@@ -2,6 +2,26 @@ import { Context } from "hono";
import { createMimeMessage } from "mimetext"; import { createMimeMessage } from "mimetext";
import { HonoCustomType, UserRole } from "./types"; import { HonoCustomType, UserRole } from "./types";
export const getJsonObjectValue = <T = any>(
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 <T = any>( export const getJsonSetting = async <T = any>(
c: Context<HonoCustomType>, key: string c: Context<HonoCustomType>, key: string
): Promise<T | null> => { ): Promise<T | null> => {
+2
View File
@@ -66,6 +66,8 @@ ENABLE_AUTO_REPLY = false
# CF_TURNSTILE_SECRET_KEY = "" # CF_TURNSTILE_SECRET_KEY = ""
# telegram bot # telegram bot
# TG_MAX_ADDRESS = 5 # 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 # global forward address list, if set, all emails will be forwarded to these addresses
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"] # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]