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

View File

@@ -1,6 +1,10 @@
<!-- markdownlint-disable-file MD004 MD024 MD034 MD036 -->
# CHANGE LOG
## main(v0.7.6)
- feat: 支持提前设置 bot info, 降低 telegram 回调延迟 (#441)
## v0.7.5
- fix: 修复 `name` 的校验检查

View File

@@ -1,6 +1,6 @@
{
"name": "cloudflare_temp_email",
"version": "0.7.5",
"version": "0.7.6",
"private": true,
"type": "module",
"scripts": {

View File

@@ -1,6 +1,6 @@
{
"name": "temp-email-pages",
"version": "0.7.5",
"version": "0.7.6",
"description": "",
"main": "index.js",
"scripts": {

View File

@@ -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"]

View File

@@ -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"]

View File

@@ -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",

View File

@@ -1,6 +1,6 @@
{
"name": "cloudflare_temp_email",
"version": "0.7.5",
"version": "0.7.6",
"private": true,
"type": "module",
"scripts": {

View File

@@ -1,5 +1,5 @@
export const CONSTANTS = {
VERSION: 'v0.7.5',
VERSION: 'v0.7.6',
// DB settings
ADDRESS_BLOCK_LIST_KEY: 'address_block_list',

View File

@@ -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<HonoCustomType>, token: string): Telegraf {
const bot = new Telegraf(token);
const botInfo = getJsonObjectValue<UserFromGetMe>(c.env.TG_BOT_INFO);
if (botInfo) {
bot.botInfo = botInfo;
}
bot.use(async (ctx, next) => {
// check if in private chat

View File

@@ -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 = {

View File

@@ -2,6 +2,26 @@ import { Context } from "hono";
import { createMimeMessage } from "mimetext";
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>(
c: Context<HonoCustomType>, key: string
): Promise<T | null> => {

View File

@@ -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"]