feat: telegram bot global push (#269)

This commit is contained in:
Dream Hunter
2024-05-25 14:07:00 +08:00
committed by GitHub
parent 9414f7a977
commit bf3c372d8c
24 changed files with 232 additions and 147 deletions

View File

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

View File

@@ -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, []));
}

View File

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