@@ -95,7 +95,7 @@ export class MessageManager {
9595 try {
9696 let imageUrl : string | null = null ;
9797
98- logger . info ( `Telegram Message: ${ JSON . stringify ( message , null , 2 ) } ` ) ;
98+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , messageId : message . message_id } , 'Processing image from message' ) ;
9999
100100 if ( 'photo' in message && message . photo ?. length > 0 ) {
101101 const photo = message . photo [ message . photo . length - 1 ] ;
@@ -118,7 +118,7 @@ export class MessageManager {
118118 return { description : `[Image: ${ title } \n${ description } ]` } ;
119119 }
120120 } catch ( error ) {
121- console . error ( '❌ Error processing image:' , error ) ;
121+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , error : error instanceof Error ? error . message : String ( error ) } , 'Error processing image' ) ;
122122 }
123123
124124 return null ;
@@ -141,9 +141,7 @@ export class MessageManager {
141141 const fileLink = await this . bot . telegram . getFileLink ( document . file_id ) ;
142142 const documentUrl = fileLink . toString ( ) ;
143143
144- logger . info (
145- `Processing document: ${ document . file_name } (${ document . mime_type } , ${ document . file_size } bytes)`
146- ) ;
144+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : document . file_name , mimeType : document . mime_type , fileSize : document . file_size } , 'Processing document' ) ;
147145
148146 // Centralized document processing based on MIME type
149147 const documentProcessor = this . getDocumentProcessor ( document . mime_type ) ;
@@ -161,7 +159,7 @@ export class MessageManager {
161159 fileSize : document . file_size ,
162160 } ;
163161 } catch ( error ) {
164- logger . error ( { error } , 'Error processing document' ) ;
162+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , error : error instanceof Error ? error . message : String ( error ) } , 'Error processing document' ) ;
165163 return null ;
166164 }
167165 }
@@ -199,7 +197,7 @@ export class MessageManager {
199197 try {
200198 const pdfService = this . runtime . getService ( ServiceType . PDF ) as any ;
201199 if ( ! pdfService ) {
202- logger . warn ( 'PDF service not available, using fallback' ) ;
200+ logger . warn ( { src : 'plugin:telegram' , agentId : this . runtime . agentId } , 'PDF service not available, using fallback' ) ;
203201 return {
204202 title : `PDF Document: ${ document . file_name || 'Unknown Document' } ` ,
205203 fullText : '' ,
@@ -218,7 +216,7 @@ export class MessageManager {
218216 const pdfBuffer = await response . arrayBuffer ( ) ;
219217 const text = await pdfService . convertPdfToText ( Buffer . from ( pdfBuffer ) ) ;
220218
221- logger . info ( `PDF processed successfully: ${ text . length } characters extracted` ) ;
219+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : document . file_name , charactersExtracted : text . length } , 'PDF processed successfully' ) ;
222220 return {
223221 title : document . file_name || 'Unknown Document' ,
224222 fullText : text ,
@@ -228,7 +226,7 @@ export class MessageManager {
228226 fileSize : document . file_size ,
229227 } ;
230228 } catch ( error ) {
231- logger . error ( { error } , 'Error processing PDF document' ) ;
229+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : document . file_name , error : error instanceof Error ? error . message : String ( error ) } , 'Error processing PDF document' ) ;
232230 return {
233231 title : `PDF Document: ${ document . file_name || 'Unknown Document' } ` ,
234232 fullText : '' ,
@@ -255,7 +253,7 @@ export class MessageManager {
255253
256254 const text = await response . text ( ) ;
257255
258- logger . info ( `Text document processed successfully: ${ text . length } characters extracted` ) ;
256+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : document . file_name , charactersExtracted : text . length } , 'Text document processed successfully' ) ;
259257 return {
260258 title : document . file_name || 'Unknown Document' ,
261259 fullText : text ,
@@ -265,7 +263,7 @@ export class MessageManager {
265263 fileSize : document . file_size ,
266264 } ;
267265 } catch ( error ) {
268- logger . error ( { error } , 'Error processing text document' ) ;
266+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : document . file_name , error : error instanceof Error ? error . message : String ( error ) } , 'Error processing text document' ) ;
269267 return {
270268 title : `Text Document: ${ document . file_name || 'Unknown Document' } ` ,
271269 fullText : '' ,
@@ -324,9 +322,9 @@ export class MessageManager {
324322 description : documentInfo . formattedDescription ,
325323 text : fullText ,
326324 } ) ;
327- logger . info ( `Document processed successfully: ${ documentInfo . fileName } ` ) ;
325+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : documentInfo . fileName } , 'Document processed successfully' ) ;
328326 } catch ( error ) {
329- logger . error ( { error } , `Error processing document ${ documentInfo . fileName } ` ) ;
327+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , fileName : documentInfo . fileName , error : error instanceof Error ? error . message : String ( error ) } , 'Error processing document' ) ;
330328 // Add a fallback attachment even if processing failed
331329 attachments . push ( {
332330 id : document . file_id ,
@@ -367,9 +365,7 @@ export class MessageManager {
367365 }
368366 }
369367
370- logger . info (
371- `Message processed - Content: ${ processedContent ? 'yes' : 'no' } , Attachments: ${ attachments . length } `
372- ) ;
368+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , hasContent : ! ! processedContent , attachmentsCount : attachments . length } , 'Message processed' ) ;
373369
374370 return { processedContent, attachments } ;
375371 }
@@ -422,15 +418,15 @@ export class MessageManager {
422418 const telegramButtons = convertToTelegramButtons ( content . buttons ?? [ ] ) ;
423419
424420 if ( ! ctx . chat ) {
425- logger . error ( 'sendMessageInChunks: ctx.chat is undefined' ) ;
421+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId } , 'sendMessageInChunks: ctx.chat is undefined' ) ;
426422 return [ ] ;
427423 }
428424 await ctx . telegram . sendChatAction ( ctx . chat . id , 'typing' ) ;
429425
430426 for ( let i = 0 ; i < chunks . length ; i ++ ) {
431427 const chunk = convertMarkdownToTelegram ( chunks [ i ] ) ;
432428 if ( ! ctx . chat ) {
433- logger . error ( 'sendMessageInChunks loop: ctx.chat is undefined' ) ;
429+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId } , 'sendMessageInChunks loop: ctx.chat is undefined' ) ;
434430 continue ;
435431 }
436432 const sentMessage = ( await ctx . telegram . sendMessage ( ctx . chat . id , chunk , {
@@ -504,15 +500,9 @@ export class MessageManager {
504500 }
505501 }
506502
507- logger . info (
508- `${ type . charAt ( 0 ) . toUpperCase ( ) + type . slice ( 1 ) } sent successfully: ${ mediaPath } `
509- ) ;
503+ logger . debug ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , mediaType : type , mediaPath } , 'Media sent successfully' ) ;
510504 } catch ( error ) {
511- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
512- logger . error (
513- { originalError : error } ,
514- `Failed to send ${ type } . Path: ${ mediaPath } . Error: ${ errorMessage } `
515- ) ;
505+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , mediaType : type , mediaPath, error : error instanceof Error ? error . message : String ( error ) } , 'Failed to send media' ) ;
516506 throw error ;
517507 }
518508 }
@@ -564,7 +554,7 @@ export class MessageManager {
564554
565555 // Add null check for ctx.chat
566556 if ( ! ctx . chat ) {
567- logger . error ( 'handleMessage: ctx.chat is undefined' ) ;
557+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId } , 'handleMessage: ctx.chat is undefined' ) ;
568558 return ;
569559 }
570560 // Generate room ID based on whether this is in a forum topic
@@ -687,7 +677,7 @@ export class MessageManager {
687677
688678 return memories ;
689679 } catch ( error ) {
690- logger . error ( { error } , 'Error in message callback' ) ;
680+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , error : error instanceof Error ? error . message : String ( error ) } , 'Error in message callback' ) ;
691681 return [ ] ;
692682 }
693683 } ;
@@ -703,21 +693,13 @@ export class MessageManager {
703693 } else if ( this . runtime . messageService ) {
704694 await this . runtime . messageService . handleMessage ( this . runtime , memory , callback ) ;
705695 } else {
706- logger . error ( 'Message service is not available' ) ;
696+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId } , 'Message service is not available' ) ;
707697 throw new Error (
708698 'Message service is not initialized. Ensure the message service is properly configured.'
709699 ) ;
710700 }
711701 } catch ( error ) {
712- logger . error (
713- {
714- error,
715- chatId : ctx . chat ?. id ,
716- messageId : ctx . message ?. message_id ,
717- from : ctx . from ?. username || ctx . from ?. id ,
718- } ,
719- 'Error handling Telegram message'
720- ) ;
702+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , chatId : ctx . chat ?. id , messageId : ctx . message ?. message_id , from : ctx . from ?. username || ctx . from ?. id , error : error instanceof Error ? error . message : String ( error ) } , 'Error handling Telegram message' ) ;
721703 throw error ;
722704 }
723705 }
@@ -789,7 +771,7 @@ export class MessageManager {
789771 } ;
790772 return [ responseMemory ] ;
791773 } catch ( error ) {
792- logger . error ( { error } , 'Error in reaction callback' ) ;
774+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , error : error instanceof Error ? error . message : String ( error ) } , 'Error in reaction callback' ) ;
793775 return [ ] ;
794776 }
795777 } ;
@@ -818,14 +800,7 @@ export class MessageManager {
818800 originalReaction : reaction . new_reaction [ 0 ] as ReactionType ,
819801 } as TelegramReactionReceivedPayload ) ;
820802 } catch ( error ) {
821- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
822- logger . error (
823- {
824- error : errorMessage ,
825- originalError : error ,
826- } ,
827- 'Error handling reaction'
828- ) ;
803+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , error : error instanceof Error ? error . message : String ( error ) } , 'Error handling reaction' ) ;
829804 }
830805 }
831806
@@ -901,14 +876,7 @@ export class MessageManager {
901876
902877 return sentMessages ;
903878 } catch ( error ) {
904- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
905- logger . error (
906- {
907- error : errorMessage ,
908- originalError : error ,
909- } ,
910- 'Error sending message to Telegram'
911- ) ;
879+ logger . error ( { src : 'plugin:telegram' , agentId : this . runtime . agentId , chatId, error : error instanceof Error ? error . message : String ( error ) } , 'Error sending message to Telegram' ) ;
912880 return [ ] ;
913881 }
914882 }
0 commit comments