mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-21 16:19:52 +08:00
feat: telegram bot global push (#269)
This commit is contained in:
@@ -4,6 +4,7 @@ import { HonoCustomType } from "../types";
|
||||
import { CONSTANTS } from "../constants";
|
||||
import { bindTelegramAddress, jwtListToAddressData, tgUserNewAddress, unbindTelegramAddress } from "./common";
|
||||
import { checkCfTurnstile } from "../utils";
|
||||
import { TelegramSettings } from "./settings";
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const TG_AUTH_TIMEOUT = 300;
|
||||
@@ -130,7 +131,12 @@ async function getMail(c: Context<HonoCustomType>): Promise<Response> {
|
||||
const result = await c.env.DB.prepare(
|
||||
`SELECT * FROM raw_mails where id = ?`
|
||||
).bind(mailId).first();
|
||||
if (result?.address && !(result.address as string in addressIdMap)) {
|
||||
const settings = await c.env.KV.get<TelegramSettings>(CONSTANTS.TG_KV_SETTINGS_KEY, "json");
|
||||
const superUser = settings?.enableGlobalMailPush && settings?.globalMailPushList.includes(userId);
|
||||
if (
|
||||
!superUser && result?.address &&
|
||||
!(result.address as string in addressIdMap)
|
||||
) {
|
||||
return c.text("无权查看此邮件", 403);
|
||||
}
|
||||
const address_id = addressIdMap[result?.address as string];
|
||||
|
||||
@@ -6,17 +6,24 @@ export class TelegramSettings {
|
||||
enableAllowList: boolean;
|
||||
allowList: string[];
|
||||
miniAppUrl: string;
|
||||
enableGlobalMailPush: boolean;
|
||||
globalMailPushList: string[];
|
||||
|
||||
constructor(enableAllowList: boolean, allowList: string[], miniAppUrl: string) {
|
||||
constructor(
|
||||
enableAllowList: boolean, allowList: string[], miniAppUrl: string,
|
||||
enableGlobalMailPush: boolean, globalMailPushList: string[]
|
||||
) {
|
||||
this.enableAllowList = enableAllowList;
|
||||
this.allowList = allowList;
|
||||
this.miniAppUrl = miniAppUrl;
|
||||
this.enableGlobalMailPush = enableGlobalMailPush;
|
||||
this.globalMailPushList = globalMailPushList;
|
||||
}
|
||||
}
|
||||
|
||||
async function getTelegramSettings(c: Context<HonoCustomType>): Promise<Response> {
|
||||
const settings = await c.env.KV.get<TelegramSettings>(CONSTANTS.TG_KV_SETTINGS_KEY, "json");
|
||||
return c.json(settings || new TelegramSettings(false, [], ""));
|
||||
return c.json(settings || new TelegramSettings(false, [], "", false, []));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -318,6 +318,15 @@ export async function sendMailToTelegram(
|
||||
url.searchParams.set("mail_id", mailId);
|
||||
miniAppButtons.push(Markup.button.webApp("查看邮件", url.toString()));
|
||||
}
|
||||
if (settings?.enableGlobalMailPush && settings?.globalMailPushList) {
|
||||
for (const pushId of settings.globalMailPushList) {
|
||||
await bot.telegram.sendMessage(pushId, mail, {
|
||||
...Markup.inlineKeyboard([
|
||||
...miniAppButtons,
|
||||
])
|
||||
});
|
||||
}
|
||||
}
|
||||
await bot.telegram.sendMessage(userId, mail, {
|
||||
...Markup.inlineKeyboard([
|
||||
...miniAppButtons,
|
||||
|
||||
Reference in New Issue
Block a user