fix: telegram bot/miniapp bugs (#261)

This commit is contained in:
Dream Hunter
2024-05-21 22:45:48 +08:00
committed by GitHub
parent 2533257b68
commit 9ec11f7040
2 changed files with 21 additions and 3 deletions

View File

@@ -29,7 +29,7 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu
try {
await sendMailToTelegram(
{ env: env } as Context<HonoCustomType>,
message.to, rawEmail);
message.to, rawEmail, message_id);
} catch (error) {
console.log("send mail to telegram error", error);
}

View File

@@ -291,7 +291,10 @@ const parseMail = async (raw_mail: string | undefined | null) => {
}
export async function sendMailToTelegram(c: Context<HonoCustomType>, address: string, raw_mail: string) {
export async function sendMailToTelegram(
c: Context<HonoCustomType>, address: string,
raw_mail: string, message_id: string | null
) {
if (!c.env.TELEGRAM_BOT_TOKEN || !c.env.KV) {
return;
}
@@ -303,6 +306,21 @@ export async function sendMailToTelegram(c: Context<HonoCustomType>, address: st
if (!mail) {
return;
}
const mailId = await c.env.DB.prepare(
`SELECT id FROM raw_mails where address = ? and message_id = ?`
).bind(address, message_id).first<string>("id");
const bot = newTelegramBot(c, c.env.TELEGRAM_BOT_TOKEN);
await bot.telegram.sendMessage(userId, mail);
const settings = await c.env.KV.get<TelegramSettings>(CONSTANTS.TG_KV_SETTINGS_KEY, "json");
const miniAppButtons = []
if (settings?.miniAppUrl && settings?.miniAppUrl?.length > 0 && mailId) {
const url = new URL(settings.miniAppUrl);
url.pathname = "/telegram_mail"
url.searchParams.set("mail_id", mailId);
miniAppButtons.push(Markup.button.webApp("查看邮件", url.toString()));
}
await bot.telegram.sendMessage(userId, mail, {
...Markup.inlineKeyboard([
...miniAppButtons,
])
});
}