@elizaos/plugin-telegram 1.0.0-beta.13 → 1.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +324 -123
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -575,46 +575,77 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
575
575
|
* The middleware chain runs in sequence for each message, with each step potentially
|
|
576
576
|
* enriching the context or stopping processing if conditions aren't met.
|
|
577
577
|
* This preprocessing is essential for maintaining consistent state before message handlers execute.
|
|
578
|
+
*
|
|
578
579
|
* @private
|
|
579
580
|
*/
|
|
580
581
|
setupMiddlewares() {
|
|
581
|
-
this.bot.use(
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
582
|
+
this.bot.use(this.authorizationMiddleware.bind(this));
|
|
583
|
+
this.bot.use(this.chatAndEntityMiddleware.bind(this));
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Authorization middleware - checks if chat is allowed to interact with the bot
|
|
587
|
+
* based on the TELEGRAM_ALLOWED_CHATS configuration.
|
|
588
|
+
*
|
|
589
|
+
* @param {Context} ctx - The context of the incoming update
|
|
590
|
+
* @param {Function} next - The function to call to proceed to the next middleware
|
|
591
|
+
* @returns {Promise<void>}
|
|
592
|
+
* @private
|
|
593
|
+
*/
|
|
594
|
+
async authorizationMiddleware(ctx, next) {
|
|
595
|
+
if (!await this.isGroupAuthorized(ctx)) {
|
|
596
|
+
logger2.debug("Chat not authorized, skipping message processing");
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
await next();
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Chat and entity management middleware - handles new chats, forum topics, and entity synchronization.
|
|
603
|
+
* This middleware implements decision logic to determine which operations are needed based on
|
|
604
|
+
* the chat type and whether we've seen this chat before.
|
|
605
|
+
*
|
|
606
|
+
* @param {Context} ctx - The context of the incoming update
|
|
607
|
+
* @param {Function} next - The function to call to proceed to the next middleware
|
|
608
|
+
* @returns {Promise<void>}
|
|
609
|
+
* @private
|
|
610
|
+
*/
|
|
611
|
+
async chatAndEntityMiddleware(ctx, next) {
|
|
612
|
+
if (!ctx.chat) return next();
|
|
613
|
+
const chatId = ctx.chat.id.toString();
|
|
614
|
+
if (!this.knownChats.has(chatId)) {
|
|
615
|
+
await this.handleNewChat(ctx);
|
|
616
|
+
return next();
|
|
617
|
+
}
|
|
618
|
+
await this.processExistingChat(ctx);
|
|
619
|
+
await next();
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Process an existing chat based on chat type and message properties.
|
|
623
|
+
* Different chat types require different processing steps.
|
|
624
|
+
*
|
|
625
|
+
* @param {Context} ctx - The context of the incoming update
|
|
626
|
+
* @returns {Promise<void>}
|
|
627
|
+
* @private
|
|
628
|
+
*/
|
|
629
|
+
async processExistingChat(ctx) {
|
|
630
|
+
var _a;
|
|
631
|
+
if (!ctx.chat) return;
|
|
632
|
+
const chat = ctx.chat;
|
|
633
|
+
if (chat.type === "supergroup" && chat.is_forum && ((_a = ctx.message) == null ? void 0 : _a.message_thread_id)) {
|
|
634
|
+
try {
|
|
635
|
+
await this.handleForumTopic(ctx);
|
|
636
|
+
} catch (error) {
|
|
637
|
+
logger2.error(`Error handling forum topic: ${error}`);
|
|
604
638
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
this.bot.use(async (ctx, next) => {
|
|
608
|
-
if (!ctx.chat || !ctx.from || ctx.chat.type === "private") return next();
|
|
639
|
+
}
|
|
640
|
+
if (ctx.from && ctx.chat.type !== "private") {
|
|
609
641
|
await this.syncEntity(ctx);
|
|
610
|
-
|
|
611
|
-
});
|
|
642
|
+
}
|
|
612
643
|
}
|
|
613
644
|
/**
|
|
614
645
|
* Sets up message and reaction handlers for the bot.
|
|
646
|
+
* Configures event handlers to process incoming messages and reactions.
|
|
615
647
|
*
|
|
616
648
|
* @private
|
|
617
|
-
* @returns {void}
|
|
618
649
|
*/
|
|
619
650
|
setupMessageHandlers() {
|
|
620
651
|
this.bot.on("message", async (ctx) => {
|
|
@@ -654,54 +685,90 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
654
685
|
}
|
|
655
686
|
}
|
|
656
687
|
/**
|
|
657
|
-
* Synchronizes an entity from a message context
|
|
688
|
+
* Synchronizes an entity from a message context with the runtime system.
|
|
689
|
+
* This method handles three cases:
|
|
690
|
+
* 1. Message sender - most common case
|
|
691
|
+
* 2. New chat member - when a user joins the chat
|
|
692
|
+
* 3. Left chat member - when a user leaves the chat
|
|
693
|
+
*
|
|
658
694
|
* @param {Context} ctx - The context of the incoming update
|
|
659
695
|
* @returns {Promise<void>}
|
|
696
|
+
* @private
|
|
660
697
|
*/
|
|
661
698
|
async syncEntity(ctx) {
|
|
699
|
+
var _a;
|
|
662
700
|
if (!ctx.chat) return;
|
|
663
|
-
|
|
701
|
+
const chat = ctx.chat;
|
|
702
|
+
const chatId = chat.id.toString();
|
|
703
|
+
const worldId = createUniqueUuid2(this.runtime, chatId);
|
|
704
|
+
const roomId = createUniqueUuid2(
|
|
705
|
+
this.runtime,
|
|
706
|
+
((_a = ctx.message) == null ? void 0 : _a.message_thread_id) ? `${ctx.chat.id}-${ctx.message.message_thread_id}` : ctx.chat.id.toString()
|
|
707
|
+
);
|
|
708
|
+
await this.syncMessageSender(ctx, worldId, roomId, chatId);
|
|
709
|
+
await this.syncNewChatMember(ctx, worldId, roomId, chatId);
|
|
710
|
+
await this.syncLeftChatMember(ctx);
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Synchronizes the message sender entity with the runtime system.
|
|
714
|
+
* This is the most common entity sync case.
|
|
715
|
+
*
|
|
716
|
+
* @param {Context} ctx - The context of the incoming update
|
|
717
|
+
* @param {UUID} worldId - The ID of the world
|
|
718
|
+
* @param {UUID} roomId - The ID of the room
|
|
719
|
+
* @param {string} chatId - The ID of the chat
|
|
720
|
+
* @returns {Promise<void>}
|
|
721
|
+
* @private
|
|
722
|
+
*/
|
|
723
|
+
async syncMessageSender(ctx, worldId, roomId, chatId) {
|
|
724
|
+
if (ctx.from && !this.syncedEntityIds.has(ctx.from.id.toString())) {
|
|
664
725
|
const telegramId = ctx.from.id.toString();
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
}
|
|
680
|
-
});
|
|
681
|
-
}
|
|
682
|
-
this.syncedEntityIds.add(telegramId);
|
|
683
|
-
}
|
|
726
|
+
const entityId = createUniqueUuid2(this.runtime, telegramId);
|
|
727
|
+
await this.runtime.ensureConnection({
|
|
728
|
+
entityId,
|
|
729
|
+
roomId,
|
|
730
|
+
userName: ctx.from.username,
|
|
731
|
+
userId: telegramId,
|
|
732
|
+
name: ctx.from.first_name || ctx.from.username || "Unknown User",
|
|
733
|
+
source: "telegram",
|
|
734
|
+
channelId: chatId,
|
|
735
|
+
serverId: chatId,
|
|
736
|
+
type: ChannelType2.GROUP,
|
|
737
|
+
worldId
|
|
738
|
+
});
|
|
739
|
+
this.syncedEntityIds.add(entityId);
|
|
684
740
|
}
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Synchronizes a new chat member entity with the runtime system.
|
|
744
|
+
* Triggered when a user joins the chat.
|
|
745
|
+
*
|
|
746
|
+
* @param {Context} ctx - The context of the incoming update
|
|
747
|
+
* @param {UUID} worldId - The ID of the world
|
|
748
|
+
* @param {UUID} roomId - The ID of the room
|
|
749
|
+
* @param {string} chatId - The ID of the chat
|
|
750
|
+
* @returns {Promise<void>}
|
|
751
|
+
* @private
|
|
752
|
+
*/
|
|
753
|
+
async syncNewChatMember(ctx, worldId, roomId, chatId) {
|
|
685
754
|
if (ctx.message && "new_chat_member" in ctx.message) {
|
|
686
755
|
const newMember = ctx.message.new_chat_member;
|
|
687
756
|
const telegramId = newMember.id.toString();
|
|
688
757
|
const entityId = createUniqueUuid2(this.runtime, telegramId);
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
joinedAt: Date.now()
|
|
702
|
-
}
|
|
758
|
+
if (this.syncedEntityIds.has(telegramId)) return;
|
|
759
|
+
await this.runtime.ensureConnection({
|
|
760
|
+
entityId,
|
|
761
|
+
roomId,
|
|
762
|
+
userName: newMember.username,
|
|
763
|
+
userId: telegramId,
|
|
764
|
+
name: newMember.first_name || newMember.username || "Unknown User",
|
|
765
|
+
source: "telegram",
|
|
766
|
+
channelId: chatId,
|
|
767
|
+
serverId: chatId,
|
|
768
|
+
type: ChannelType2.GROUP,
|
|
769
|
+
worldId
|
|
703
770
|
});
|
|
704
|
-
this.syncedEntityIds.add(
|
|
771
|
+
this.syncedEntityIds.add(entityId);
|
|
705
772
|
this.runtime.emitEvent(["TELEGRAM_ENTITY_JOINED" /* ENTITY_JOINED */], {
|
|
706
773
|
runtime: this.runtime,
|
|
707
774
|
entityId,
|
|
@@ -710,6 +777,15 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
710
777
|
ctx
|
|
711
778
|
});
|
|
712
779
|
}
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Updates entity status when a user leaves the chat.
|
|
783
|
+
*
|
|
784
|
+
* @param {Context} ctx - The context of the incoming update
|
|
785
|
+
* @returns {Promise<void>}
|
|
786
|
+
* @private
|
|
787
|
+
*/
|
|
788
|
+
async syncLeftChatMember(ctx) {
|
|
713
789
|
if (ctx.message && "left_chat_member" in ctx.message) {
|
|
714
790
|
const leftMember = ctx.message.left_chat_member;
|
|
715
791
|
const telegramId = leftMember.id.toString();
|
|
@@ -726,65 +802,61 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
726
802
|
}
|
|
727
803
|
}
|
|
728
804
|
/**
|
|
729
|
-
* Handles forum topics by creating appropriate rooms
|
|
805
|
+
* Handles forum topics by creating appropriate rooms in the runtime system.
|
|
806
|
+
* This enables proper conversation management for Telegram's forum feature.
|
|
807
|
+
*
|
|
730
808
|
* @param {Context} ctx - The context of the incoming update
|
|
809
|
+
* @returns {Promise<void>}
|
|
810
|
+
* @private
|
|
731
811
|
*/
|
|
732
812
|
async handleForumTopic(ctx) {
|
|
733
813
|
var _a;
|
|
734
814
|
if (!ctx.chat || !((_a = ctx.message) == null ? void 0 : _a.message_thread_id)) return;
|
|
735
815
|
const chat = ctx.chat;
|
|
736
816
|
const chatId = chat.id.toString();
|
|
737
|
-
const threadId = ctx.message.message_thread_id.toString();
|
|
738
817
|
const worldId = createUniqueUuid2(this.runtime, chatId);
|
|
739
|
-
const
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
818
|
+
const room = await this.buildForumTopicRoom(ctx, worldId);
|
|
819
|
+
if (!room) return;
|
|
820
|
+
await this.runtime.ensureRoomExists(room);
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Builds entity for message sender
|
|
824
|
+
*/
|
|
825
|
+
buildMsgSenderEntity(from) {
|
|
826
|
+
if (!from) return null;
|
|
827
|
+
const userId = createUniqueUuid2(this.runtime, from.id.toString());
|
|
828
|
+
const telegramId = from.id.toString();
|
|
829
|
+
return {
|
|
830
|
+
id: userId,
|
|
831
|
+
agentId: this.runtime.agentId,
|
|
832
|
+
names: [from.first_name || from.username || "Unknown User"],
|
|
833
|
+
metadata: {
|
|
834
|
+
telegram: {
|
|
835
|
+
id: telegramId,
|
|
836
|
+
username: from.username,
|
|
837
|
+
name: from.first_name || from.username || "Unknown User"
|
|
751
838
|
}
|
|
752
839
|
}
|
|
753
|
-
|
|
754
|
-
id: roomId,
|
|
755
|
-
name: topicName,
|
|
756
|
-
source: "telegram",
|
|
757
|
-
type: ChannelType2.GROUP,
|
|
758
|
-
channelId: `${chatId}-${threadId}`,
|
|
759
|
-
serverId: chatId,
|
|
760
|
-
worldId,
|
|
761
|
-
metadata: {
|
|
762
|
-
threadId,
|
|
763
|
-
isForumTopic: true,
|
|
764
|
-
parentChatId: chatId
|
|
765
|
-
}
|
|
766
|
-
};
|
|
767
|
-
await this.runtime.ensureRoomExists(room);
|
|
768
|
-
logger2.debug(`Created room for forum topic: ${topicName} (${roomId})`);
|
|
769
|
-
} catch (error) {
|
|
770
|
-
logger2.error(
|
|
771
|
-
`Error handling forum topic: ${error instanceof Error ? error.message : String(error)}`
|
|
772
|
-
);
|
|
773
|
-
throw error;
|
|
774
|
-
}
|
|
840
|
+
};
|
|
775
841
|
}
|
|
776
842
|
/**
|
|
777
|
-
* Handles new chat discovery and emits WORLD_JOINED event
|
|
843
|
+
* Handles new chat discovery and emits WORLD_JOINED event.
|
|
844
|
+
* This is a critical function that ensures new chats are properly
|
|
845
|
+
* registered in the runtime system and appropriate events are emitted.
|
|
846
|
+
*
|
|
778
847
|
* @param {Context} ctx - The context of the incoming update
|
|
848
|
+
* @returns {Promise<void>}
|
|
849
|
+
* @private
|
|
779
850
|
*/
|
|
780
851
|
async handleNewChat(ctx) {
|
|
852
|
+
var _a;
|
|
781
853
|
if (!ctx.chat) return;
|
|
782
854
|
const chat = ctx.chat;
|
|
783
855
|
const chatId = chat.id.toString();
|
|
784
856
|
this.knownChats.set(chatId, chat);
|
|
785
857
|
const { chatTitle, channelType } = this.getChatTypeInfo(chat);
|
|
786
858
|
const worldId = createUniqueUuid2(this.runtime, chatId);
|
|
787
|
-
const existingWorld = this.runtime.getWorld(worldId);
|
|
859
|
+
const existingWorld = await this.runtime.getWorld(worldId);
|
|
788
860
|
if (existingWorld) {
|
|
789
861
|
return;
|
|
790
862
|
}
|
|
@@ -818,8 +890,8 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
818
890
|
isForumEnabled: chat.type === "supergroup" && chat.is_forum
|
|
819
891
|
}
|
|
820
892
|
};
|
|
821
|
-
|
|
822
|
-
const
|
|
893
|
+
await this.runtime.ensureWorldExists(world);
|
|
894
|
+
const generalRoom = {
|
|
823
895
|
id: createUniqueUuid2(this.runtime, chatId),
|
|
824
896
|
name: chatTitle,
|
|
825
897
|
source: "telegram",
|
|
@@ -828,29 +900,100 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
828
900
|
serverId: chatId,
|
|
829
901
|
worldId
|
|
830
902
|
};
|
|
831
|
-
|
|
903
|
+
await this.runtime.ensureRoomExists(generalRoom);
|
|
904
|
+
const rooms = [generalRoom];
|
|
905
|
+
if (chat.type === "supergroup" && chat.is_forum && ((_a = ctx.message) == null ? void 0 : _a.message_thread_id)) {
|
|
906
|
+
const topicRoom = await this.buildForumTopicRoom(ctx, worldId);
|
|
907
|
+
if (topicRoom) {
|
|
908
|
+
rooms.push(topicRoom);
|
|
909
|
+
}
|
|
910
|
+
await this.runtime.ensureRoomExists(topicRoom);
|
|
911
|
+
}
|
|
912
|
+
const entities = await this.buildStandardizedEntities(chat);
|
|
913
|
+
if (ctx.from) {
|
|
914
|
+
const senderEntity = this.buildMsgSenderEntity(ctx.from);
|
|
915
|
+
if (senderEntity && !entities.some((e) => e.id === senderEntity.id)) {
|
|
916
|
+
entities.push(senderEntity);
|
|
917
|
+
this.syncedEntityIds.add(senderEntity.id);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
await this.batchProcessEntities(
|
|
921
|
+
entities,
|
|
922
|
+
generalRoom.id,
|
|
923
|
+
generalRoom.channelId,
|
|
924
|
+
generalRoom.serverId,
|
|
925
|
+
generalRoom.type,
|
|
926
|
+
worldId
|
|
927
|
+
);
|
|
928
|
+
const telegramWorldPayload = {
|
|
832
929
|
runtime: this.runtime,
|
|
833
930
|
world,
|
|
834
|
-
rooms
|
|
931
|
+
rooms,
|
|
835
932
|
entities,
|
|
836
933
|
source: "telegram",
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
...worldPayload,
|
|
840
|
-
chat,
|
|
841
|
-
botUsername: this.bot.botInfo.username
|
|
842
|
-
};
|
|
843
|
-
if (chat.type === "group" || chat.type === "supergroup" || chat.type === "channel") {
|
|
844
|
-
this.runtime.emitEvent("TELEGRAM_WORLD_JOINED" /* WORLD_JOINED */, telegramWorldPayload);
|
|
845
|
-
}
|
|
846
|
-
}
|
|
934
|
+
chat,
|
|
935
|
+
botUsername: this.bot.botInfo.username
|
|
847
936
|
};
|
|
848
|
-
|
|
937
|
+
if (chat.type !== "private") {
|
|
938
|
+
await this.runtime.emitEvent("TELEGRAM_WORLD_JOINED" /* WORLD_JOINED */, telegramWorldPayload);
|
|
939
|
+
}
|
|
940
|
+
await this.runtime.emitEvent(EventType2.WORLD_JOINED, {
|
|
941
|
+
runtime: this.runtime,
|
|
942
|
+
world,
|
|
943
|
+
rooms,
|
|
944
|
+
entities,
|
|
945
|
+
source: "telegram"
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Processes entities in batches to prevent overwhelming the system.
|
|
950
|
+
*
|
|
951
|
+
* @param {Entity[]} entities - The entities to process
|
|
952
|
+
* @param {UUID} roomId - The ID of the room to connect entities to
|
|
953
|
+
* @param {string} channelId - The channel ID
|
|
954
|
+
* @param {string} serverId - The server ID
|
|
955
|
+
* @param {ChannelType} roomType - The type of the room
|
|
956
|
+
* @param {UUID} worldId - The ID of the world
|
|
957
|
+
* @returns {Promise<void>}
|
|
958
|
+
* @private
|
|
959
|
+
*/
|
|
960
|
+
async batchProcessEntities(entities, roomId, channelId, serverId, roomType, worldId) {
|
|
961
|
+
const batchSize = 50;
|
|
962
|
+
for (let i = 0; i < entities.length; i += batchSize) {
|
|
963
|
+
const entityBatch = entities.slice(i, i + batchSize);
|
|
964
|
+
await Promise.all(
|
|
965
|
+
entityBatch.map(async (entity) => {
|
|
966
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
967
|
+
try {
|
|
968
|
+
await this.runtime.ensureConnection({
|
|
969
|
+
entityId: entity.id,
|
|
970
|
+
roomId,
|
|
971
|
+
userName: (_b = (_a = entity.metadata) == null ? void 0 : _a.telegram) == null ? void 0 : _b.username,
|
|
972
|
+
name: (_d = (_c = entity.metadata) == null ? void 0 : _c.telegram) == null ? void 0 : _d.name,
|
|
973
|
+
userId: (_f = (_e = entity.metadata) == null ? void 0 : _e.telegram) == null ? void 0 : _f.id,
|
|
974
|
+
source: "telegram",
|
|
975
|
+
channelId,
|
|
976
|
+
serverId,
|
|
977
|
+
type: roomType,
|
|
978
|
+
worldId
|
|
979
|
+
});
|
|
980
|
+
} catch (err) {
|
|
981
|
+
logger2.warn(`Failed to sync user ${(_h = (_g = entity.metadata) == null ? void 0 : _g.telegram) == null ? void 0 : _h.username}: ${err}`);
|
|
982
|
+
}
|
|
983
|
+
})
|
|
984
|
+
);
|
|
985
|
+
if (i + batchSize < entities.length) {
|
|
986
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
987
|
+
}
|
|
988
|
+
}
|
|
849
989
|
}
|
|
850
990
|
/**
|
|
851
|
-
* Gets chat title and channel type based on Telegram chat type
|
|
991
|
+
* Gets chat title and channel type based on Telegram chat type.
|
|
992
|
+
* Maps Telegram-specific chat types to standardized system types.
|
|
993
|
+
*
|
|
852
994
|
* @param {any} chat - The Telegram chat object
|
|
853
995
|
* @returns {Object} Object containing chatTitle and channelType
|
|
996
|
+
* @private
|
|
854
997
|
*/
|
|
855
998
|
getChatTypeInfo(chat) {
|
|
856
999
|
let chatTitle;
|
|
@@ -879,9 +1022,12 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
879
1022
|
return { chatTitle, channelType };
|
|
880
1023
|
}
|
|
881
1024
|
/**
|
|
882
|
-
* Builds standardized entity representations from Telegram chat data
|
|
883
|
-
*
|
|
884
|
-
*
|
|
1025
|
+
* Builds standardized entity representations from Telegram chat data.
|
|
1026
|
+
* Transforms Telegram-specific user data into system-standard Entity objects.
|
|
1027
|
+
*
|
|
1028
|
+
* @param {any} chat - The Telegram chat object
|
|
1029
|
+
* @returns {Promise<Entity[]>} Array of standardized Entity objects
|
|
1030
|
+
* @private
|
|
885
1031
|
*/
|
|
886
1032
|
async buildStandardizedEntities(chat) {
|
|
887
1033
|
const entities = [];
|
|
@@ -901,6 +1047,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
901
1047
|
source: "telegram"
|
|
902
1048
|
}
|
|
903
1049
|
});
|
|
1050
|
+
this.syncedEntityIds.add(userId);
|
|
904
1051
|
} else if (chat.type === "group" || chat.type === "supergroup") {
|
|
905
1052
|
try {
|
|
906
1053
|
const admins = await this.bot.telegram.getChatAdministrators(chat.id);
|
|
@@ -923,6 +1070,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
923
1070
|
roles: [admin.status === "creator" ? Role.OWNER : Role.ADMIN]
|
|
924
1071
|
}
|
|
925
1072
|
});
|
|
1073
|
+
this.syncedEntityIds.add(userId);
|
|
926
1074
|
}
|
|
927
1075
|
}
|
|
928
1076
|
} catch (error) {
|
|
@@ -936,6 +1084,59 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
936
1084
|
}
|
|
937
1085
|
return entities;
|
|
938
1086
|
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Extracts and builds the room object for a forum topic from a message context.
|
|
1089
|
+
* This refactored method can be used both in middleware and when handling new chats.
|
|
1090
|
+
*
|
|
1091
|
+
* @param {Context} ctx - The context of the incoming update
|
|
1092
|
+
* @param {UUID} worldId - The ID of the world the topic belongs to
|
|
1093
|
+
* @returns {Promise<Room | null>} A Promise that resolves with the room or null if not a topic
|
|
1094
|
+
* @private
|
|
1095
|
+
*/
|
|
1096
|
+
async buildForumTopicRoom(ctx, worldId) {
|
|
1097
|
+
var _a;
|
|
1098
|
+
if (!ctx.chat || !((_a = ctx.message) == null ? void 0 : _a.message_thread_id)) return null;
|
|
1099
|
+
if (ctx.chat.type !== "supergroup" || !ctx.chat.is_forum) return null;
|
|
1100
|
+
const chat = ctx.chat;
|
|
1101
|
+
const chatId = chat.id.toString();
|
|
1102
|
+
const threadId = ctx.message.message_thread_id.toString();
|
|
1103
|
+
const roomId = createUniqueUuid2(this.runtime, `${chatId}-${threadId}`);
|
|
1104
|
+
try {
|
|
1105
|
+
const replyMessage = JSON.parse(JSON.stringify(ctx.message));
|
|
1106
|
+
let topicName = `Topic #${threadId}`;
|
|
1107
|
+
if (replyMessage && typeof replyMessage === "object" && "forum_topic_created" in replyMessage && replyMessage.forum_topic_created) {
|
|
1108
|
+
const topicCreated = replyMessage.forum_topic_created;
|
|
1109
|
+
if (topicCreated && typeof topicCreated === "object" && "name" in topicCreated) {
|
|
1110
|
+
topicName = topicCreated.name;
|
|
1111
|
+
}
|
|
1112
|
+
} else if (replyMessage && typeof replyMessage === "object" && "reply_to_message" in replyMessage && replyMessage.reply_to_message && typeof replyMessage.reply_to_message === "object" && "forum_topic_created" in replyMessage.reply_to_message && replyMessage.reply_to_message.forum_topic_created) {
|
|
1113
|
+
const topicCreated = replyMessage.reply_to_message.forum_topic_created;
|
|
1114
|
+
if (topicCreated && typeof topicCreated === "object" && "name" in topicCreated) {
|
|
1115
|
+
topicName = topicCreated.name;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
const room = {
|
|
1119
|
+
id: roomId,
|
|
1120
|
+
name: topicName,
|
|
1121
|
+
source: "telegram",
|
|
1122
|
+
type: ChannelType2.GROUP,
|
|
1123
|
+
channelId: `${chatId}-${threadId}`,
|
|
1124
|
+
serverId: chatId,
|
|
1125
|
+
worldId,
|
|
1126
|
+
metadata: {
|
|
1127
|
+
threadId,
|
|
1128
|
+
isForumTopic: true,
|
|
1129
|
+
parentChatId: chatId
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
return room;
|
|
1133
|
+
} catch (error) {
|
|
1134
|
+
logger2.error(
|
|
1135
|
+
`Error building forum topic room: ${error instanceof Error ? error.message : String(error)}`
|
|
1136
|
+
);
|
|
1137
|
+
return null;
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
939
1140
|
};
|
|
940
1141
|
|
|
941
1142
|
// src/tests.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.ts","../src/service.ts","../src/environment.ts","../src/messageManager.ts","../src/utils.ts","../src/tests.ts","../src/index.ts"],"sourcesContent":["export const MESSAGE_CONSTANTS = {\n MAX_MESSAGES: 50,\n RECENT_MESSAGE_COUNT: 5,\n CHAT_HISTORY_COUNT: 10,\n DEFAULT_SIMILARITY_THRESHOLD: 0.6,\n DEFAULT_SIMILARITY_THRESHOLD_FOLLOW_UPS: 0.4,\n INTEREST_DECAY_TIME: 5 * 60 * 1000, // 5 minutes\n PARTIAL_INTEREST_DECAY: 3 * 60 * 1000, // 3 minutes\n} as const;\n\nexport const TELEGRAM_SERVICE_NAME = 'telegram';\n","import {\n ChannelType,\n type Entity,\n EventType,\n type IAgentRuntime,\n Role,\n type Room,\n Service,\n type UUID,\n type World,\n createUniqueUuid,\n logger,\n} from '@elizaos/core';\nimport { type Context, Telegraf } from 'telegraf';\nimport { TELEGRAM_SERVICE_NAME } from './constants';\nimport { validateTelegramConfig } from './environment';\nimport { MessageManager } from './messageManager';\nimport { TelegramEventTypes, TelegramWorldPayload } from './types';\n\n/**\n * Class representing a Telegram service that allows the agent to send and receive messages on Telegram.\n * @extends Service\n */\n\nexport class TelegramService extends Service {\n static serviceType = TELEGRAM_SERVICE_NAME;\n capabilityDescription = 'The agent is able to send and receive messages on telegram';\n private bot: Telegraf<Context>;\n public messageManager: MessageManager;\n private options;\n private knownChats: Map<string, any> = new Map();\n private syncedEntityIds: Set<string> = new Set<string>();\n\n /**\n * Constructor for TelegramService class.\n * @param {IAgentRuntime} runtime - The runtime object for the agent.\n */\n constructor(runtime: IAgentRuntime) {\n super(runtime);\n logger.log('📱 Constructing new TelegramService...');\n this.options = {\n telegram: {\n apiRoot:\n runtime.getSetting('TELEGRAM_API_ROOT') ||\n process.env.TELEGRAM_API_ROOT ||\n 'https://api.telegram.org',\n },\n };\n const botToken = runtime.getSetting('TELEGRAM_BOT_TOKEN');\n this.bot = new Telegraf(botToken, this.options);\n this.messageManager = new MessageManager(this.bot, this.runtime);\n logger.log('✅ TelegramService constructor completed');\n }\n\n /**\n * Starts the Telegram service for the given runtime.\n *\n * @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for.\n * @returns {Promise<TelegramService>} A promise that resolves with the initialized TelegramService.\n */\n static async start(runtime: IAgentRuntime): Promise<TelegramService> {\n await validateTelegramConfig(runtime);\n\n const maxRetries = 5;\n let retryCount = 0;\n let lastError: Error | null = null;\n\n while (retryCount < maxRetries) {\n try {\n const service = new TelegramService(runtime);\n\n logger.success(\n `✅ Telegram client successfully started for character ${runtime.character.name}`\n );\n\n logger.log('🚀 Starting Telegram bot...');\n await service.initializeBot();\n\n // Set up middlewares before message handlers to ensure proper preprocessing\n service.setupMiddlewares();\n\n // Set up message handlers after middlewares\n service.setupMessageHandlers();\n\n // Wait for bot to be ready by testing getMe()\n await service.bot.telegram.getMe();\n\n return service;\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n logger.error(\n `Telegram initialization attempt ${retryCount + 1} failed: ${lastError.message}`\n );\n retryCount++;\n\n if (retryCount < maxRetries) {\n const delay = 2 ** retryCount * 1000; // Exponential backoff\n logger.info(`Retrying Telegram initialization in ${delay / 1000} seconds...`);\n await new Promise((resolve) => setTimeout(resolve, delay));\n }\n }\n }\n\n throw new Error(\n `Telegram initialization failed after ${maxRetries} attempts. Last error: ${lastError?.message}`\n );\n }\n\n /**\n * Stops the agent runtime.\n * @param {IAgentRuntime} runtime - The agent runtime to stop\n */\n static async stop(runtime: IAgentRuntime) {\n // Implement shutdown if necessary\n const tgClient = runtime.getService(TELEGRAM_SERVICE_NAME);\n if (tgClient) {\n await tgClient.stop();\n }\n }\n\n /**\n * Asynchronously stops the bot.\n *\n * @returns A Promise that resolves once the bot has stopped.\n */\n async stop(): Promise<void> {\n this.bot.stop();\n }\n\n /**\n * Initializes the Telegram bot by launching it, getting bot info, and setting up message manager.\n * @returns {Promise<void>} A Promise that resolves when the initialization is complete.\n */\n private async initializeBot(): Promise<void> {\n this.bot.launch({\n dropPendingUpdates: true,\n allowedUpdates: ['message', 'message_reaction'],\n });\n\n // Get bot info for identification purposes\n const botInfo = await this.bot.telegram.getMe();\n logger.log(`Bot info: ${JSON.stringify(botInfo)}`);\n\n // Handle sigint and sigterm signals to gracefully stop the bot\n process.once('SIGINT', () => this.bot.stop('SIGINT'));\n process.once('SIGTERM', () => this.bot.stop('SIGTERM'));\n }\n\n /**\n * Sets up the middleware chain for preprocessing messages before they reach handlers.\n * This critical method establishes a sequential processing pipeline that:\n *\n * 1. Authorization - Verifies if a chat is allowed to interact with the bot based on configured settings\n * 2. Chat Discovery - Ensures chat entities and worlds exist in the runtime, creating them if needed\n * 3. Forum Topics - Handles Telegram forum topics as separate rooms for better conversation management\n * 4. Entity Synchronization - Ensures message senders are properly synchronized as entities\n *\n * The middleware chain runs in sequence for each message, with each step potentially\n * enriching the context or stopping processing if conditions aren't met.\n * This preprocessing is essential for maintaining consistent state before message handlers execute.\n * @private\n */\n private setupMiddlewares(): void {\n // Authorization middleware - checks if chat is allowed to interact with the bot\n this.bot.use(async (ctx, next) => {\n if (!(await this.isGroupAuthorized(ctx))) return;\n await next();\n });\n\n // Chat discovery middleware - ensures the chat and world exist.\n this.bot.use(async (ctx, next) => {\n if (!ctx.chat) return next();\n\n const chatId = ctx.chat.id.toString();\n\n // If we haven't seen this chat before, process it\n if (!this.knownChats.has(chatId)) {\n // Create a promise for this processing that other middleware calls can wait on\n await this.handleNewChat(ctx);\n }\n\n await next();\n });\n\n // Forum topic middleware - handle forum topics as separate rooms\n this.bot.use(async (ctx, next) => {\n if (!ctx.chat || !ctx.message?.message_thread_id || ctx.chat.type === 'private')\n return next();\n\n const chat = ctx.chat;\n if (chat.type === 'supergroup' && chat.is_forum) {\n try {\n await this.handleForumTopic(ctx);\n } catch (error) {\n logger.error(`Error handling forum topic: ${error}`);\n }\n }\n\n await next();\n });\n\n // Entity synchronization middleware - ensures the message sender is synced\n this.bot.use(async (ctx, next) => {\n if (!ctx.chat || !ctx.from || ctx.chat.type === 'private') return next();\n await this.syncEntity(ctx);\n await next();\n });\n }\n\n /**\n * Sets up message and reaction handlers for the bot.\n *\n * @private\n * @returns {void}\n */\n private setupMessageHandlers(): void {\n // Regular message handler\n this.bot.on('message', async (ctx) => {\n try {\n // Message handling is now simplified since all preprocessing is done by middleware\n await this.messageManager.handleMessage(ctx);\n } catch (error) {\n logger.error('Error handling message:', error);\n }\n });\n\n // Reaction handler\n this.bot.on('message_reaction', async (ctx) => {\n try {\n await this.messageManager.handleReaction(ctx);\n } catch (error) {\n logger.error('Error handling reaction:', error);\n }\n });\n }\n\n /**\n * Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting.\n * @param {Context} ctx - The context of the incoming update.\n * @returns {Promise<boolean>} A Promise that resolves with a boolean indicating if the group is authorized.\n */\n private async isGroupAuthorized(ctx: Context): Promise<boolean> {\n const chatId = ctx.chat?.id.toString();\n if (!chatId) return false;\n\n const allowedChats = this.runtime.getSetting('TELEGRAM_ALLOWED_CHATS');\n if (!allowedChats) {\n return true;\n }\n\n try {\n const allowedChatsList = JSON.parse(allowedChats as string);\n return allowedChatsList.includes(chatId);\n } catch (error) {\n logger.error('Error parsing TELEGRAM_ALLOWED_CHATS:', error);\n return false;\n }\n }\n\n /**\n * Synchronizes an entity from a message context\n * @param {Context} ctx - The context of the incoming update\n * @returns {Promise<void>}\n */\n private async syncEntity(ctx: Context): Promise<void> {\n if (!ctx.chat) return;\n\n // Handle message sender\n if (ctx.from) {\n const telegramId = ctx.from.id.toString();\n if (!this.syncedEntityIds.has(telegramId)) {\n const entityId = createUniqueUuid(this.runtime, telegramId) as UUID;\n const existingEntity = await this.runtime.getEntityById(entityId);\n\n if (!existingEntity) {\n await this.runtime.createEntity({\n id: entityId,\n agentId: this.runtime.agentId,\n names: [ctx.from.first_name || ctx.from.username || 'Unknown User'],\n metadata: {\n telegram: ctx.from,\n username: ctx.from.username,\n first_name: ctx.from.first_name,\n status: 'ACTIVE',\n joinedAt: Date.now(),\n },\n });\n }\n\n this.syncedEntityIds.add(telegramId);\n }\n }\n\n // Handle new chat member\n if (ctx.message && 'new_chat_member' in ctx.message) {\n const newMember = ctx.message.new_chat_member as any;\n const telegramId = newMember.id.toString();\n const entityId = createUniqueUuid(this.runtime, telegramId) as UUID;\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n const worldId = createUniqueUuid(this.runtime, chatId) as UUID;\n\n await this.runtime.createEntity({\n id: entityId,\n agentId: this.runtime.agentId,\n names: [newMember.first_name || newMember.username || 'Unknown User'],\n metadata: {\n telegram: newMember,\n username: newMember.username,\n first_name: newMember.first_name,\n status: 'ACTIVE',\n joinedAt: Date.now(),\n },\n });\n\n this.syncedEntityIds.add(telegramId);\n this.runtime.emitEvent([TelegramEventTypes.ENTITY_JOINED], {\n runtime: this.runtime,\n entityId,\n worldId,\n newMember,\n ctx,\n });\n }\n\n // Handle left chat member\n if (ctx.message && 'left_chat_member' in ctx.message) {\n const leftMember = ctx.message.left_chat_member as any;\n const telegramId = leftMember.id.toString();\n const entityId = createUniqueUuid(this.runtime, telegramId) as UUID;\n\n const existingEntity = await this.runtime.getEntityById(entityId);\n if (existingEntity) {\n existingEntity.metadata = {\n ...existingEntity.metadata,\n status: 'INACTIVE',\n leftAt: Date.now(),\n };\n await this.runtime.updateEntity(existingEntity);\n }\n }\n }\n\n /**\n * Handles forum topics by creating appropriate rooms\n * @param {Context} ctx - The context of the incoming update\n */\n private async handleForumTopic(ctx: Context): Promise<void> {\n if (!ctx.chat || !ctx.message?.message_thread_id) return;\n\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n const threadId = ctx.message.message_thread_id.toString();\n const worldId = createUniqueUuid(this.runtime, chatId) as UUID;\n const roomId = createUniqueUuid(this.runtime, `${chatId}-${threadId}`) as UUID;\n\n // Check if room already exists\n const existingRoom = await this.runtime.getRoom(roomId);\n if (existingRoom) return;\n\n try {\n // Get topic information\n let topicName = `Topic #${threadId}`;\n\n // Try to extract topic name from the message if it's a forum topic creation\n if (ctx.message && 'reply_to_message' in ctx.message) {\n const replyMessage = ctx.message.reply_to_message as any;\n if (replyMessage && 'forum_topic_created' in replyMessage) {\n const topicCreated = replyMessage.forum_topic_created as { name: string };\n if (topicCreated && topicCreated.name) {\n topicName = topicCreated.name;\n }\n }\n }\n\n // Create a room for this topic\n const room: Room = {\n id: roomId,\n name: topicName,\n source: 'telegram',\n type: ChannelType.GROUP,\n channelId: `${chatId}-${threadId}`,\n serverId: chatId,\n worldId,\n metadata: {\n threadId: threadId,\n isForumTopic: true,\n parentChatId: chatId,\n },\n };\n\n await this.runtime.ensureRoomExists(room);\n logger.debug(`Created room for forum topic: ${topicName} (${roomId})`);\n } catch (error) {\n logger.error(\n `Error handling forum topic: ${error instanceof Error ? error.message : String(error)}`\n );\n throw error;\n }\n }\n\n /**\n * Handles new chat discovery and emits WORLD_JOINED event\n * @param {Context} ctx - The context of the incoming update\n */\n private async handleNewChat(ctx: Context): Promise<void> {\n if (!ctx.chat) return;\n\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n\n // Mark this chat as known\n this.knownChats.set(chatId, chat);\n\n // Get chat title and channel type\n const { chatTitle, channelType } = this.getChatTypeInfo(chat);\n\n const worldId = createUniqueUuid(this.runtime, chatId) as UUID;\n\n const existingWorld = this.runtime.getWorld(worldId);\n if (existingWorld) {\n return;\n }\n\n const userId = ctx.from\n ? (createUniqueUuid(this.runtime, ctx.from.id.toString()) as UUID)\n : null;\n\n let admins = [];\n let owner = null;\n if (chat.type === 'group' || chat.type === 'supergroup' || chat.type === 'channel') {\n try {\n admins = await ctx.getChatAdministrators();\n owner = admins.find((admin) => admin.status === 'creator');\n } catch (error) {\n logger.warn(`Could not get chat administrators: ${error.message}`);\n }\n }\n\n let ownerId = userId;\n\n if (owner) {\n ownerId = createUniqueUuid(this.runtime, String(owner.user.id)) as UUID;\n }\n\n // Build world representation\n const world: World = {\n id: worldId,\n name: chatTitle,\n agentId: this.runtime.agentId,\n serverId: chatId,\n metadata: {\n source: 'telegram',\n ownership: { ownerId },\n roles: ownerId\n ? {\n [ownerId]: Role.OWNER,\n }\n : {},\n chatType: chat.type,\n isForumEnabled: chat.type === 'supergroup' && chat.is_forum,\n },\n };\n\n const entities = await this.buildStandardizedEntities(chat);\n\n const room = {\n id: createUniqueUuid(this.runtime, chatId) as UUID,\n name: chatTitle,\n source: 'telegram',\n type: channelType,\n channelId: chatId,\n serverId: chatId,\n worldId,\n };\n\n // Create payload for world events\n const worldPayload = {\n runtime: this.runtime,\n world,\n rooms: [room],\n entities,\n source: 'telegram',\n onComplete: () => {\n const telegramWorldPayload: TelegramWorldPayload = {\n ...worldPayload,\n chat,\n botUsername: this.bot.botInfo.username,\n };\n\n if (chat.type === 'group' || chat.type === 'supergroup' || chat.type === 'channel') {\n this.runtime.emitEvent(TelegramEventTypes.WORLD_JOINED, telegramWorldPayload);\n }\n },\n };\n\n // Emit generic WORLD_JOINED event\n await this.runtime.emitEvent(EventType.WORLD_JOINED, worldPayload);\n }\n\n /**\n * Gets chat title and channel type based on Telegram chat type\n * @param {any} chat - The Telegram chat object\n * @returns {Object} Object containing chatTitle and channelType\n */\n private getChatTypeInfo(chat: any): { chatTitle: string; channelType: ChannelType } {\n let chatTitle: string;\n let channelType: ChannelType;\n\n switch (chat.type) {\n case 'private':\n chatTitle = `Chat with ${chat.first_name || 'Unknown User'}`;\n channelType = ChannelType.DM;\n break;\n case 'group':\n chatTitle = chat.title || 'Unknown Group';\n channelType = ChannelType.GROUP;\n break;\n case 'supergroup':\n chatTitle = chat.title || 'Unknown Supergroup';\n channelType = ChannelType.GROUP;\n break;\n case 'channel':\n chatTitle = chat.title || 'Unknown Channel';\n channelType = ChannelType.FEED;\n break;\n default:\n chatTitle = 'Unknown Chat';\n channelType = ChannelType.GROUP;\n }\n\n return { chatTitle, channelType };\n }\n\n /**\n * Builds standardized entity representations from Telegram chat data\n * @param chat - The Telegram chat object\n * @returns Array of standardized Entity objects\n */\n private async buildStandardizedEntities(chat: any): Promise<Entity[]> {\n const entities: Entity[] = [];\n\n try {\n // For private chats, add the user\n if (chat.type === 'private' && chat.id) {\n const userId = createUniqueUuid(this.runtime, chat.id.toString()) as UUID;\n entities.push({\n id: userId,\n names: [chat.first_name || 'Unknown User'],\n agentId: this.runtime.agentId,\n metadata: {\n telegram: {\n id: chat.id.toString(),\n username: chat.username || 'unknown',\n name: chat.first_name || 'Unknown User',\n },\n source: 'telegram',\n },\n });\n } else if (chat.type === 'group' || chat.type === 'supergroup') {\n // For groups and supergroups, try to get member information\n try {\n // Get chat administrators (this is what's available through the Bot API)\n const admins = await this.bot.telegram.getChatAdministrators(chat.id);\n\n if (admins && admins.length > 0) {\n for (const admin of admins) {\n const userId = createUniqueUuid(this.runtime, admin.user.id.toString()) as UUID;\n entities.push({\n id: userId,\n names: [admin.user.first_name || admin.user.username || 'Unknown Admin'],\n agentId: this.runtime.agentId,\n metadata: {\n telegram: {\n id: admin.user.id.toString(),\n username: admin.user.username || 'unknown',\n name: admin.user.first_name || 'Unknown Admin',\n isAdmin: true,\n adminTitle:\n admin.custom_title || (admin.status === 'creator' ? 'Owner' : 'Admin'),\n },\n source: 'telegram',\n roles: [admin.status === 'creator' ? Role.OWNER : Role.ADMIN],\n },\n });\n }\n }\n } catch (error) {\n logger.warn(`Could not fetch administrators for chat ${chat.id}: ${error}`);\n }\n }\n } catch (error) {\n logger.error(\n `Error building standardized entities: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n return entities;\n }\n}\n","import type { IAgentRuntime } from '@elizaos/core';\nimport { z } from 'zod';\n\nexport const telegramEnvSchema = z.object({\n TELEGRAM_BOT_TOKEN: z.string().min(1, 'Telegram bot token is required'),\n});\n\n/**\n * Represents the type definition for configuring a Telegram bot based on the inferred schema.\n */\nexport type TelegramConfig = z.infer<typeof telegramEnvSchema>;\n\n/**\n * Validates the Telegram configuration by retrieving the Telegram bot token from the runtime settings or environment variables.\n *\n * @param {IAgentRuntime} runtime - The agent runtime used to get the setting.\n * @returns {Promise<TelegramConfig>} A promise that resolves with the validated Telegram configuration.\n */\nexport async function validateTelegramConfig(runtime: IAgentRuntime): Promise<TelegramConfig> {\n try {\n const config = {\n TELEGRAM_BOT_TOKEN:\n runtime.getSetting('TELEGRAM_BOT_TOKEN') || process.env.TELEGRAM_BOT_TOKEN,\n };\n\n return telegramEnvSchema.parse(config);\n } catch (error) {\n if (error instanceof z.ZodError) {\n const errorMessages = error.errors\n .map((err) => `${err.path.join('.')}: ${err.message}`)\n .join('\\n');\n throw new Error(`Telegram configuration validation failed:\\n${errorMessages}`);\n }\n throw error;\n }\n}\n","import {\n ChannelType,\n type Content,\n EventType,\n type HandlerCallback,\n type IAgentRuntime,\n type Media,\n type Memory,\n ModelType,\n type UUID,\n createUniqueUuid,\n logger,\n} from '@elizaos/core';\nimport type { Chat, Message, ReactionType, Update } from '@telegraf/types';\nimport type { Context, NarrowedContext, Telegraf } from 'telegraf';\nimport {\n TelegramEventTypes,\n type TelegramMessageReceivedPayload,\n type TelegramMessageSentPayload,\n type TelegramReactionReceivedPayload,\n} from './types';\nimport { escapeMarkdown } from './utils';\n\nimport fs from 'node:fs';\n\n/**\n * Enum representing different types of media.\n * @enum { string }\n * @readonly\n */\nexport enum MediaType {\n PHOTO = 'photo',\n VIDEO = 'video',\n DOCUMENT = 'document',\n AUDIO = 'audio',\n ANIMATION = 'animation',\n}\n\nconst MAX_MESSAGE_LENGTH = 4096; // Telegram's max message length\n\nconst getChannelType = (chat: Chat): ChannelType => {\n if (chat.type === 'private') return ChannelType.DM;\n if (chat.type === 'supergroup') return ChannelType.GROUP;\n if (chat.type === 'channel') return ChannelType.GROUP;\n if (chat.type === 'group') return ChannelType.GROUP;\n};\n\n/**\n * Class representing a message manager.\n * @class\n */\nexport class MessageManager {\n public bot: Telegraf<Context>;\n protected runtime: IAgentRuntime;\n\n /**\n * Constructor for creating a new instance of a BotAgent.\n *\n * @param {Telegraf<Context>} bot - The Telegraf instance used for interacting with the bot platform.\n * @param {IAgentRuntime} runtime - The runtime environment for the agent.\n */\n constructor(bot: Telegraf<Context>, runtime: IAgentRuntime) {\n this.bot = bot;\n this.runtime = runtime;\n }\n\n // Process image messages and generate descriptions\n /**\n * Process an image from a Telegram message to extract the image URL and description.\n *\n * @param {Message} message - The Telegram message object containing the image.\n * @returns {Promise<{ description: string } | null>} The description of the processed image or null if no image found.\n */\n async processImage(message: Message): Promise<{ description: string } | null> {\n try {\n let imageUrl: string | null = null;\n\n logger.info(`Telegram Message: ${message}`);\n\n if ('photo' in message && message.photo?.length > 0) {\n const photo = message.photo[message.photo.length - 1];\n const fileLink = await this.bot.telegram.getFileLink(photo.file_id);\n imageUrl = fileLink.toString();\n } else if ('document' in message && message.document?.mime_type?.startsWith('image/')) {\n const fileLink = await this.bot.telegram.getFileLink(message.document.file_id);\n imageUrl = fileLink.toString();\n }\n\n if (imageUrl) {\n const { title, description } = await this.runtime.useModel(\n ModelType.IMAGE_DESCRIPTION,\n imageUrl\n );\n return { description: `[Image: ${title}\\n${description}]` };\n }\n } catch (error) {\n console.error('❌ Error processing image:', error);\n }\n\n return null;\n }\n\n // Send long messages in chunks\n /**\n * Sends a message in chunks, handling attachments and splitting the message if necessary\n *\n * @param {Context} ctx - The context object representing the current state of the bot\n * @param {Content} content - The content of the message to be sent\n * @param {number} [replyToMessageId] - The ID of the message to reply to, if any\n * @returns {Promise<Message.TextMessage[]>} - An array of TextMessage objects representing the messages sent\n */\n async sendMessageInChunks(\n ctx: Context,\n content: Content,\n replyToMessageId?: number\n ): Promise<Message.TextMessage[]> {\n if (content.attachments && content.attachments.length > 0) {\n content.attachments.map(async (attachment: Media) => {\n const typeMap: { [key: string]: MediaType } = {\n 'image/gif': MediaType.ANIMATION,\n image: MediaType.PHOTO,\n doc: MediaType.DOCUMENT,\n video: MediaType.VIDEO,\n audio: MediaType.AUDIO,\n };\n\n let mediaType: MediaType | undefined = undefined;\n\n for (const prefix in typeMap) {\n if (attachment.contentType.startsWith(prefix)) {\n mediaType = typeMap[prefix];\n break;\n }\n }\n\n if (!mediaType) {\n throw new Error(\n `Unsupported Telegram attachment content type: ${attachment.contentType}`\n );\n }\n\n await this.sendMedia(ctx, attachment.url, mediaType, attachment.description);\n });\n } else {\n const chunks = this.splitMessage(content.text);\n const sentMessages: Message.TextMessage[] = [];\n\n for (let i = 0; i < chunks.length; i++) {\n const chunk = escapeMarkdown(chunks[i]);\n const sentMessage = (await ctx.telegram.sendMessage(ctx.chat.id, chunk, {\n reply_parameters:\n i === 0 && replyToMessageId ? { message_id: replyToMessageId } : undefined,\n parse_mode: 'Markdown',\n })) as Message.TextMessage;\n\n sentMessages.push(sentMessage);\n }\n\n return sentMessages;\n }\n }\n\n /**\n * Sends media to a chat using the Telegram API.\n *\n * @param {Context} ctx - The context object containing information about the current chat.\n * @param {string} mediaPath - The path to the media to be sent, either a URL or a local file path.\n * @param {MediaType} type - The type of media being sent (PHOTO, VIDEO, DOCUMENT, AUDIO, or ANIMATION).\n * @param {string} [caption] - Optional caption for the media being sent.\n *\n * @returns {Promise<void>} A Promise that resolves when the media is successfully sent.\n */\n async sendMedia(\n ctx: Context,\n mediaPath: string,\n type: MediaType,\n caption?: string\n ): Promise<void> {\n try {\n const isUrl = /^(http|https):\\/\\//.test(mediaPath);\n const sendFunctionMap: Record<MediaType, Function> = {\n [MediaType.PHOTO]: ctx.telegram.sendPhoto.bind(ctx.telegram),\n [MediaType.VIDEO]: ctx.telegram.sendVideo.bind(ctx.telegram),\n [MediaType.DOCUMENT]: ctx.telegram.sendDocument.bind(ctx.telegram),\n [MediaType.AUDIO]: ctx.telegram.sendAudio.bind(ctx.telegram),\n [MediaType.ANIMATION]: ctx.telegram.sendAnimation.bind(ctx.telegram),\n };\n\n const sendFunction = sendFunctionMap[type];\n\n if (!sendFunction) {\n throw new Error(`Unsupported media type: ${type}`);\n }\n\n if (isUrl) {\n // Handle HTTP URLs\n await sendFunction(ctx.chat.id, mediaPath, { caption });\n } else {\n // Handle local file paths\n if (!fs.existsSync(mediaPath)) {\n throw new Error(`File not found at path: ${mediaPath}`);\n }\n\n const fileStream = fs.createReadStream(mediaPath);\n\n try {\n await sendFunction(ctx.chat.id, { source: fileStream }, { caption });\n } finally {\n fileStream.destroy();\n }\n }\n\n logger.info(\n `${type.charAt(0).toUpperCase() + type.slice(1)} sent successfully: ${mediaPath}`\n );\n } catch (error) {\n logger.error(`Failed to send ${type}. Path: ${mediaPath}. Error: ${error.message}`);\n logger.debug(error.stack);\n throw error;\n }\n }\n\n // Split message into smaller parts\n /**\n * Splits a given text into an array of strings based on the maximum message length.\n *\n * @param {string} text - The text to split into chunks.\n * @returns {string[]} An array of strings with each element representing a chunk of the original text.\n */\n private splitMessage(text: string): string[] {\n const chunks: string[] = [];\n let currentChunk = '';\n\n const lines = text.split('\\n');\n for (const line of lines) {\n if (currentChunk.length + line.length + 1 <= MAX_MESSAGE_LENGTH) {\n currentChunk += (currentChunk ? '\\n' : '') + line;\n } else {\n if (currentChunk) chunks.push(currentChunk);\n currentChunk = line;\n }\n }\n\n if (currentChunk) chunks.push(currentChunk);\n return chunks;\n }\n\n // Main handler for incoming messages\n /**\n * Handle incoming messages from Telegram and process them accordingly.\n * @param {Context} ctx - The context object containing information about the message.\n * @returns {Promise<void>}\n */\n public async handleMessage(ctx: Context): Promise<void> {\n // Type guard to ensure message exists\n if (!ctx.message || !ctx.from) return;\n\n const message = ctx.message as Message.TextMessage;\n\n try {\n // Convert IDs to UUIDs\n const entityId = createUniqueUuid(this.runtime, ctx.from.id.toString()) as UUID;\n\n const threadId =\n 'is_topic_message' in message && message.is_topic_message\n ? message.message_thread_id?.toString()\n : undefined;\n\n // Generate room ID based on whether this is in a forum topic\n const roomId = createUniqueUuid(\n this.runtime,\n threadId ? `${ctx.chat.id}-${threadId}` : ctx.chat.id.toString()\n ) as UUID;\n\n // Get message ID\n const messageId = createUniqueUuid(this.runtime, message?.message_id?.toString());\n\n // Handle images\n const imageInfo = await this.processImage(message);\n\n // Get message text - use type guards for safety\n let messageText = '';\n if ('text' in message && message.text) {\n messageText = message.text;\n } else if ('caption' in message && message.caption) {\n messageText = message.caption as string;\n }\n\n // Combine text and image description\n const fullText = imageInfo ? `${messageText} ${imageInfo.description}` : messageText;\n if (!fullText) return;\n\n // Get chat type and determine channel type\n const chat = message.chat as Chat;\n const channelType = getChannelType(chat);\n\n // Create the memory object\n const memory: Memory = {\n id: messageId,\n entityId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n text: fullText,\n source: 'telegram',\n channelType: channelType,\n inReplyTo:\n 'reply_to_message' in message && message.reply_to_message\n ? createUniqueUuid(this.runtime, message.reply_to_message.message_id.toString())\n : undefined,\n },\n createdAt: message.date * 1000,\n };\n\n // Create callback for handling responses\n const callback: HandlerCallback = async (content: Content, _files?: string[]) => {\n try {\n // If response is from reasoning do not send it.\n if (!content.text) return [];\n\n const sentMessages = await this.sendMessageInChunks(ctx, content, message.message_id);\n\n if (!sentMessages) return [];\n\n const memories: Memory[] = [];\n for (let i = 0; i < sentMessages.length; i++) {\n const sentMessage = sentMessages[i];\n const _isLastMessage = i === sentMessages.length - 1;\n\n const responseMemory: Memory = {\n id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),\n entityId: this.runtime.agentId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n ...content,\n text: sentMessage.text,\n inReplyTo: messageId,\n channelType: channelType,\n },\n createdAt: sentMessage.date * 1000,\n };\n\n await this.runtime.createMemory(responseMemory, 'messages');\n memories.push(responseMemory);\n }\n\n return memories;\n } catch (error) {\n logger.error('Error in message callback:', error);\n return [];\n }\n };\n\n // Let the bootstrap plugin handle the message\n this.runtime.emitEvent(EventType.MESSAGE_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n });\n\n // Also emit the platform-specific event\n this.runtime.emitEvent(TelegramEventTypes.MESSAGE_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n ctx,\n originalMessage: message,\n } as TelegramMessageReceivedPayload);\n } catch (error) {\n logger.error('Error handling Telegram message:', {\n error,\n chatId: ctx.chat?.id,\n messageId: ctx.message?.message_id,\n from: ctx.from?.username || ctx.from?.id,\n });\n throw error;\n }\n }\n\n /**\n * Handles the reaction event triggered by a user reacting to a message.\n * @param {NarrowedContext<Context<Update>, Update.MessageReactionUpdate>} ctx The context of the message reaction update\n * @returns {Promise<void>} A Promise that resolves when the reaction handling is complete\n */\n public async handleReaction(\n ctx: NarrowedContext<Context<Update>, Update.MessageReactionUpdate>\n ): Promise<void> {\n // Ensure we have the necessary data\n if (!ctx.update.message_reaction || !ctx.from) return;\n\n const reaction = ctx.update.message_reaction;\n const reactionType = reaction.new_reaction[0].type;\n const reactionEmoji = (reaction.new_reaction[0] as ReactionType).type;\n\n try {\n const entityId = createUniqueUuid(this.runtime, ctx.from.id.toString()) as UUID;\n const roomId = createUniqueUuid(this.runtime, ctx.chat.id.toString());\n\n const reactionId = createUniqueUuid(\n this.runtime,\n `${reaction.message_id}-${ctx.from.id}-${Date.now()}`\n );\n\n // Create reaction memory\n const memory: Memory = {\n id: reactionId,\n entityId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n channelType: getChannelType(reaction.chat as Chat),\n text: `Reacted with: ${reactionType === 'emoji' ? reactionEmoji : reactionType}`,\n source: 'telegram',\n inReplyTo: createUniqueUuid(this.runtime, reaction.message_id.toString()),\n },\n createdAt: Date.now(),\n };\n\n // Create callback for handling reaction responses\n const callback: HandlerCallback = async (content: Content) => {\n try {\n const sentMessage = await ctx.reply(content.text);\n const responseMemory: Memory = {\n id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),\n entityId: this.runtime.agentId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n ...content,\n inReplyTo: reactionId,\n },\n createdAt: sentMessage.date * 1000,\n };\n return [responseMemory];\n } catch (error) {\n logger.error('Error in reaction callback:', error);\n return [];\n }\n };\n\n // Let the bootstrap plugin handle the reaction\n this.runtime.emitEvent(EventType.REACTION_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n });\n\n // Also emit the platform-specific event\n this.runtime.emitEvent(TelegramEventTypes.REACTION_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n ctx,\n reactionString: reactionType === 'emoji' ? reactionEmoji : reactionType,\n originalReaction: reaction.new_reaction[0] as ReactionType,\n } as TelegramReactionReceivedPayload);\n } catch (error) {\n logger.error('Error handling reaction:', error);\n }\n }\n\n /**\n * Sends a message to a Telegram chat and emits appropriate events\n * @param {number | string} chatId - The Telegram chat ID to send the message to\n * @param {Content} content - The content to send\n * @param {number} [replyToMessageId] - Optional message ID to reply to\n * @returns {Promise<Message.TextMessage[]>} The sent messages\n */\n public async sendMessage(\n chatId: number | string,\n content: Content,\n replyToMessageId?: number\n ): Promise<Message.TextMessage[]> {\n try {\n // Create a context-like object for sending\n const ctx = {\n chat: { id: chatId },\n telegram: this.bot.telegram,\n };\n\n const sentMessages = await this.sendMessageInChunks(\n ctx as Context,\n content,\n replyToMessageId\n );\n\n if (!sentMessages?.length) return [];\n\n // Create room ID\n const roomId = createUniqueUuid(this.runtime, chatId.toString());\n\n // Create memories for the sent messages\n const memories: Memory[] = [];\n for (const sentMessage of sentMessages) {\n const memory: Memory = {\n id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),\n entityId: this.runtime.agentId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n ...content,\n text: sentMessage.text,\n source: 'telegram',\n channelType: getChannelType({\n id: typeof chatId === 'string' ? Number.parseInt(chatId, 10) : chatId,\n type: 'private', // Default to private, will be overridden if in context\n } as Chat),\n },\n createdAt: sentMessage.date * 1000,\n };\n\n await this.runtime.createMemory(memory, 'messages');\n memories.push(memory);\n }\n\n // Emit both generic and platform-specific message sent events\n this.runtime.emitEvent(EventType.MESSAGE_SENT, {\n runtime: this.runtime,\n message: {\n content: content,\n },\n roomId,\n source: 'telegram',\n });\n\n // Also emit platform-specific event\n this.runtime.emitEvent(TelegramEventTypes.MESSAGE_SENT, {\n originalMessages: sentMessages,\n chatId,\n } as TelegramMessageSentPayload);\n\n return sentMessages;\n } catch (error) {\n logger.error('Error sending message to Telegram:', error);\n return [];\n }\n }\n}\n","/**\n * Escapes Markdown special characters in the given text, excluding code blocks.\n * @param {string} text - The text to escape Markdown characters from.\n * @returns {string} The text with escaped Markdown characters.\n */\nexport function escapeMarkdown(text: string): string {\n // Don't escape if it's a code block\n if (text.startsWith('```') && text.endsWith('```')) {\n return text;\n }\n\n // Split the text by code blocks\n const parts = text.split(/(```[\\s\\S]*?```)/g);\n\n return parts\n .map((part, index) => {\n // If it's a code block (odd indices in the split result will be code blocks)\n if (index % 2 === 1) {\n return part;\n }\n // For regular text, only escape characters that need escaping in Markdown\n return (\n part\n // First preserve any intended inline code spans\n .replace(/`.*?`/g, (match) => match)\n // Then only escape the minimal set of special characters that need escaping in Markdown mode\n .replace(/([*_`\\\\])/g, '\\\\$1')\n );\n })\n .join('');\n}\n\n/**\n * Splits a message into chunks that fit within Telegram's message length limit\n */\n/**\n * Splits a text message into chunks based on a maximum length for each chunk.\n *\n * @param {string} text - The text message to split.\n * @param {number} maxLength - The maximum length for each chunk (default is 4096).\n * @returns {string[]} An array containing the text message split into chunks.\n */\nexport function splitMessage(text: string, maxLength = 4096): string[] {\n const chunks: string[] = [];\n let currentChunk = '';\n\n const lines = text.split('\\n');\n for (const line of lines) {\n if (currentChunk.length + line.length + 1 <= maxLength) {\n currentChunk += (currentChunk ? '\\n' : '') + line;\n } else {\n if (currentChunk) chunks.push(currentChunk);\n currentChunk = line;\n }\n }\n\n if (currentChunk) chunks.push(currentChunk);\n return chunks;\n}\n","import { type IAgentRuntime, type TestSuite, logger } from '@elizaos/core';\nimport type { Chat, User } from '@telegraf/types';\nimport type { Telegraf } from 'telegraf';\nimport type { Context } from 'telegraf';\nimport type { MessageManager } from './messageManager';\nimport type { TelegramService } from './service';\n\nconst TEST_IMAGE_URL =\n 'https://github.com/elizaOS/awesome-eliza/blob/main/assets/eliza-logo.jpg?raw=true';\n\n/**\n * Represents a test suite for testing Telegram functionality.\n *\n * This test suite includes methods to initialize and validate a Telegram bot connection,\n * send basic text messages to a Telegram chat, send text messages with image attachments,\n * handle and process incoming Telegram messages, and process and validate image attachments\n * in incoming messages.\n *\n * @implements {TestSuite}\n */\n\nexport class TelegramTestSuite implements TestSuite {\n name = 'telegram';\n private telegramClient: TelegramService = null;\n private bot: Telegraf<Context> | null = null;\n private messageManager: MessageManager | null = null;\n tests: { name: string; fn: (runtime: IAgentRuntime) => Promise<void> }[];\n\n /**\n * Constructor for initializing a set of test cases for a Telegram bot.\n *\n * @constructor\n * @property {Array<Object>} tests - An array of test cases with name and corresponding test functions.\n * @property {string} tests.name - The name of the test case.\n * @property {function} tests.fn - The test function to be executed.\n */\n constructor() {\n this.tests = [\n {\n name: 'Initialize and Validate Telegram Bot Connection',\n fn: this.testCreatingTelegramBot.bind(this),\n },\n {\n name: 'Send Basic Text Message to Telegram Chat',\n fn: this.testSendingTextMessage.bind(this),\n },\n {\n name: 'Send Text Message with an Image Attachment',\n fn: this.testSendingMessageWithAttachment.bind(this),\n },\n {\n name: 'Handle and Process Incoming Telegram Messages',\n fn: this.testHandlingMessage.bind(this),\n },\n {\n name: 'Process and Validate Image Attachments in Incoming Messages',\n fn: this.testProcessingImages.bind(this),\n },\n ];\n }\n\n /**\n * Retrieves the Telegram test chat ID from environment variables.\n *\n * Reference on getting the Telegram chat ID:\n * https://stackoverflow.com/a/32572159\n */\n /**\n * Validates the chat ID by checking if it is set in the runtime settings or environment variables.\n * If not set, an error is thrown with a message instructing to provide a valid chat ID.\n * @param {IAgentRuntime} runtime - The runtime object that provides access to the settings and environment variables.\n * @throws {Error} If TELEGRAM_TEST_CHAT_ID is not set in the runtime settings or environment variables.\n * @returns {string} The validated chat ID.\n */\n validateChatId(runtime: IAgentRuntime) {\n const testChatId =\n runtime.getSetting('TELEGRAM_TEST_CHAT_ID') || process.env.TELEGRAM_TEST_CHAT_ID;\n if (!testChatId) {\n throw new Error(\n 'TELEGRAM_TEST_CHAT_ID is not set. Please provide a valid chat ID in the environment variables.'\n );\n }\n return testChatId;\n }\n\n async getChatInfo(runtime: IAgentRuntime): Promise<Context['chat']> {\n try {\n const chatId = this.validateChatId(runtime);\n const chat = await this.bot.telegram.getChat(chatId);\n logger.log(`Fetched real chat: ${JSON.stringify(chat)}`);\n return chat;\n } catch (error) {\n throw new Error(`Error fetching real Telegram chat: ${error}`);\n }\n }\n\n async testCreatingTelegramBot(runtime: IAgentRuntime) {\n this.telegramClient = runtime.getService('telegram') as TelegramService;\n this.bot = this.telegramClient.messageManager.bot;\n this.messageManager = this.telegramClient.messageManager;\n logger.debug('Telegram bot initialized successfully.');\n }\n\n async testSendingTextMessage(runtime: IAgentRuntime) {\n try {\n if (!this.bot) throw new Error('Bot not initialized.');\n\n const chatId = this.validateChatId(runtime);\n await this.bot.telegram.sendMessage(chatId, 'Testing Telegram message!');\n logger.debug('Message sent successfully.');\n } catch (error) {\n throw new Error(`Error sending Telegram message: ${error}`);\n }\n }\n\n async testSendingMessageWithAttachment(runtime: IAgentRuntime) {\n try {\n if (!this.messageManager) throw new Error('MessageManager not initialized.');\n\n const chat = await this.getChatInfo(runtime);\n const mockContext: Partial<Context> = {\n chat,\n from: { id: 123, username: 'TestUser' } as User,\n telegram: this.bot.telegram,\n };\n\n const messageContent = {\n text: 'Here is an image attachment:',\n attachments: [\n {\n id: '123',\n title: 'Sample Image',\n source: TEST_IMAGE_URL,\n text: 'Sample Image',\n url: TEST_IMAGE_URL,\n contentType: 'image/png',\n description: 'Sample Image',\n },\n ],\n };\n\n await this.messageManager.sendMessageInChunks(mockContext as Context, messageContent);\n\n logger.success('Message with image attachment sent successfully.');\n } catch (error) {\n throw new Error(`Error sending Telegram message with attachment: ${error}`);\n }\n }\n\n async testHandlingMessage(runtime: IAgentRuntime) {\n try {\n const chat = await this.getChatInfo(runtime);\n const mockContext: Partial<Context> = {\n chat,\n from: {\n id: 123,\n username: 'TestUser',\n is_bot: false,\n first_name: 'Test',\n last_name: 'User',\n } as User,\n message: {\n message_id: undefined,\n text: `@${this.bot.botInfo?.username}! Hello!`,\n date: Math.floor(Date.now() / 1000),\n chat,\n } as any,\n telegram: this.bot.telegram,\n };\n\n try {\n await this.messageManager.handleMessage(mockContext as Context);\n } catch (error) {\n throw new Error(`Error handling Telegram message: ${error}`);\n }\n } catch (error) {\n throw new Error(`Error handling Telegram message: ${error}`);\n }\n }\n\n async testProcessingImages(runtime: IAgentRuntime) {\n try {\n const chatId = this.validateChatId(runtime);\n const fileId = await this.getFileId(chatId, TEST_IMAGE_URL);\n\n const mockMessage = {\n message_id: undefined,\n chat: { id: chatId } as Chat,\n date: Math.floor(Date.now() / 1000),\n photo: [{ file_id: fileId }],\n text: `@${this.bot.botInfo?.username}!`,\n };\n\n const { description } = await this.messageManager.processImage(mockMessage);\n if (!description) {\n throw new Error('Error processing Telegram image');\n }\n logger.log(`Processing Telegram image successfully: ${description}`);\n } catch (error) {\n throw new Error(`Error processing Telegram image: ${error}`);\n }\n }\n\n async getFileId(chatId: string, imageUrl: string) {\n try {\n const message = await this.bot.telegram.sendPhoto(chatId, imageUrl);\n return message.photo[message.photo.length - 1].file_id;\n } catch (error) {\n logger.error(`Error sending image: ${error}`);\n throw error;\n }\n }\n}\n","import type { Plugin } from '@elizaos/core';\nimport { TELEGRAM_SERVICE_NAME } from './constants';\nimport { TelegramService } from './service';\nimport { TelegramTestSuite } from './tests';\n\nconst telegramPlugin: Plugin = {\n name: TELEGRAM_SERVICE_NAME,\n description: 'Telegram client plugin',\n services: [TelegramService],\n tests: [new TelegramTestSuite()],\n};\nexport default telegramPlugin;\n"],"mappings":";AAAO,IAAM,oBAAoB;AAAA,EAC/B,cAAc;AAAA,EACd,sBAAsB;AAAA,EACtB,oBAAoB;AAAA,EACpB,8BAA8B;AAAA,EAC9B,yCAAyC;AAAA,EACzC,qBAAqB,IAAI,KAAK;AAAA;AAAA,EAC9B,wBAAwB,IAAI,KAAK;AAAA;AACnC;AAEO,IAAM,wBAAwB;;;ACVrC;AAAA,EACE,eAAAA;AAAA,EAEA,aAAAC;AAAA,EAEA;AAAA,EAEA;AAAA,EAGA,oBAAAC;AAAA,EACA,UAAAC;AAAA,OACK;AACP,SAAuB,gBAAgB;;;ACZvC,SAAS,SAAS;AAEX,IAAM,oBAAoB,EAAE,OAAO;AAAA,EACxC,oBAAoB,EAAE,OAAO,EAAE,IAAI,GAAG,gCAAgC;AACxE,CAAC;AAaD,eAAsB,uBAAuB,SAAiD;AAC5F,MAAI;AACF,UAAM,SAAS;AAAA,MACb,oBACE,QAAQ,WAAW,oBAAoB,KAAK,QAAQ,IAAI;AAAA,IAC5D;AAEA,WAAO,kBAAkB,MAAM,MAAM;AAAA,EACvC,SAAS,OAAO;AACd,QAAI,iBAAiB,EAAE,UAAU;AAC/B,YAAM,gBAAgB,MAAM,OACzB,IAAI,CAAC,QAAQ,GAAG,IAAI,KAAK,KAAK,GAAG,CAAC,KAAK,IAAI,OAAO,EAAE,EACpD,KAAK,IAAI;AACZ,YAAM,IAAI,MAAM;AAAA,EAA8C,aAAa,EAAE;AAAA,IAC/E;AACA,UAAM;AAAA,EACR;AACF;;;ACnCA;AAAA,EACE;AAAA,EAEA;AAAA,EAKA;AAAA,EAEA;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AiC;AAAA,MACnD;AACA,MAAAA,QAAO,IAAI,2CAA2C,WAAW,EAAE;AAAA,IACrE,SAAS,OAAO;AACd,YAAM,IAAI,MAAM,oCAAoC,KAAK,EAAE;AAAA,IAC7D;AAAA,EACF;AAAA,EAEA,MAAM,UAAU,QAAgB,UAAkB;AAChD,QAAI;AACF,YAAM,UAAU,MAAM,KAAK,IAAI,SAAS,UAAU,QAAQ,QAAQ;AAClE,aAAO,QAAQ,MAAM,QAAQ,MAAM,SAAS,CAAC,EAAE;AAAA,IACjD,SAAS,OAAO;AACd,MAAAA,QAAO,MAAM,wBAAwB,KAAK,EAAE;AAC5C,YAAM;AAAA,IACR;AAAA,EACF;AACF;;;AC/MA,IAAM,iBAAyB;AAAA,EAC7B,MAAM;AAAA,EACN,aAAa;AAAA,EACb,UAAU,CAAC,eAAe;AAAA,EAC1B,OAAO,CAAC,IAAI,kBAAkB,CAAC;AACjC;AACA,IAAO,gBAAQ;","names":["ChannelType","EventType","createUniqueUuid","logger","logger","createUniqueUuid","ChannelType","EventType","logger"]}
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts","../src/service.ts","../src/environment.ts","../src/messageManager.ts","../src/utils.ts","../src/tests.ts","../src/index.ts"],"sourcesContent":["export const MESSAGE_CONSTANTS = {\n MAX_MESSAGES: 50,\n RECENT_MESSAGE_COUNT: 5,\n CHAT_HISTORY_COUNT: 10,\n DEFAULT_SIMILARITY_THRESHOLD: 0.6,\n DEFAULT_SIMILARITY_THRESHOLD_FOLLOW_UPS: 0.4,\n INTEREST_DECAY_TIME: 5 * 60 * 1000, // 5 minutes\n PARTIAL_INTEREST_DECAY: 3 * 60 * 1000, // 3 minutes\n} as const;\n\nexport const TELEGRAM_SERVICE_NAME = 'telegram';\n","import {\n ChannelType,\n type Entity,\n EventType,\n type IAgentRuntime,\n Role,\n type Room,\n Service,\n type UUID,\n type World,\n WorldPayload,\n createUniqueUuid,\n logger,\n} from '@elizaos/core';\nimport { type Context, Telegraf } from 'telegraf';\nimport { TELEGRAM_SERVICE_NAME } from './constants';\nimport { validateTelegramConfig } from './environment';\nimport { MessageManager } from './messageManager';\nimport { TelegramEventTypes, TelegramWorldPayload } from './types';\n\n/**\n * Class representing a Telegram service that allows the agent to send and receive messages on Telegram.\n * This service handles all Telegram-specific functionality including:\n * - Initializing and managing the Telegram bot\n * - Setting up middleware for preprocessing messages\n * - Handling message and reaction events\n * - Synchronizing Telegram chats, users, and entities with the agent runtime\n * - Managing forum topics as separate rooms\n *\n * @extends Service\n */\nexport class TelegramService extends Service {\n static serviceType = TELEGRAM_SERVICE_NAME;\n capabilityDescription = 'The agent is able to send and receive messages on telegram';\n private bot: Telegraf<Context>;\n public messageManager: MessageManager;\n private options;\n private knownChats: Map<string, any> = new Map();\n private syncedEntityIds: Set<string> = new Set<string>();\n\n /**\n * Constructor for TelegramService class.\n * @param {IAgentRuntime} runtime - The runtime object for the agent.\n */\n constructor(runtime: IAgentRuntime) {\n super(runtime);\n logger.log('📱 Constructing new TelegramService...');\n this.options = {\n telegram: {\n apiRoot:\n runtime.getSetting('TELEGRAM_API_ROOT') ||\n process.env.TELEGRAM_API_ROOT ||\n 'https://api.telegram.org',\n },\n };\n const botToken = runtime.getSetting('TELEGRAM_BOT_TOKEN');\n this.bot = new Telegraf(botToken, this.options);\n this.messageManager = new MessageManager(this.bot, this.runtime);\n logger.log('✅ TelegramService constructor completed');\n }\n\n /**\n * Starts the Telegram service for the given runtime.\n *\n * @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for.\n * @returns {Promise<TelegramService>} A promise that resolves with the initialized TelegramService.\n */\n static async start(runtime: IAgentRuntime): Promise<TelegramService> {\n await validateTelegramConfig(runtime);\n\n const maxRetries = 5;\n let retryCount = 0;\n let lastError: Error | null = null;\n\n while (retryCount < maxRetries) {\n try {\n const service = new TelegramService(runtime);\n\n logger.success(\n `✅ Telegram client successfully started for character ${runtime.character.name}`\n );\n\n logger.log('🚀 Starting Telegram bot...');\n await service.initializeBot();\n\n // Set up middlewares before message handlers to ensure proper preprocessing\n service.setupMiddlewares();\n\n // Set up message handlers after middlewares\n service.setupMessageHandlers();\n\n // Wait for bot to be ready by testing getMe()\n await service.bot.telegram.getMe();\n\n return service;\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n logger.error(\n `Telegram initialization attempt ${retryCount + 1} failed: ${lastError.message}`\n );\n retryCount++;\n\n if (retryCount < maxRetries) {\n const delay = 2 ** retryCount * 1000; // Exponential backoff\n logger.info(`Retrying Telegram initialization in ${delay / 1000} seconds...`);\n await new Promise((resolve) => setTimeout(resolve, delay));\n }\n }\n }\n\n throw new Error(\n `Telegram initialization failed after ${maxRetries} attempts. Last error: ${lastError?.message}`\n );\n }\n\n /**\n * Stops the agent runtime.\n * @param {IAgentRuntime} runtime - The agent runtime to stop\n */\n static async stop(runtime: IAgentRuntime) {\n // Implement shutdown if necessary\n const tgClient = runtime.getService(TELEGRAM_SERVICE_NAME);\n if (tgClient) {\n await tgClient.stop();\n }\n }\n\n /**\n * Asynchronously stops the bot.\n *\n * @returns A Promise that resolves once the bot has stopped.\n */\n async stop(): Promise<void> {\n this.bot.stop();\n }\n\n /**\n * Initializes the Telegram bot by launching it, getting bot info, and setting up message manager.\n * @returns {Promise<void>} A Promise that resolves when the initialization is complete.\n */\n private async initializeBot(): Promise<void> {\n this.bot.launch({\n dropPendingUpdates: true,\n allowedUpdates: ['message', 'message_reaction'],\n });\n\n // Get bot info for identification purposes\n const botInfo = await this.bot.telegram.getMe();\n logger.log(`Bot info: ${JSON.stringify(botInfo)}`);\n\n // Handle sigint and sigterm signals to gracefully stop the bot\n process.once('SIGINT', () => this.bot.stop('SIGINT'));\n process.once('SIGTERM', () => this.bot.stop('SIGTERM'));\n }\n\n /**\n * Sets up the middleware chain for preprocessing messages before they reach handlers.\n * This critical method establishes a sequential processing pipeline that:\n *\n * 1. Authorization - Verifies if a chat is allowed to interact with the bot based on configured settings\n * 2. Chat Discovery - Ensures chat entities and worlds exist in the runtime, creating them if needed\n * 3. Forum Topics - Handles Telegram forum topics as separate rooms for better conversation management\n * 4. Entity Synchronization - Ensures message senders are properly synchronized as entities\n *\n * The middleware chain runs in sequence for each message, with each step potentially\n * enriching the context or stopping processing if conditions aren't met.\n * This preprocessing is essential for maintaining consistent state before message handlers execute.\n *\n * @private\n */\n private setupMiddlewares(): void {\n // Register the authorization middleware\n this.bot.use(this.authorizationMiddleware.bind(this));\n\n // Register the chat and entity management middleware\n this.bot.use(this.chatAndEntityMiddleware.bind(this));\n }\n\n /**\n * Authorization middleware - checks if chat is allowed to interact with the bot\n * based on the TELEGRAM_ALLOWED_CHATS configuration.\n *\n * @param {Context} ctx - The context of the incoming update\n * @param {Function} next - The function to call to proceed to the next middleware\n * @returns {Promise<void>}\n * @private\n */\n private async authorizationMiddleware(ctx: Context, next: Function): Promise<void> {\n if (!(await this.isGroupAuthorized(ctx))) {\n // Skip further processing if chat is not authorized\n logger.debug('Chat not authorized, skipping message processing');\n return;\n }\n await next();\n }\n\n /**\n * Chat and entity management middleware - handles new chats, forum topics, and entity synchronization.\n * This middleware implements decision logic to determine which operations are needed based on\n * the chat type and whether we've seen this chat before.\n *\n * @param {Context} ctx - The context of the incoming update\n * @param {Function} next - The function to call to proceed to the next middleware\n * @returns {Promise<void>}\n * @private\n */\n private async chatAndEntityMiddleware(ctx: Context, next: Function): Promise<void> {\n if (!ctx.chat) return next();\n\n const chatId = ctx.chat.id.toString();\n\n // If we haven't seen this chat before, process it as a new chat\n if (!this.knownChats.has(chatId)) {\n // Process the new chat - creates world, room, topic room (if applicable) and entities\n await this.handleNewChat(ctx);\n // Skip entity synchronization for new chats and proceed to the next middleware\n return next();\n }\n\n // For existing chats, determine the required operations based on chat type\n await this.processExistingChat(ctx);\n\n await next();\n }\n\n /**\n * Process an existing chat based on chat type and message properties.\n * Different chat types require different processing steps.\n *\n * @param {Context} ctx - The context of the incoming update\n * @returns {Promise<void>}\n * @private\n */\n private async processExistingChat(ctx: Context): Promise<void> {\n if (!ctx.chat) return;\n\n const chat = ctx.chat;\n\n // Handle forum topics for supergroups with forums\n if (chat.type === 'supergroup' && chat.is_forum && ctx.message?.message_thread_id) {\n try {\n await this.handleForumTopic(ctx);\n } catch (error) {\n logger.error(`Error handling forum topic: ${error}`);\n }\n }\n\n // For non-private chats, synchronize entity information\n if (ctx.from && ctx.chat.type !== 'private') {\n await this.syncEntity(ctx);\n }\n }\n\n /**\n * Sets up message and reaction handlers for the bot.\n * Configures event handlers to process incoming messages and reactions.\n *\n * @private\n */\n private setupMessageHandlers(): void {\n // Regular message handler\n this.bot.on('message', async (ctx) => {\n try {\n // Message handling is now simplified since all preprocessing is done by middleware\n await this.messageManager.handleMessage(ctx);\n } catch (error) {\n logger.error('Error handling message:', error);\n }\n });\n\n // Reaction handler\n this.bot.on('message_reaction', async (ctx) => {\n try {\n await this.messageManager.handleReaction(ctx);\n } catch (error) {\n logger.error('Error handling reaction:', error);\n }\n });\n }\n\n /**\n * Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting.\n * @param {Context} ctx - The context of the incoming update.\n * @returns {Promise<boolean>} A Promise that resolves with a boolean indicating if the group is authorized.\n */\n private async isGroupAuthorized(ctx: Context): Promise<boolean> {\n const chatId = ctx.chat?.id.toString();\n if (!chatId) return false;\n\n const allowedChats = this.runtime.getSetting('TELEGRAM_ALLOWED_CHATS');\n if (!allowedChats) {\n return true;\n }\n\n try {\n const allowedChatsList = JSON.parse(allowedChats as string);\n return allowedChatsList.includes(chatId);\n } catch (error) {\n logger.error('Error parsing TELEGRAM_ALLOWED_CHATS:', error);\n return false;\n }\n }\n\n /**\n * Synchronizes an entity from a message context with the runtime system.\n * This method handles three cases:\n * 1. Message sender - most common case\n * 2. New chat member - when a user joins the chat\n * 3. Left chat member - when a user leaves the chat\n *\n * @param {Context} ctx - The context of the incoming update\n * @returns {Promise<void>}\n * @private\n */\n private async syncEntity(ctx: Context): Promise<void> {\n if (!ctx.chat) return;\n\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n const worldId = createUniqueUuid(this.runtime, chatId) as UUID;\n const roomId = createUniqueUuid(\n this.runtime,\n ctx.message?.message_thread_id\n ? `${ctx.chat.id}-${ctx.message.message_thread_id}`\n : ctx.chat.id.toString()\n ) as UUID;\n\n // Handle all three entity sync cases separately for clarity\n await this.syncMessageSender(ctx, worldId, roomId, chatId);\n await this.syncNewChatMember(ctx, worldId, roomId, chatId);\n await this.syncLeftChatMember(ctx);\n }\n\n /**\n * Synchronizes the message sender entity with the runtime system.\n * This is the most common entity sync case.\n *\n * @param {Context} ctx - The context of the incoming update\n * @param {UUID} worldId - The ID of the world\n * @param {UUID} roomId - The ID of the room\n * @param {string} chatId - The ID of the chat\n * @returns {Promise<void>}\n * @private\n */\n private async syncMessageSender(\n ctx: Context,\n worldId: UUID,\n roomId: UUID,\n chatId: string\n ): Promise<void> {\n // Handle message sender\n if (ctx.from && !this.syncedEntityIds.has(ctx.from.id.toString())) {\n const telegramId = ctx.from.id.toString();\n const entityId = createUniqueUuid(this.runtime, telegramId) as UUID;\n\n await this.runtime.ensureConnection({\n entityId,\n roomId: roomId,\n userName: ctx.from.username,\n userId: telegramId as UUID,\n name: ctx.from.first_name || ctx.from.username || 'Unknown User',\n source: 'telegram',\n channelId: chatId,\n serverId: chatId,\n type: ChannelType.GROUP,\n worldId: worldId,\n });\n\n this.syncedEntityIds.add(entityId);\n }\n }\n\n /**\n * Synchronizes a new chat member entity with the runtime system.\n * Triggered when a user joins the chat.\n *\n * @param {Context} ctx - The context of the incoming update\n * @param {UUID} worldId - The ID of the world\n * @param {UUID} roomId - The ID of the room\n * @param {string} chatId - The ID of the chat\n * @returns {Promise<void>}\n * @private\n */\n private async syncNewChatMember(\n ctx: Context,\n worldId: UUID,\n roomId: UUID,\n chatId: string\n ): Promise<void> {\n // Handle new chat member\n if (ctx.message && 'new_chat_member' in ctx.message) {\n const newMember = ctx.message.new_chat_member as any;\n const telegramId = newMember.id.toString();\n const entityId = createUniqueUuid(this.runtime, telegramId) as UUID;\n\n // Skip if we've already synced this entity\n if (this.syncedEntityIds.has(telegramId)) return;\n\n // We call ensure connection here for this user.\n await this.runtime.ensureConnection({\n entityId,\n roomId: roomId,\n userName: newMember.username,\n userId: telegramId,\n name: newMember.first_name || newMember.username || 'Unknown User',\n source: 'telegram',\n channelId: chatId,\n serverId: chatId,\n type: ChannelType.GROUP,\n worldId: worldId,\n });\n\n this.syncedEntityIds.add(entityId);\n\n this.runtime.emitEvent([TelegramEventTypes.ENTITY_JOINED], {\n runtime: this.runtime,\n entityId,\n worldId,\n newMember,\n ctx,\n });\n }\n }\n\n /**\n * Updates entity status when a user leaves the chat.\n *\n * @param {Context} ctx - The context of the incoming update\n * @returns {Promise<void>}\n * @private\n */\n private async syncLeftChatMember(ctx: Context): Promise<void> {\n // Handle left chat member\n if (ctx.message && 'left_chat_member' in ctx.message) {\n const leftMember = ctx.message.left_chat_member as any;\n const telegramId = leftMember.id.toString();\n const entityId = createUniqueUuid(this.runtime, telegramId) as UUID;\n\n const existingEntity = await this.runtime.getEntityById(entityId);\n if (existingEntity) {\n existingEntity.metadata = {\n ...existingEntity.metadata,\n status: 'INACTIVE',\n leftAt: Date.now(),\n };\n await this.runtime.updateEntity(existingEntity);\n }\n }\n }\n\n /**\n * Handles forum topics by creating appropriate rooms in the runtime system.\n * This enables proper conversation management for Telegram's forum feature.\n *\n * @param {Context} ctx - The context of the incoming update\n * @returns {Promise<void>}\n * @private\n */\n private async handleForumTopic(ctx: Context): Promise<void> {\n if (!ctx.chat || !ctx.message?.message_thread_id) return;\n\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n const worldId = createUniqueUuid(this.runtime, chatId) as UUID;\n\n const room = await this.buildForumTopicRoom(ctx, worldId);\n if (!room) return;\n\n await this.runtime.ensureRoomExists(room);\n }\n\n /**\n * Builds entity for message sender\n */\n private buildMsgSenderEntity(from: any): Entity | null {\n if (!from) return null;\n\n const userId = createUniqueUuid(this.runtime, from.id.toString()) as UUID;\n const telegramId = from.id.toString();\n\n return {\n id: userId,\n agentId: this.runtime.agentId,\n names: [from.first_name || from.username || 'Unknown User'],\n metadata: {\n telegram: {\n id: telegramId,\n username: from.username,\n name: from.first_name || from.username || 'Unknown User',\n },\n },\n };\n }\n\n /**\n * Handles new chat discovery and emits WORLD_JOINED event.\n * This is a critical function that ensures new chats are properly\n * registered in the runtime system and appropriate events are emitted.\n *\n * @param {Context} ctx - The context of the incoming update\n * @returns {Promise<void>}\n * @private\n */\n private async handleNewChat(ctx: Context): Promise<void> {\n if (!ctx.chat) return;\n\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n\n // Mark this chat as known\n this.knownChats.set(chatId, chat);\n\n // Get chat title and channel type\n const { chatTitle, channelType } = this.getChatTypeInfo(chat);\n\n const worldId = createUniqueUuid(this.runtime, chatId) as UUID;\n\n const existingWorld = await this.runtime.getWorld(worldId);\n if (existingWorld) {\n return;\n }\n\n const userId = ctx.from\n ? (createUniqueUuid(this.runtime, ctx.from.id.toString()) as UUID)\n : null;\n\n // Fetch admin information for proper role assignment\n let admins = [];\n let owner = null;\n if (chat.type === 'group' || chat.type === 'supergroup' || chat.type === 'channel') {\n try {\n admins = await ctx.getChatAdministrators();\n owner = admins.find((admin) => admin.status === 'creator');\n } catch (error) {\n logger.warn(`Could not get chat administrators: ${error.message}`);\n }\n }\n\n let ownerId = userId;\n\n if (owner) {\n ownerId = createUniqueUuid(this.runtime, String(owner.user.id)) as UUID;\n }\n\n // Build world representation\n const world: World = {\n id: worldId,\n name: chatTitle,\n agentId: this.runtime.agentId,\n serverId: chatId,\n metadata: {\n source: 'telegram',\n ownership: { ownerId },\n roles: ownerId\n ? {\n [ownerId]: Role.OWNER,\n }\n : {},\n chatType: chat.type,\n isForumEnabled: chat.type === 'supergroup' && chat.is_forum,\n },\n };\n\n // Directly ensure world exists instead of using syncTelegram\n await this.runtime.ensureWorldExists(world);\n\n // Create the main room for the chat\n const generalRoom: Room = {\n id: createUniqueUuid(this.runtime, chatId) as UUID,\n name: chatTitle,\n source: 'telegram',\n type: channelType,\n channelId: chatId,\n serverId: chatId,\n worldId,\n };\n\n // Directly ensure room exists instead of using syncTelegram\n await this.runtime.ensureRoomExists(generalRoom);\n\n // Prepare the rooms array starting with the main room\n const rooms = [generalRoom];\n\n // If this is a message in a forum topic, add the topic room as well\n if (chat.type === 'supergroup' && chat.is_forum && ctx.message?.message_thread_id) {\n const topicRoom = await this.buildForumTopicRoom(ctx, worldId);\n if (topicRoom) {\n rooms.push(topicRoom);\n }\n await this.runtime.ensureRoomExists(topicRoom);\n }\n\n // Build entities from chat\n const entities = await this.buildStandardizedEntities(chat);\n\n // Add sender if not already in entities\n if (ctx.from) {\n const senderEntity = this.buildMsgSenderEntity(ctx.from);\n if (senderEntity && !entities.some((e) => e.id === senderEntity.id)) {\n entities.push(senderEntity);\n this.syncedEntityIds.add(senderEntity.id);\n }\n }\n\n // Use the new batch processing method for entities\n await this.batchProcessEntities(\n entities,\n generalRoom.id,\n generalRoom.channelId,\n generalRoom.serverId,\n generalRoom.type,\n worldId\n );\n\n // Create payload for world events\n const telegramWorldPayload: TelegramWorldPayload = {\n runtime: this.runtime,\n world,\n rooms,\n entities,\n source: 'telegram',\n chat,\n botUsername: this.bot.botInfo.username,\n };\n\n // Emit telegram-specific world joined event\n if (chat.type !== 'private') {\n await this.runtime.emitEvent(TelegramEventTypes.WORLD_JOINED, telegramWorldPayload);\n }\n\n // Finally emit the standard WORLD_JOINED event\n await this.runtime.emitEvent(EventType.WORLD_JOINED, {\n runtime: this.runtime,\n world,\n rooms,\n entities,\n source: 'telegram',\n });\n }\n\n /**\n * Processes entities in batches to prevent overwhelming the system.\n *\n * @param {Entity[]} entities - The entities to process\n * @param {UUID} roomId - The ID of the room to connect entities to\n * @param {string} channelId - The channel ID\n * @param {string} serverId - The server ID\n * @param {ChannelType} roomType - The type of the room\n * @param {UUID} worldId - The ID of the world\n * @returns {Promise<void>}\n * @private\n */\n private async batchProcessEntities(\n entities: Entity[],\n roomId: UUID,\n channelId: string,\n serverId: string,\n roomType: ChannelType,\n worldId: UUID\n ): Promise<void> {\n const batchSize = 50;\n\n for (let i = 0; i < entities.length; i += batchSize) {\n const entityBatch = entities.slice(i, i + batchSize);\n\n // Process each entity in the batch concurrently\n await Promise.all(\n entityBatch.map(async (entity: Entity) => {\n try {\n await this.runtime.ensureConnection({\n entityId: entity.id,\n roomId: roomId,\n userName: entity.metadata?.telegram?.username,\n name: entity.metadata?.telegram?.name,\n userId: entity.metadata?.telegram?.id,\n source: 'telegram',\n channelId: channelId,\n serverId: serverId,\n type: roomType,\n worldId: worldId,\n });\n } catch (err) {\n logger.warn(`Failed to sync user ${entity.metadata?.telegram?.username}: ${err}`);\n }\n })\n );\n\n // Add a small delay between batches if not the last batch\n if (i + batchSize < entities.length) {\n await new Promise((resolve) => setTimeout(resolve, 500));\n }\n }\n }\n\n /**\n * Gets chat title and channel type based on Telegram chat type.\n * Maps Telegram-specific chat types to standardized system types.\n *\n * @param {any} chat - The Telegram chat object\n * @returns {Object} Object containing chatTitle and channelType\n * @private\n */\n private getChatTypeInfo(chat: any): { chatTitle: string; channelType: ChannelType } {\n let chatTitle: string;\n let channelType: ChannelType;\n\n switch (chat.type) {\n case 'private':\n chatTitle = `Chat with ${chat.first_name || 'Unknown User'}`;\n channelType = ChannelType.DM;\n break;\n case 'group':\n chatTitle = chat.title || 'Unknown Group';\n channelType = ChannelType.GROUP;\n break;\n case 'supergroup':\n chatTitle = chat.title || 'Unknown Supergroup';\n channelType = ChannelType.GROUP;\n break;\n case 'channel':\n chatTitle = chat.title || 'Unknown Channel';\n channelType = ChannelType.FEED;\n break;\n default:\n chatTitle = 'Unknown Chat';\n channelType = ChannelType.GROUP;\n }\n\n return { chatTitle, channelType };\n }\n\n /**\n * Builds standardized entity representations from Telegram chat data.\n * Transforms Telegram-specific user data into system-standard Entity objects.\n *\n * @param {any} chat - The Telegram chat object\n * @returns {Promise<Entity[]>} Array of standardized Entity objects\n * @private\n */\n private async buildStandardizedEntities(chat: any): Promise<Entity[]> {\n const entities: Entity[] = [];\n\n try {\n // For private chats, add the user\n if (chat.type === 'private' && chat.id) {\n const userId = createUniqueUuid(this.runtime, chat.id.toString()) as UUID;\n entities.push({\n id: userId,\n names: [chat.first_name || 'Unknown User'],\n agentId: this.runtime.agentId,\n metadata: {\n telegram: {\n id: chat.id.toString(),\n username: chat.username || 'unknown',\n name: chat.first_name || 'Unknown User',\n },\n source: 'telegram',\n },\n });\n this.syncedEntityIds.add(userId);\n } else if (chat.type === 'group' || chat.type === 'supergroup') {\n // For groups and supergroups, try to get member information\n try {\n // Get chat administrators (this is what's available through the Bot API)\n const admins = await this.bot.telegram.getChatAdministrators(chat.id);\n\n if (admins && admins.length > 0) {\n for (const admin of admins) {\n const userId = createUniqueUuid(this.runtime, admin.user.id.toString()) as UUID;\n entities.push({\n id: userId,\n names: [admin.user.first_name || admin.user.username || 'Unknown Admin'],\n agentId: this.runtime.agentId,\n metadata: {\n telegram: {\n id: admin.user.id.toString(),\n username: admin.user.username || 'unknown',\n name: admin.user.first_name || 'Unknown Admin',\n isAdmin: true,\n adminTitle:\n admin.custom_title || (admin.status === 'creator' ? 'Owner' : 'Admin'),\n },\n source: 'telegram',\n roles: [admin.status === 'creator' ? Role.OWNER : Role.ADMIN],\n },\n });\n this.syncedEntityIds.add(userId);\n }\n }\n } catch (error) {\n logger.warn(`Could not fetch administrators for chat ${chat.id}: ${error}`);\n }\n }\n } catch (error) {\n logger.error(\n `Error building standardized entities: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n return entities;\n }\n\n /**\n * Extracts and builds the room object for a forum topic from a message context.\n * This refactored method can be used both in middleware and when handling new chats.\n *\n * @param {Context} ctx - The context of the incoming update\n * @param {UUID} worldId - The ID of the world the topic belongs to\n * @returns {Promise<Room | null>} A Promise that resolves with the room or null if not a topic\n * @private\n */\n private async buildForumTopicRoom(ctx: Context, worldId: UUID): Promise<Room | null> {\n if (!ctx.chat || !ctx.message?.message_thread_id) return null;\n if (ctx.chat.type !== 'supergroup' || !ctx.chat.is_forum) return null;\n\n const chat = ctx.chat;\n const chatId = chat.id.toString();\n const threadId = ctx.message.message_thread_id.toString();\n const roomId = createUniqueUuid(this.runtime, `${chatId}-${threadId}`) as UUID;\n\n try {\n // Ensure the message object is fully initialized\n const replyMessage = JSON.parse(JSON.stringify(ctx.message));\n\n // Default topic name\n let topicName = `Topic #${threadId}`;\n\n // Check if forum_topic_created exists directly in the message\n if (\n replyMessage &&\n typeof replyMessage === 'object' &&\n 'forum_topic_created' in replyMessage &&\n replyMessage.forum_topic_created\n ) {\n const topicCreated = replyMessage.forum_topic_created;\n if (topicCreated && typeof topicCreated === 'object' && 'name' in topicCreated) {\n topicName = topicCreated.name;\n }\n }\n // Check if forum_topic_created exists in reply_to_message\n else if (\n replyMessage &&\n typeof replyMessage === 'object' &&\n 'reply_to_message' in replyMessage &&\n replyMessage.reply_to_message &&\n typeof replyMessage.reply_to_message === 'object' &&\n 'forum_topic_created' in replyMessage.reply_to_message &&\n replyMessage.reply_to_message.forum_topic_created\n ) {\n const topicCreated = replyMessage.reply_to_message.forum_topic_created;\n if (topicCreated && typeof topicCreated === 'object' && 'name' in topicCreated) {\n topicName = topicCreated.name;\n }\n }\n\n // Create a room for this topic\n const room: Room = {\n id: roomId,\n name: topicName,\n source: 'telegram',\n type: ChannelType.GROUP,\n channelId: `${chatId}-${threadId}`,\n serverId: chatId,\n worldId,\n metadata: {\n threadId: threadId,\n isForumTopic: true,\n parentChatId: chatId,\n },\n };\n\n return room;\n } catch (error) {\n logger.error(\n `Error building forum topic room: ${error instanceof Error ? error.message : String(error)}`\n );\n return null;\n }\n }\n}\n","import type { IAgentRuntime } from '@elizaos/core';\nimport { z } from 'zod';\n\nexport const telegramEnvSchema = z.object({\n TELEGRAM_BOT_TOKEN: z.string().min(1, 'Telegram bot token is required'),\n});\n\n/**\n * Represents the type definition for configuring a Telegram bot based on the inferred schema.\n */\nexport type TelegramConfig = z.infer<typeof telegramEnvSchema>;\n\n/**\n * Validates the Telegram configuration by retrieving the Telegram bot token from the runtime settings or environment variables.\n *\n * @param {IAgentRuntime} runtime - The agent runtime used to get the setting.\n * @returns {Promise<TelegramConfig>} A promise that resolves with the validated Telegram configuration.\n */\nexport async function validateTelegramConfig(runtime: IAgentRuntime): Promise<TelegramConfig> {\n try {\n const config = {\n TELEGRAM_BOT_TOKEN:\n runtime.getSetting('TELEGRAM_BOT_TOKEN') || process.env.TELEGRAM_BOT_TOKEN,\n };\n\n return telegramEnvSchema.parse(config);\n } catch (error) {\n if (error instanceof z.ZodError) {\n const errorMessages = error.errors\n .map((err) => `${err.path.join('.')}: ${err.message}`)\n .join('\\n');\n throw new Error(`Telegram configuration validation failed:\\n${errorMessages}`);\n }\n throw error;\n }\n}\n","import {\n ChannelType,\n type Content,\n EventType,\n type HandlerCallback,\n type IAgentRuntime,\n type Media,\n type Memory,\n ModelType,\n type UUID,\n createUniqueUuid,\n logger,\n} from '@elizaos/core';\nimport type { Chat, Message, ReactionType, Update } from '@telegraf/types';\nimport type { Context, NarrowedContext, Telegraf } from 'telegraf';\nimport {\n TelegramEventTypes,\n type TelegramMessageReceivedPayload,\n type TelegramMessageSentPayload,\n type TelegramReactionReceivedPayload,\n} from './types';\nimport { escapeMarkdown } from './utils';\n\nimport fs from 'node:fs';\n\n/**\n * Enum representing different types of media.\n * @enum { string }\n * @readonly\n */\nexport enum MediaType {\n PHOTO = 'photo',\n VIDEO = 'video',\n DOCUMENT = 'document',\n AUDIO = 'audio',\n ANIMATION = 'animation',\n}\n\nconst MAX_MESSAGE_LENGTH = 4096; // Telegram's max message length\n\nconst getChannelType = (chat: Chat): ChannelType => {\n if (chat.type === 'private') return ChannelType.DM;\n if (chat.type === 'supergroup') return ChannelType.GROUP;\n if (chat.type === 'channel') return ChannelType.GROUP;\n if (chat.type === 'group') return ChannelType.GROUP;\n};\n\n/**\n * Class representing a message manager.\n * @class\n */\nexport class MessageManager {\n public bot: Telegraf<Context>;\n protected runtime: IAgentRuntime;\n\n /**\n * Constructor for creating a new instance of a BotAgent.\n *\n * @param {Telegraf<Context>} bot - The Telegraf instance used for interacting with the bot platform.\n * @param {IAgentRuntime} runtime - The runtime environment for the agent.\n */\n constructor(bot: Telegraf<Context>, runtime: IAgentRuntime) {\n this.bot = bot;\n this.runtime = runtime;\n }\n\n // Process image messages and generate descriptions\n /**\n * Process an image from a Telegram message to extract the image URL and description.\n *\n * @param {Message} message - The Telegram message object containing the image.\n * @returns {Promise<{ description: string } | null>} The description of the processed image or null if no image found.\n */\n async processImage(message: Message): Promise<{ description: string } | null> {\n try {\n let imageUrl: string | null = null;\n\n logger.info(`Telegram Message: ${message}`);\n\n if ('photo' in message && message.photo?.length > 0) {\n const photo = message.photo[message.photo.length - 1];\n const fileLink = await this.bot.telegram.getFileLink(photo.file_id);\n imageUrl = fileLink.toString();\n } else if ('document' in message && message.document?.mime_type?.startsWith('image/')) {\n const fileLink = await this.bot.telegram.getFileLink(message.document.file_id);\n imageUrl = fileLink.toString();\n }\n\n if (imageUrl) {\n const { title, description } = await this.runtime.useModel(\n ModelType.IMAGE_DESCRIPTION,\n imageUrl\n );\n return { description: `[Image: ${title}\\n${description}]` };\n }\n } catch (error) {\n console.error('❌ Error processing image:', error);\n }\n\n return null;\n }\n\n // Send long messages in chunks\n /**\n * Sends a message in chunks, handling attachments and splitting the message if necessary\n *\n * @param {Context} ctx - The context object representing the current state of the bot\n * @param {Content} content - The content of the message to be sent\n * @param {number} [replyToMessageId] - The ID of the message to reply to, if any\n * @returns {Promise<Message.TextMessage[]>} - An array of TextMessage objects representing the messages sent\n */\n async sendMessageInChunks(\n ctx: Context,\n content: Content,\n replyToMessageId?: number\n ): Promise<Message.TextMessage[]> {\n if (content.attachments && content.attachments.length > 0) {\n content.attachments.map(async (attachment: Media) => {\n const typeMap: { [key: string]: MediaType } = {\n 'image/gif': MediaType.ANIMATION,\n image: MediaType.PHOTO,\n doc: MediaType.DOCUMENT,\n video: MediaType.VIDEO,\n audio: MediaType.AUDIO,\n };\n\n let mediaType: MediaType | undefined = undefined;\n\n for (const prefix in typeMap) {\n if (attachment.contentType.startsWith(prefix)) {\n mediaType = typeMap[prefix];\n break;\n }\n }\n\n if (!mediaType) {\n throw new Error(\n `Unsupported Telegram attachment content type: ${attachment.contentType}`\n );\n }\n\n await this.sendMedia(ctx, attachment.url, mediaType, attachment.description);\n });\n } else {\n const chunks = this.splitMessage(content.text);\n const sentMessages: Message.TextMessage[] = [];\n\n for (let i = 0; i < chunks.length; i++) {\n const chunk = escapeMarkdown(chunks[i]);\n const sentMessage = (await ctx.telegram.sendMessage(ctx.chat.id, chunk, {\n reply_parameters:\n i === 0 && replyToMessageId ? { message_id: replyToMessageId } : undefined,\n parse_mode: 'Markdown',\n })) as Message.TextMessage;\n\n sentMessages.push(sentMessage);\n }\n\n return sentMessages;\n }\n }\n\n /**\n * Sends media to a chat using the Telegram API.\n *\n * @param {Context} ctx - The context object containing information about the current chat.\n * @param {string} mediaPath - The path to the media to be sent, either a URL or a local file path.\n * @param {MediaType} type - The type of media being sent (PHOTO, VIDEO, DOCUMENT, AUDIO, or ANIMATION).\n * @param {string} [caption] - Optional caption for the media being sent.\n *\n * @returns {Promise<void>} A Promise that resolves when the media is successfully sent.\n */\n async sendMedia(\n ctx: Context,\n mediaPath: string,\n type: MediaType,\n caption?: string\n ): Promise<void> {\n try {\n const isUrl = /^(http|https):\\/\\//.test(mediaPath);\n const sendFunctionMap: Record<MediaType, Function> = {\n [MediaType.PHOTO]: ctx.telegram.sendPhoto.bind(ctx.telegram),\n [MediaType.VIDEO]: ctx.telegram.sendVideo.bind(ctx.telegram),\n [MediaType.DOCUMENT]: ctx.telegram.sendDocument.bind(ctx.telegram),\n [MediaType.AUDIO]: ctx.telegram.sendAudio.bind(ctx.telegram),\n [MediaType.ANIMATION]: ctx.telegram.sendAnimation.bind(ctx.telegram),\n };\n\n const sendFunction = sendFunctionMap[type];\n\n if (!sendFunction) {\n throw new Error(`Unsupported media type: ${type}`);\n }\n\n if (isUrl) {\n // Handle HTTP URLs\n await sendFunction(ctx.chat.id, mediaPath, { caption });\n } else {\n // Handle local file paths\n if (!fs.existsSync(mediaPath)) {\n throw new Error(`File not found at path: ${mediaPath}`);\n }\n\n const fileStream = fs.createReadStream(mediaPath);\n\n try {\n await sendFunction(ctx.chat.id, { source: fileStream }, { caption });\n } finally {\n fileStream.destroy();\n }\n }\n\n logger.info(\n `${type.charAt(0).toUpperCase() + type.slice(1)} sent successfully: ${mediaPath}`\n );\n } catch (error) {\n logger.error(`Failed to send ${type}. Path: ${mediaPath}. Error: ${error.message}`);\n logger.debug(error.stack);\n throw error;\n }\n }\n\n // Split message into smaller parts\n /**\n * Splits a given text into an array of strings based on the maximum message length.\n *\n * @param {string} text - The text to split into chunks.\n * @returns {string[]} An array of strings with each element representing a chunk of the original text.\n */\n private splitMessage(text: string): string[] {\n const chunks: string[] = [];\n let currentChunk = '';\n\n const lines = text.split('\\n');\n for (const line of lines) {\n if (currentChunk.length + line.length + 1 <= MAX_MESSAGE_LENGTH) {\n currentChunk += (currentChunk ? '\\n' : '') + line;\n } else {\n if (currentChunk) chunks.push(currentChunk);\n currentChunk = line;\n }\n }\n\n if (currentChunk) chunks.push(currentChunk);\n return chunks;\n }\n\n // Main handler for incoming messages\n /**\n * Handle incoming messages from Telegram and process them accordingly.\n * @param {Context} ctx - The context object containing information about the message.\n * @returns {Promise<void>}\n */\n public async handleMessage(ctx: Context): Promise<void> {\n // Type guard to ensure message exists\n if (!ctx.message || !ctx.from) return;\n\n const message = ctx.message as Message.TextMessage;\n\n try {\n // Convert IDs to UUIDs\n const entityId = createUniqueUuid(this.runtime, ctx.from.id.toString()) as UUID;\n\n const threadId =\n 'is_topic_message' in message && message.is_topic_message\n ? message.message_thread_id?.toString()\n : undefined;\n\n // Generate room ID based on whether this is in a forum topic\n const roomId = createUniqueUuid(\n this.runtime,\n threadId ? `${ctx.chat.id}-${threadId}` : ctx.chat.id.toString()\n ) as UUID;\n\n // Get message ID\n const messageId = createUniqueUuid(this.runtime, message?.message_id?.toString());\n\n // Handle images\n const imageInfo = await this.processImage(message);\n\n // Get message text - use type guards for safety\n let messageText = '';\n if ('text' in message && message.text) {\n messageText = message.text;\n } else if ('caption' in message && message.caption) {\n messageText = message.caption as string;\n }\n\n // Combine text and image description\n const fullText = imageInfo ? `${messageText} ${imageInfo.description}` : messageText;\n if (!fullText) return;\n\n // Get chat type and determine channel type\n const chat = message.chat as Chat;\n const channelType = getChannelType(chat);\n\n // Create the memory object\n const memory: Memory = {\n id: messageId,\n entityId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n text: fullText,\n source: 'telegram',\n channelType: channelType,\n inReplyTo:\n 'reply_to_message' in message && message.reply_to_message\n ? createUniqueUuid(this.runtime, message.reply_to_message.message_id.toString())\n : undefined,\n },\n createdAt: message.date * 1000,\n };\n\n // Create callback for handling responses\n const callback: HandlerCallback = async (content: Content, _files?: string[]) => {\n try {\n // If response is from reasoning do not send it.\n if (!content.text) return [];\n\n const sentMessages = await this.sendMessageInChunks(ctx, content, message.message_id);\n\n if (!sentMessages) return [];\n\n const memories: Memory[] = [];\n for (let i = 0; i < sentMessages.length; i++) {\n const sentMessage = sentMessages[i];\n const _isLastMessage = i === sentMessages.length - 1;\n\n const responseMemory: Memory = {\n id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),\n entityId: this.runtime.agentId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n ...content,\n text: sentMessage.text,\n inReplyTo: messageId,\n channelType: channelType,\n },\n createdAt: sentMessage.date * 1000,\n };\n\n await this.runtime.createMemory(responseMemory, 'messages');\n memories.push(responseMemory);\n }\n\n return memories;\n } catch (error) {\n logger.error('Error in message callback:', error);\n return [];\n }\n };\n\n // Let the bootstrap plugin handle the message\n this.runtime.emitEvent(EventType.MESSAGE_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n });\n\n // Also emit the platform-specific event\n this.runtime.emitEvent(TelegramEventTypes.MESSAGE_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n ctx,\n originalMessage: message,\n } as TelegramMessageReceivedPayload);\n } catch (error) {\n logger.error('Error handling Telegram message:', {\n error,\n chatId: ctx.chat?.id,\n messageId: ctx.message?.message_id,\n from: ctx.from?.username || ctx.from?.id,\n });\n throw error;\n }\n }\n\n /**\n * Handles the reaction event triggered by a user reacting to a message.\n * @param {NarrowedContext<Context<Update>, Update.MessageReactionUpdate>} ctx The context of the message reaction update\n * @returns {Promise<void>} A Promise that resolves when the reaction handling is complete\n */\n public async handleReaction(\n ctx: NarrowedContext<Context<Update>, Update.MessageReactionUpdate>\n ): Promise<void> {\n // Ensure we have the necessary data\n if (!ctx.update.message_reaction || !ctx.from) return;\n\n const reaction = ctx.update.message_reaction;\n const reactionType = reaction.new_reaction[0].type;\n const reactionEmoji = (reaction.new_reaction[0] as ReactionType).type;\n\n try {\n const entityId = createUniqueUuid(this.runtime, ctx.from.id.toString()) as UUID;\n const roomId = createUniqueUuid(this.runtime, ctx.chat.id.toString());\n\n const reactionId = createUniqueUuid(\n this.runtime,\n `${reaction.message_id}-${ctx.from.id}-${Date.now()}`\n );\n\n // Create reaction memory\n const memory: Memory = {\n id: reactionId,\n entityId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n channelType: getChannelType(reaction.chat as Chat),\n text: `Reacted with: ${reactionType === 'emoji' ? reactionEmoji : reactionType}`,\n source: 'telegram',\n inReplyTo: createUniqueUuid(this.runtime, reaction.message_id.toString()),\n },\n createdAt: Date.now(),\n };\n\n // Create callback for handling reaction responses\n const callback: HandlerCallback = async (content: Content) => {\n try {\n const sentMessage = await ctx.reply(content.text);\n const responseMemory: Memory = {\n id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),\n entityId: this.runtime.agentId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n ...content,\n inReplyTo: reactionId,\n },\n createdAt: sentMessage.date * 1000,\n };\n return [responseMemory];\n } catch (error) {\n logger.error('Error in reaction callback:', error);\n return [];\n }\n };\n\n // Let the bootstrap plugin handle the reaction\n this.runtime.emitEvent(EventType.REACTION_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n });\n\n // Also emit the platform-specific event\n this.runtime.emitEvent(TelegramEventTypes.REACTION_RECEIVED, {\n runtime: this.runtime,\n message: memory,\n callback,\n source: 'telegram',\n ctx,\n reactionString: reactionType === 'emoji' ? reactionEmoji : reactionType,\n originalReaction: reaction.new_reaction[0] as ReactionType,\n } as TelegramReactionReceivedPayload);\n } catch (error) {\n logger.error('Error handling reaction:', error);\n }\n }\n\n /**\n * Sends a message to a Telegram chat and emits appropriate events\n * @param {number | string} chatId - The Telegram chat ID to send the message to\n * @param {Content} content - The content to send\n * @param {number} [replyToMessageId] - Optional message ID to reply to\n * @returns {Promise<Message.TextMessage[]>} The sent messages\n */\n public async sendMessage(\n chatId: number | string,\n content: Content,\n replyToMessageId?: number\n ): Promise<Message.TextMessage[]> {\n try {\n // Create a context-like object for sending\n const ctx = {\n chat: { id: chatId },\n telegram: this.bot.telegram,\n };\n\n const sentMessages = await this.sendMessageInChunks(\n ctx as Context,\n content,\n replyToMessageId\n );\n\n if (!sentMessages?.length) return [];\n\n // Create room ID\n const roomId = createUniqueUuid(this.runtime, chatId.toString());\n\n // Create memories for the sent messages\n const memories: Memory[] = [];\n for (const sentMessage of sentMessages) {\n const memory: Memory = {\n id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),\n entityId: this.runtime.agentId,\n agentId: this.runtime.agentId,\n roomId,\n content: {\n ...content,\n text: sentMessage.text,\n source: 'telegram',\n channelType: getChannelType({\n id: typeof chatId === 'string' ? Number.parseInt(chatId, 10) : chatId,\n type: 'private', // Default to private, will be overridden if in context\n } as Chat),\n },\n createdAt: sentMessage.date * 1000,\n };\n\n await this.runtime.createMemory(memory, 'messages');\n memories.push(memory);\n }\n\n // Emit both generic and platform-specific message sent events\n this.runtime.emitEvent(EventType.MESSAGE_SENT, {\n runtime: this.runtime,\n message: {\n content: content,\n },\n roomId,\n source: 'telegram',\n });\n\n // Also emit platform-specific event\n this.runtime.emitEvent(TelegramEventTypes.MESSAGE_SENT, {\n originalMessages: sentMessages,\n chatId,\n } as TelegramMessageSentPayload);\n\n return sentMessages;\n } catch (error) {\n logger.error('Error sending message to Telegram:', error);\n return [];\n }\n }\n}\n","/**\n * Escapes Markdown special characters in the given text, excluding code blocks.\n * @param {string} text - The text to escape Markdown characters from.\n * @returns {string} The text with escaped Markdown characters.\n */\nexport function escapeMarkdown(text: string): string {\n // Don't escape if it's a code block\n if (text.startsWith('```') && text.endsWith('```')) {\n return text;\n }\n\n // Split the text by code blocks\n const parts = text.split(/(```[\\s\\S]*?```)/g);\n\n return parts\n .map((part, index) => {\n // If it's a code block (odd indices in the split result will be code blocks)\n if (index % 2 === 1) {\n return part;\n }\n // For regular text, only escape characters that need escaping in Markdown\n return (\n part\n // First preserve any intended inline code spans\n .replace(/`.*?`/g, (match) => match)\n // Then only escape the minimal set of special characters that need escaping in Markdown mode\n .replace(/([*_`\\\\])/g, '\\\\$1')\n );\n })\n .join('');\n}\n\n/**\n * Splits a message into chunks that fit within Telegram's message length limit\n */\n/**\n * Splits a text message into chunks based on a maximum length for each chunk.\n *\n * @param {string} text - The text message to split.\n * @param {number} maxLength - The maximum length for each chunk (default is 4096).\n * @returns {string[]} An array containing the text message split into chunks.\n */\nexport function splitMessage(text: string, maxLength = 4096): string[] {\n const chunks: string[] = [];\n let currentChunk = '';\n\n const lines = text.split('\\n');\n for (const line of lines) {\n if (currentChunk.length + line.length + 1 <= maxLength) {\n currentChunk += (currentChunk ? '\\n' : '') + line;\n } else {\n if (currentChunk) chunks.push(currentChunk);\n currentChunk = line;\n }\n }\n\n if (currentChunk) chunks.push(currentChunk);\n return chunks;\n}\n","import { type IAgentRuntime, type TestSuite, logger } from '@elizaos/core';\nimport type { Chat, User } from '@telegraf/types';\nimport type { Telegraf } from 'telegraf';\nimport type { Context } from 'telegraf';\nimport type { MessageManager } from './messageManager';\nimport type { TelegramService } from './service';\n\nconst TEST_IMAGE_URL =\n 'https://github.com/elizaOS/awesome-eliza/blob/main/assets/eliza-logo.jpg?raw=true';\n\n/**\n * Represents a test suite for testing Telegram functionality.\n *\n * This test suite includes methods to initialize and validate a Telegram bot connection,\n * send basic text messages to a Telegram chat, send text messages with image attachments,\n * handle and process incoming Telegram messages, and process and validate image attachments\n * in incoming messages.\n *\n * @implements {TestSuite}\n */\n\nexport class TelegramTestSuite implements TestSuite {\n name = 'telegram';\n private telegramClient: TelegramService = null;\n private bot: Telegraf<Context> | null = null;\n private messageManager: MessageManager | null = null;\n tests: { name: string; fn: (runtime: IAgentRuntime) => Promise<void> }[];\n\n /**\n * Constructor for initializing a set of test cases for a Telegram bot.\n *\n * @constructor\n * @property {Array<Object>} tests - An array of test cases with name and corresponding test functions.\n * @property {string} tests.name - The name of the test case.\n * @property {function} tests.fn - The test function to be executed.\n */\n constructor() {\n this.tests = [\n {\n name: 'Initialize and Validate Telegram Bot Connection',\n fn: this.testCreatingTelegramBot.bind(this),\n },\n {\n name: 'Send Basic Text Message to Telegram Chat',\n fn: this.testSendingTextMessage.bind(this),\n },\n {\n name: 'Send Text Message with an Image Attachment',\n fn: this.testSendingMessageWithAttachment.bind(this),\n },\n {\n name: 'Handle and Process Incoming Telegram Messages',\n fn: this.testHandlingMessage.bind(this),\n },\n {\n name: 'Process and Validate Image Attachments in Incoming Messages',\n fn: this.testProcessingImages.bind(this),\n },\n ];\n }\n\n /**\n * Retrieves the Telegram test chat ID from environment variables.\n *\n * Reference on getting the Telegram chat ID:\n * https://stackoverflow.com/a/32572159\n */\n /**\n * Validates the chat ID by checking if it is set in the runtime settings or environment variables.\n * If not set, an error is thrown with a message instructing to provide a valid chat ID.\n * @param {IAgentRuntime} runtime - The runtime object that provides access to the settings and environment variables.\n * @throws {Error} If TELEGRAM_TEST_CHAT_ID is not set in the runtime settings or environment variables.\n * @returns {string} The validated chat ID.\n */\n validateChatId(runtime: IAgentRuntime) {\n const testChatId =\n runtime.getSetting('TELEGRAM_TEST_CHAT_ID') || process.env.TELEGRAM_TEST_CHAT_ID;\n if (!testChatId) {\n throw new Error(\n 'TELEGRAM_TEST_CHAT_ID is not set. Please provide a valid chat ID in the environment variables.'\n );\n }\n return testChatId;\n }\n\n async getChatInfo(runtime: IAgentRuntime): Promise<Context['chat']> {\n try {\n const chatId = this.validateChatId(runtime);\n const chat = await this.bot.telegram.getChat(chatId);\n logger.log(`Fetched real chat: ${JSON.stringify(chat)}`);\n return chat;\n } catch (error) {\n throw new Error(`Error fetching real Telegram chat: ${error}`);\n }\n }\n\n async testCreatingTelegramBot(runtime: IAgentRuntime) {\n this.telegramClient = runtime.getService('telegram') as TelegramService;\n this.bot = this.telegramClient.messageManager.bot;\n this.messageManager = this.telegramClient.messageManager;\n logger.debug('Telegram bot initialized successfully.');\n }\n\n async testSendingTextMessage(runtime: IAgentRuntime) {\n try {\n if (!this.bot) throw new Error('Bot not initialized.');\n\n const chatId = this.validateChatId(runtime);\n await this.bot.telegram.sendMessage(chatId, 'Testing Telegram message!');\n logger.debug('Message sent successfully.');\n } catch (error) {\n throw new Error(`Error sending Telegram message: ${error}`);\n }\n }\n\n async testSendingMessageWithAttachment(runtime: IAgentRuntime) {\n try {\n if (!this.messageManager) throw new Error('MessageManager not initialized.');\n\n const chat = await this.getChatInfo(runtime);\n const mockContext: Partial<Context> = {\n chat,\n from: { id: 123, username: 'TestUser' } as User,\n telegram: this.bot.telegram,\n };\n\n const messageContent = {\n text: 'Here is an image attachment:',\n attachments: [\n {\n id: '123',\n title: 'Sample Image',\n source: TEST_IMAGE_URL,\n text: 'Sample Image',\n url: TEST_IMAGE_URL,\n contentType: 'image/png',\n description: 'Sample Image',\n },\n ],\n };\n\n await this.messageManager.sendMessageInChunks(mockContext as Context, messageContent);\n\n logger.success('Message with image attachment sent successfully.');\n } catch (error) {\n throw new Error(`Error sending Telegram message with attachment: ${error}`);\n }\n }\n\n async testHandlingMessage(runtime: IAgentRuntime) {\n try {\n const chat = await this.getChatInfo(runtime);\n const mockContext: Partial<Context> = {\n chat,\n from: {\n id: 123,\n username: 'TestUser',\n is_bot: false,\n first_name: 'Test',\n last_name: 'User',\n } as User,\n message: {\n message_id: undefined,\n text: `@${this.bot.botInfo?.username}! Hello!`,\n date: Math.floor(Date.now() / 1000),\n chat,\n } as any,\n telegram: this.bot.telegram,\n };\n\n try {\n await this.messageManager.handleMessage(mockContext as Context);\n } catch (error) {\n throw new Error(`Error handling Telegram message: ${error}`);\n }\n } catch (error) {\n throw new Error(`Error handling Telegram message: ${error}`);\n }\n }\n\n async testProcessingImages(runtime: IAgentRuntime) {\n try {\n const chatId = this.validateChatId(runtime);\n const fileId = await this.getFileId(chatId, TEST_IMAGE_URL);\n\n const mockMessage = {\n message_id: undefined,\n chat: { id: chatId } as Chat,\n date: Math.floor(Date.now() / 1000),\n photo: [{ file_id: fileId }],\n text: `@${this.bot.botInfo?.username}!`,\n };\n\n const { description } = await this.messageManager.processImage(mockMessage);\n if (!description) {\n throw new Error('Error processing Telegram image');\n }\n logger.log(`Processing Telegram image successfully: ${description}`);\n } catch (error) {\n throw new Error(`Error processing Telegram image: ${error}`);\n }\n }\n\n async getFileId(chatId: string, imageUrl: string) {\n try {\n const message = await this.bot.telegram.sendPhoto(chatId, imageUrl);\n return message.photo[message.photo.length - 1].file_id;\n } catch (error) {\n logger.error(`Error sending image: ${error}`);\n throw error;\n }\n }\n}\n","import type { Plugin } from '@elizaos/core';\nimport { TELEGRAM_SERVICE_NAME } from './constants';\nimport { TelegramService } from './service';\nimport { TelegramTestSuite } from './tests';\n\nconst telegramPlugin: Plugin = {\n name: TELEGRAM_SERVICE_NAME,\n description: 'Telegram client plugin',\n services: [TelegramService],\n tests: [new TelegramTestSuite()],\n};\nexport default telegramPlugin;\n"],"mappings":";AAAO,IAAM,oBAAoB;AAAA,EAC/B,cAAc;AAAA,EACd,sBAAsB;AAAA,EACtB,oBAAoB;AA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e;AAAA,EAC1B,OAAO,CAAC,IAAI,kBAAkB,CAAC;AACjC;AACA,IAAO,gBAAQ;","names":["ChannelType","EventType","createUniqueUuid","logger","logger","createUniqueUuid","ChannelType","EventType","logger"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elizaos/plugin-telegram",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "2e45ccd9535c11f9778e8268fb6c048aa4f52d7c"
|
|
45
45
|
}
|