diff --git a/src/lib/server/index.ts b/src/lib/server/index.ts index 9d7e5e6..cfb3f0a 100644 --- a/src/lib/server/index.ts +++ b/src/lib/server/index.ts @@ -1,4 +1,4 @@ -import { Bot, webhookCallback, Context, GrammyError, HttpError } from "grammy"; +import { Bot, Context, GrammyError, HttpError } from "grammy"; import { BiliArchiver } from "./api.js"; import * as MARKUP from "./markup.js"; import { isAdmin, addAdmin, removeAdmin, listAdmins } from "./admin.ts"; @@ -13,6 +13,7 @@ import { autoQuote } from "@roziscoding/grammy-autoquote"; import { autoRetry } from "@grammyjs/auto-retry"; import { handleBiliLink } from "./utils.js"; +// setup bot and api const token = env.BILIARCHIVER_BOT; if (!token) { console.error("\x1b[31mBOT_TOKEN must be provided!\x1b[0m"); @@ -96,12 +97,12 @@ bot.use(async (ctx, next) => { }); bot.command("bili", async (ctx) => { - await handleBiliLink(ctx); + await handleBiliLink(ctx, true); }); bot.hears( /(BV[a-zA-Z0-9]+)|(av\d+)|(bili2233.cn|b23\.(tv|wtf))\/\S+|www\.bilibili\.com\/(video|medialist|list)\/\S+|space\.bilibili\.com\/\d+/i, async (ctx) => { - await handleBiliLink(ctx); + await handleBiliLink(ctx, false); } ); diff --git a/src/lib/server/utils.ts b/src/lib/server/utils.ts index 7ce21df..4bc69e3 100644 --- a/src/lib/server/utils.ts +++ b/src/lib/server/utils.ts @@ -4,6 +4,7 @@ import resolveB23 from "./b23.js"; import * as MARKUP from "./markup.js"; import { BiliArchiver } from "./api.js"; import { env } from "$env/dynamic/private"; +import type { Message } from "grammy/types"; const apiBase = env.BILIARCHIVER_API; if (!apiBase) { @@ -13,14 +14,23 @@ const api = new BiliArchiver(new URL(apiBase)); const handleBiliLink = async (ctx: Context, includeReplyTo: boolean) => { - if (!ctx.message?.text || !ctx.chat) { - return; - } + const { message, chat } = ctx; + if (!message || !chat) return; + + let text = message.text ?? message.caption; + if (!text) return; - let text = ctx.message?.text; - if (ctx.message.reply_to_message && ctx.message.reply_to_message.text) { - text = ctx.message.reply_to_message.text + "\n" + text; + // save original text for logging + const originalText = text; + const replyTo = message.reply_to_message; + const replyToText = replyTo?.text ?? replyTo?.caption; + + // include reply_to_message.text for /bili command + if (includeReplyTo && replyTo) { + // /bili -> /bili + /n + + if (replyToText) text += "\n" + replyToText; } + text = await resolveB23(text); console.info("Resolved", text); @@ -35,17 +45,23 @@ const handleBiliLink = async (ctx: Context, includeReplyTo: boolean) => { // handle BVid const bv = new Bvid(matches[0]); - console.log("Found", { - chatId: ctx.chat?.id ?? "unknown", - chatType: ctx.chat?.type ?? "unknown", - fromUser: ctx.from?.username ?? ctx.from?.first_name ?? ctx.from?.id?.toString() ?? "unknown", - text: ctx.message?.text ?? "no text" - }); + + { // log found + const { from } = ctx; + const user = from?.username ?? from?.first_name ?? from?.id?.toString(); + console.log("Found", { + chatId: chat.id ?? "unknown", + chatType: chat.type ?? "unknown", + fromUser: user ?? "unknown", + text: originalText ?? "no text", + replyText: replyToText ?? "no text" + }); + } let pending: Message.TextMessage; try { pending = await ctx.reply("正在发送请求……", { - reply_to_message_id: ctx.message.message_id, + reply_to_message_id: message.message_id, }); } catch (e) { return; @@ -54,7 +70,7 @@ const handleBiliLink = async (ctx: Context, includeReplyTo: boolean) => { const success = await api.add(bv); const reply_markup = - ctx.chat.type === "private" ? MARKUP.MINIAPP_PRIVATE : MARKUP.MINIAPP; + chat.type === "private" ? MARKUP.MINIAPP_PRIVATE : MARKUP.MINIAPP; (async () => { const sleep = (ms: number) =>