Skip to content

Commit

Permalink
feat: support media caption, fix reply to support
Browse files Browse the repository at this point in the history
  • Loading branch information
Crystal-RainSlide committed Jan 26, 2025
1 parent 46d3121 commit 1f101ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/lib/server/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
);

Expand Down
44 changes: 30 additions & 14 deletions src/lib/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 + <reply to message text>
if (replyToText) text += "\n" + replyToText;
}

text = await resolveB23(text);
console.info("Resolved", text);

Expand All @@ -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;
Expand All @@ -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) =>
Expand Down

0 comments on commit 1f101ca

Please sign in to comment.