1
1
import { Message } from "@telegraf/types" ;
2
2
import { Context , Telegraf } from "telegraf" ;
3
- import { composeContext , elizaLogger , ServiceType , composeRandomUser } from "@elizaos/core" ;
3
+
4
+ import { composeContext , elizaLogger , ServiceType } from "@elizaos/core" ;
4
5
import { getEmbeddingZeroVector } from "@elizaos/core" ;
5
6
import {
6
7
Content ,
@@ -18,7 +19,7 @@ import { stringToUuid } from "@elizaos/core";
18
19
import { generateMessageResponse , generateShouldRespond } from "@elizaos/core" ;
19
20
import { messageCompletionFooter , shouldRespondFooter } from "@elizaos/core" ;
20
21
21
- import { cosineSimilarity , escapeMarkdown } from "./utils" ;
22
+ import { cosineSimilarity } from "./utils" ;
22
23
import {
23
24
MESSAGE_CONSTANTS ,
24
25
TIMING_CONSTANTS ,
@@ -296,8 +297,8 @@ export class MessageManager {
296
297
"text" in message
297
298
? message . text
298
299
: "caption" in message
299
- ? ( message as any ) . caption
300
- : "" ;
300
+ ? ( message as any ) . caption
301
+ : "" ;
301
302
302
303
if ( ! messageText ) return false ;
303
304
@@ -359,8 +360,8 @@ export class MessageManager {
359
360
"text" in message
360
361
? message . text
361
362
: "caption" in message
362
- ? ( message as any ) . caption
363
- : "" ;
363
+ ? ( message as any ) . caption
364
+ : "" ;
364
365
if ( ! messageText ) return false ;
365
366
366
367
const isReplyToBot =
@@ -375,7 +376,7 @@ export class MessageManager {
375
376
isReplyToBot ||
376
377
isMentioned ||
377
378
( ! this . runtime . character . clientConfig ?. telegram
378
- ?. shouldRespondOnlyToMentions &&
379
+ ?. shouldRespondOnlyToMentions &&
379
380
hasUsername )
380
381
) ;
381
382
}
@@ -507,8 +508,8 @@ export class MessageManager {
507
508
"text" in message
508
509
? message . text
509
510
: "caption" in message
510
- ? ( message as any ) . caption
511
- : "" ;
511
+ ? ( message as any ) . caption
512
+ : "" ;
512
513
513
514
// Check if team member has direct interest first
514
515
if (
@@ -529,8 +530,8 @@ export class MessageManager {
529
530
const randomDelay =
530
531
Math . floor (
531
532
Math . random ( ) *
532
- ( TIMING_CONSTANTS . TEAM_MEMBER_DELAY_MAX -
533
- TIMING_CONSTANTS . TEAM_MEMBER_DELAY_MIN )
533
+ ( TIMING_CONSTANTS . TEAM_MEMBER_DELAY_MAX -
534
+ TIMING_CONSTANTS . TEAM_MEMBER_DELAY_MIN )
534
535
) + TIMING_CONSTANTS . TEAM_MEMBER_DELAY_MIN ; // 1-3 second random delay
535
536
await new Promise ( ( resolve ) =>
536
537
setTimeout ( resolve , randomDelay )
@@ -556,8 +557,8 @@ export class MessageManager {
556
557
const leaderResponded = recentMessages . some (
557
558
( m ) =>
558
559
m . userId ===
559
- this . runtime . character . clientConfig ?. telegram
560
- ?. teamLeaderId &&
560
+ this . runtime . character . clientConfig ?. telegram
561
+ ?. teamLeaderId &&
561
562
Date . now ( ) - chatState . lastMessageSent < 3000
562
563
) ;
563
564
@@ -578,8 +579,8 @@ export class MessageManager {
578
579
const randomDelay =
579
580
Math . floor (
580
581
Math . random ( ) *
581
- ( TIMING_CONSTANTS . LEADER_DELAY_MAX -
582
- TIMING_CONSTANTS . LEADER_DELAY_MIN )
582
+ ( TIMING_CONSTANTS . LEADER_DELAY_MAX -
583
+ TIMING_CONSTANTS . LEADER_DELAY_MIN )
583
584
) + TIMING_CONSTANTS . LEADER_DELAY_MIN ; // 2-4 second random delay
584
585
await new Promise ( ( resolve ) =>
585
586
setTimeout ( resolve , randomDelay )
@@ -617,7 +618,7 @@ export class MessageManager {
617
618
if ( chatState ?. currentHandler ) {
618
619
if (
619
620
chatState . currentHandler !==
620
- this . bot . botInfo ?. id . toString ( ) &&
621
+ this . bot . botInfo ?. id . toString ( ) &&
621
622
this . _isTeamMember ( chatState . currentHandler )
622
623
) {
623
624
return false ;
@@ -628,7 +629,7 @@ export class MessageManager {
628
629
if ( ! this . _isMessageForMe ( message ) && this . interestChats [ chatId ] ) {
629
630
const recentMessages = this . interestChats [
630
631
chatId
631
- ] . messages . slice ( - MESSAGE_CONSTANTS . CHAT_HISTORY_COUNT ) ;
632
+ ] . messages . slice ( - MESSAGE_CONSTANTS . CHAT_HISTORY_COUNT ) ;
632
633
const ourMessageCount = recentMessages . filter (
633
634
( m ) => m . userId === this . runtime . agentId
634
635
) . length ;
@@ -660,7 +661,7 @@ export class MessageManager {
660
661
this . runtime . character . templates
661
662
?. telegramShouldRespondTemplate ||
662
663
this . runtime . character ?. templates ?. shouldRespondTemplate ||
663
- composeRandomUser ( telegramShouldRespondTemplate , 2 ) ,
664
+ telegramShouldRespondTemplate ,
664
665
} ) ;
665
666
666
667
const response = await generateShouldRespond ( {
@@ -684,15 +685,25 @@ export class MessageManager {
684
685
if ( content . attachments && content . attachments . length > 0 ) {
685
686
content . attachments . map ( async ( attachment : Media ) => {
686
687
if ( attachment . contentType . startsWith ( "image" ) ) {
687
- this . sendImage ( ctx , attachment . url , attachment . description ) ;
688
+ await this . sendImage ( ctx , attachment . url , attachment . description ) ;
689
+ } else if ( attachment . contentType . startsWith ( "doc" ) ) {
690
+ await this . sendDocument (
691
+ ctx ,
692
+ attachment . url ,
693
+ attachment . description
694
+ ) ;
695
+ } else if ( attachment . contentType . startsWith ( "video" ) ) {
696
+ await this . sendVideo ( ctx , attachment . url , attachment . description ) ;
697
+ } else if ( attachment . contentType . startsWith ( "audio" ) ) {
698
+ await this . sendAudio ( ctx , attachment . url , attachment . description ) ;
688
699
}
689
700
} ) ;
690
701
} else {
691
702
const chunks = this . splitMessage ( content . text ) ;
692
703
const sentMessages : Message . TextMessage [ ] = [ ] ;
693
704
694
705
for ( let i = 0 ; i < chunks . length ; i ++ ) {
695
- const chunk = escapeMarkdown ( chunks [ i ] ) ;
706
+ const chunk = chunks [ i ] ;
696
707
const sentMessage = ( await ctx . telegram . sendMessage (
697
708
ctx . chat . id ,
698
709
chunk ,
@@ -712,42 +723,84 @@ export class MessageManager {
712
723
}
713
724
}
714
725
715
- private async sendImage (
726
+ private async sendMedia (
716
727
ctx : Context ,
717
- imagePath : string ,
728
+ mediaPath : string ,
729
+ type : "photo" | "video" | "document" | "audio" ,
718
730
caption ?: string
719
731
) : Promise < void > {
720
732
try {
721
- if ( / ^ ( h t t p | h t t p s ) : \/ \/ / . test ( imagePath ) ) {
733
+ const isUrl = / ^ ( h t t p | h t t p s ) : \/ \/ / . test ( mediaPath ) ;
734
+ const sendFunctionMap = {
735
+ photo : ctx . telegram . sendPhoto . bind ( ctx . telegram ) ,
736
+ video : ctx . telegram . sendVideo . bind ( ctx . telegram ) ,
737
+ document : ctx . telegram . sendDocument . bind ( ctx . telegram ) ,
738
+ audio : ctx . telegram . sendAudio . bind ( ctx . telegram ) ,
739
+ } ;
740
+
741
+ if ( ! sendFunctionMap [ type ] ) {
742
+ throw new Error ( `Unsupported media type: ${ type } ` ) ;
743
+ }
744
+
745
+ const sendFunction = sendFunctionMap [ type ] ;
746
+
747
+ if ( isUrl ) {
722
748
// Handle HTTP URLs
723
- await ctx . telegram . sendPhoto ( ctx . chat . id , imagePath , {
724
- caption,
725
- } ) ;
749
+ await sendFunction ( ctx . chat . id , mediaPath , { caption } ) ;
726
750
} else {
727
751
// Handle local file paths
728
- if ( ! fs . existsSync ( imagePath ) ) {
729
- throw new Error ( `File not found: ${ imagePath } ` ) ;
752
+ if ( ! fs . existsSync ( mediaPath ) ) {
753
+ throw new Error ( `File not found: ${ mediaPath } ` ) ;
730
754
}
731
755
732
- const fileStream = fs . createReadStream ( imagePath ) ;
733
-
734
- await ctx . telegram . sendPhoto (
756
+ const fileStream = fs . createReadStream ( mediaPath ) ;
757
+ await sendFunction (
735
758
ctx . chat . id ,
736
- {
737
- source : fileStream ,
738
- } ,
739
- {
740
- caption,
741
- }
759
+ { source : fileStream } ,
760
+ { caption }
742
761
) ;
743
762
}
744
763
745
- elizaLogger . info ( `Image sent successfully: ${ imagePath } ` ) ;
764
+ elizaLogger . info (
765
+ `${ type . charAt ( 0 ) . toUpperCase ( ) + type . slice ( 1 ) } sent successfully: ${ mediaPath } `
766
+ ) ;
746
767
} catch ( error ) {
747
- elizaLogger . error ( " Error sending image:" , error ) ;
768
+ elizaLogger . error ( ` Error sending ${ type } :` , error ) ;
748
769
}
749
770
}
750
771
772
+ private async sendImage (
773
+ ctx : Context ,
774
+ imagePath : string ,
775
+ caption ?: string
776
+ ) : Promise < void > {
777
+ await this . sendMedia ( ctx , imagePath , "photo" , caption ) ;
778
+ }
779
+
780
+ private async sendVideo (
781
+ ctx : Context ,
782
+ videoPath : string ,
783
+ caption ?: string
784
+ ) : Promise < void > {
785
+ await this . sendMedia ( ctx , videoPath , "video" , caption ) ;
786
+ }
787
+
788
+ private async sendDocument (
789
+ ctx : Context ,
790
+ documentPath : string ,
791
+ caption ?: string
792
+ ) : Promise < void > {
793
+ await this . sendMedia ( ctx , documentPath , "document" , caption ) ;
794
+ }
795
+
796
+ private async sendAudio (
797
+ ctx : Context ,
798
+ audioPath : string ,
799
+ caption ?: string
800
+ ) : Promise < void > {
801
+ await this . sendMedia ( ctx , audioPath , "audio" , caption ) ;
802
+ }
803
+
751
804
// Split message into smaller parts
752
805
private splitMessage ( text : string ) : string [ ] {
753
806
const chunks : string [ ] = [ ] ;
@@ -823,8 +876,8 @@ export class MessageManager {
823
876
"text" in message
824
877
? message . text
825
878
: "caption" in message
826
- ? ( message as any ) . caption
827
- : "" ;
879
+ ? ( message as any ) . caption
880
+ : "" ;
828
881
829
882
// Add team handling at the start
830
883
if (
@@ -914,7 +967,7 @@ export class MessageManager {
914
967
if (
915
968
hasInterest ||
916
969
this . interestChats [ chatId ] ?. currentHandler ===
917
- this . bot . botInfo ?. id . toString ( )
970
+ this . bot . botInfo ?. id . toString ( )
918
971
) {
919
972
delete this . interestChats [ chatId ] ;
920
973
@@ -953,7 +1006,7 @@ export class MessageManager {
953
1006
) {
954
1007
this . interestChats [ chatId ] . messages = this . interestChats [
955
1008
chatId
956
- ] . messages . slice ( - MESSAGE_CONSTANTS . MAX_MESSAGES ) ;
1009
+ ] . messages . slice ( - MESSAGE_CONSTANTS . MAX_MESSAGES ) ;
957
1010
}
958
1011
}
959
1012
}
@@ -1018,10 +1071,10 @@ export class MessageManager {
1018
1071
inReplyTo :
1019
1072
"reply_to_message" in message && message . reply_to_message
1020
1073
? stringToUuid (
1021
- message . reply_to_message . message_id . toString ( ) +
1022
- "-" +
1023
- this . runtime . agentId
1024
- )
1074
+ message . reply_to_message . message_id . toString ( ) +
1075
+ "-" +
1076
+ this . runtime . agentId
1077
+ )
1025
1078
: undefined ,
1026
1079
} ;
1027
1080
@@ -1084,8 +1137,8 @@ export class MessageManager {
1084
1137
const memory : Memory = {
1085
1138
id : stringToUuid (
1086
1139
sentMessage . message_id . toString ( ) +
1087
- "-" +
1088
- this . runtime . agentId
1140
+ "-" +
1141
+ this . runtime . agentId
1089
1142
) ,
1090
1143
agentId,
1091
1144
userId : agentId ,
0 commit comments