Skip to content

Commit 4b85cdc

Browse files
committed
feat: add logging options for chat ID and topic in README and implement logging functionality in handleBiliLink
1 parent e3eaa13 commit 4b85cdc

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ docker run -d \
2020
-e BILIARCHIVER_API={THE_API_URL_OF_BILIARCHIVER}\
2121
-e BILIARCHIVER_BOT={YOUR_BOT_TOKEN}\
2222
-e BILIARCHIVER_ENABLE_BLACKLIST=true\ # Optional, if you want to enable blacklist feature, don't forget to trigger /addadmin command first
23+
-e BILIARCHIVER_LOG_INTO_CHAT_ID={YOUR_CHAT_ID}\ # Optional, if you want to log into a specific chat group, you can set the chat ID here.
24+
-e BILIARCHIVER_LOG_INTO_CHAT_TOPIC={YOUR_CHAT_TOPIC}\ # Optional, if you want to log into a specific chat topic, you can set the topic here.
2325
--restart always \
2426
ghcr.io/saveweb/biliarchiverbot:latest
2527
```
@@ -55,6 +57,8 @@ If you don't have public IP, you can use [ngrok](https://ngrok.com/) to expose y
5557
BILIARCHIVER_USERNAME=<THE_TELEGRAM_USERNAME_OF_BILIARCHIVER_BOT>
5658
BILIARCHIVER_API=<THE_API_URL_OF_BILIARCHIVER>
5759
BILIARCHIVER_BOT=<YOUR_BOT_TOKEN>
60+
BILIARCHIVER_LOG_INTO_CHAT_ID=<YOUR_CHAT_ID> # Optional, if you want to log into a specific chat group, you can set the chat ID here.
61+
BILIARCHIVER_LOG_INTO_CHAT_TOPIC=<YOUR_CHAT_TOPIC> # Optional, if you want to log into a specific chat topic, you can set the topic here.
5862
```
5963

6064
5. deploy
@@ -79,6 +83,9 @@ If you don't have public IP, you can use [ngrok](https://ngrok.com/) to expose y
7983
BILIARCHIVER_USERNAME=<THE_TELEGRAM_USERNAME_OF_BILIARCHIVER_BOT>
8084
BILIARCHIVER_API=<THE_API_URL_OF_BILIARCHIVER>
8185
BILIARCHIVER_BOT=<YOUR_BOT_TOKEN>
86+
BILIARCHIVER_ENABLE_BLACKLIST=true # Optional, if you want to enable blacklist feature, don't forget to trigger /addadmin command first
87+
BILIARCHIVER_LOG_INTO_CHAT_ID=<YOUR_CHAT_ID> # Optional, if you want to log into a specific chat group, you can set the chat ID here.
88+
BILIARCHIVER_LOG_INTO_CHAT_TOPIC=<YOUR_CHAT_TOPIC> # Optional, if you want to log into a specific chat topic, you can set the topic here.
8289
```
8390
4. start the development server
8491

src/lib/server/utils.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,51 @@ const handleBiliLink = async (ctx: Context, includeReplyTo: boolean) => {
6868
// log found
6969
const { from } = ctx;
7070
const user = from?.username ?? from?.first_name ?? from?.id?.toString();
71-
console.log("Found", {
71+
const logData = {
7272
chatId: chat.id ?? "unknown",
7373
chatType: chat.type ?? "unknown",
7474
fromUser: user ?? "unknown",
7575
text: originalText ?? "no text",
7676
replyText: replyToText ?? "no text",
77-
});
77+
};
78+
console.info("Found", logData);
79+
80+
if (env.BILIARCHIVER_LOG_INTO_CHAT_ID) {
81+
const logChatId = Number(env.BILIARCHIVER_LOG_INTO_CHAT_ID);
82+
83+
const logMessage =
84+
`<b>✅ Archive Request Received</b>` +
85+
`<br>-------------------------` +
86+
`<br><b>Chat Type:</b> <code>${logData.chatType}</code>` +
87+
`<a href="tg://user?id=${logData.chatId}">From</a>` +
88+
`<b>User:</b> ${escapeHtml(logData.fromUser)}` +
89+
`<b>BiliLink:</b> <a href="https://www.bilibili.com/video/${bv}">${bv}</a>` +
90+
`<b>Original Text:</b>` +
91+
`<pre>${escapeHtml(logData.text)}</pre>`;
92+
93+
const options: any = { parse_mode: "HTML" };
94+
if (env.BILIARCHIVER_LOG_INTO_CHAT_TOPIC) {
95+
try {
96+
options.message_thread_id = Number(
97+
env.BILIARCHIVER_LOG_INTO_CHAT_TOPIC
98+
);
99+
} catch (e) {
100+
console.error(
101+
"Invalid BILIARCHIVER_LOG_INTO_CHAT_TOPIC:",
102+
env.BILIARCHIVER_LOG_INTO_CHAT_TOPIC
103+
);
104+
}
105+
}
106+
107+
try {
108+
await ctx.api.sendMessage(logChatId, logMessage, options);
109+
} catch (error) {
110+
console.error(
111+
`Error sending log message to chat ID ${logChatId}:`,
112+
error
113+
);
114+
}
115+
}
78116
}
79117

80118
let pending: Message.TextMessage;
@@ -502,4 +540,17 @@ const handle_source = async (
502540
checkArchives();
503541
};
504542

543+
/**
544+
* Escapes HTML special characters to prevent formatting issues in Telegram messages.
545+
* @param unsafe The string to escape.
546+
* @returns The escaped string.
547+
*/
548+
function escapeHtml(unsafe: string | undefined | null): string {
549+
if (!unsafe) return "N/A"; // Return 'N/A' or an empty string if input is null/undefined
550+
return unsafe
551+
.replace(/&/g, "&amp;")
552+
.replace(/</g, "&lt;")
553+
.replace(/>/g, "&gt;");
554+
}
555+
505556
export { handleBiliLink };

0 commit comments

Comments
 (0)