@@ -68,13 +68,51 @@ const handleBiliLink = async (ctx: Context, includeReplyTo: boolean) => {
68
68
// log found
69
69
const { from } = ctx ;
70
70
const user = from ?. username ?? from ?. first_name ?? from ?. id ?. toString ( ) ;
71
- console . log ( "Found" , {
71
+ const logData = {
72
72
chatId : chat . id ?? "unknown" ,
73
73
chatType : chat . type ?? "unknown" ,
74
74
fromUser : user ?? "unknown" ,
75
75
text : originalText ?? "no text" ,
76
76
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
+ }
78
116
}
79
117
80
118
let pending : Message . TextMessage ;
@@ -502,4 +540,17 @@ const handle_source = async (
502
540
checkArchives ( ) ;
503
541
} ;
504
542
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, "&" )
552
+ . replace ( / < / g, "<" )
553
+ . replace ( / > / g, ">" ) ;
554
+ }
555
+
505
556
export { handleBiliLink } ;
0 commit comments