@@ -2,31 +2,46 @@ import { BotContext } from "../../types/bot.types.js";
22import { NextFunction } from "grammy" ;
33import logger from "../../utils/logger.js" ;
44
5- function getMediaType (
6- message : Record < string , unknown > | undefined
7- ) : string | undefined {
5+ const MEDIA_TYPES = new Set ( [
6+ "photo" ,
7+ "video" ,
8+ "document" ,
9+ "audio" ,
10+ "voice" ,
11+ "sticker" ,
12+ "animation" ,
13+ "location" ,
14+ "contact" ,
15+ ] ) ;
16+
17+ const MAX_TEXT_LOG_LENGTH = 100 ;
18+ const MAX_QUERY_LOG_LENGTH = 50 ;
19+
20+ function getMediaType ( message : object | undefined ) : string | undefined {
821 if ( ! message ) return undefined ;
922
10- const mediaTypes = [
11- "photo" ,
12- "video" ,
13- "document" ,
14- "audio" ,
15- "voice" ,
16- "sticker" ,
17- "animation" ,
18- "location" ,
19- "contact" ,
20- ] ;
21-
22- return mediaTypes . find ( type => message [ type ] ) ;
23+ for ( const key in message ) {
24+ if ( MEDIA_TYPES . has ( key ) ) {
25+ return key ;
26+ }
27+ }
28+
29+ return undefined ;
30+ }
31+
32+ function getUpdateType ( update : object ) : string {
33+ for ( const key in update ) {
34+ if ( key !== "update_id" ) {
35+ return key ;
36+ }
37+ }
38+ return "unknown" ;
2339}
2440
2541async function logging ( ctx : BotContext , next : NextFunction ) : Promise < void > {
2642 const start = Date . now ( ) ;
2743
28- const updateType =
29- Object . keys ( ctx . update ) . find ( key => key !== "update_id" ) || "unknown" ;
44+ const updateType = getUpdateType ( ctx . update ) ;
3045
3146 const logData : Record < string , unknown > = {
3247 update_id : ctx . update . update_id ,
@@ -40,24 +55,21 @@ async function logging(ctx: BotContext, next: NextFunction): Promise<void> {
4055 language_code : ctx . from ?. language_code ,
4156 chat_id : ctx . chat ?. id ,
4257 chat_type : ctx . chat ?. type ,
43- chat_title : ctx . chat ?. type !== "private" ? ctx . chat ?. title : undefined ,
4458 } ;
4559
4660 // Message-specific logging
4761 if ( ctx . message ) {
4862 Object . assign ( logData , {
4963 message_id : ctx . message . message_id ,
50- text : ctx . message . text ?. slice ( 0 , 100 ) , // Truncate long messages
64+ text : ctx . message . text ?. slice ( 0 , MAX_TEXT_LOG_LENGTH ) ,
5165 text_length : ctx . message . text ?. length ,
5266 command : ctx . message . text ?. startsWith ( "/" )
5367 ? ctx . message . text . split ( " " ) [ 0 ]
5468 : undefined ,
5569 message_date : ctx . message . date ,
56- has_entities : ctx . message . entities && ctx . message . entities . length > 0 ,
70+ has_entities : ( ctx . message . entities ?. length ?? 0 ) > 0 ,
5771 entity_types : ctx . message . entities ?. map ( e => e . type ) ,
58- media_type : getMediaType (
59- ctx . message as unknown as Record < string , unknown >
60- ) ,
72+ media_type : getMediaType ( ctx . message ) ,
6173 is_forwarded : ! ! ctx . message . forward_origin ,
6274 is_reply : ! ! ctx . message . reply_to_message ,
6375 reply_to_message_id : ctx . message . reply_to_message ?. message_id ,
@@ -78,7 +90,7 @@ async function logging(ctx: BotContext, next: NextFunction): Promise<void> {
7890 // Inline query logging
7991 if ( ctx . inlineQuery ) {
8092 Object . assign ( logData , {
81- query : ctx . inlineQuery . query ?. slice ( 0 , 50 ) , // Truncate long queries
93+ query : ctx . inlineQuery . query ?. slice ( 0 , MAX_QUERY_LOG_LENGTH ) ,
8294 query_length : ctx . inlineQuery . query ?. length ,
8395 inline_query_id : ctx . inlineQuery . id ,
8496 offset : ctx . inlineQuery . offset ,
@@ -91,7 +103,7 @@ async function logging(ctx: BotContext, next: NextFunction): Promise<void> {
91103 if ( ctx . editedMessage ) {
92104 Object . assign ( logData , {
93105 edited_message_id : ctx . editedMessage . message_id ,
94- edited_text : ctx . editedMessage . text ?. slice ( 0 , 100 ) ,
106+ edited_text : ctx . editedMessage . text ?. slice ( 0 , MAX_TEXT_LOG_LENGTH ) ,
95107 edit_date : ctx . editedMessage . edit_date ,
96108 } ) ;
97109 }
@@ -116,7 +128,7 @@ async function logging(ctx: BotContext, next: NextFunction): Promise<void> {
116128 logger . info (
117129 {
118130 chat_id : ctx . chat ?. id ,
119- updated_id : ctx . update . update_id ,
131+ update_id : ctx . update . update_id ,
120132 update_type : updateType ,
121133 response_time_ms : duration ,
122134 } ,
0 commit comments