@elizaos/plugin-telegram 1.6.2 → 1.6.4
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/environment.d.ts +1 -1
- package/dist/index.js +109 -146
- package/dist/index.js.map +1 -1
- package/dist/service.d.ts +0 -1
- package/dist/tests.d.ts +1 -1
- package/package.json +9 -3
package/dist/environment.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -144,7 +144,7 @@ function convertToTelegramButtons(buttons) {
|
|
|
144
144
|
telegramButton = Markup.button.url(button.text, button.url);
|
|
145
145
|
break;
|
|
146
146
|
default:
|
|
147
|
-
logger.warn(
|
|
147
|
+
logger.warn({ src: "plugin:telegram", buttonKind: button.kind }, "Unknown button kind, treating as URL button");
|
|
148
148
|
telegramButton = Markup.button.url(button.text, button.url);
|
|
149
149
|
break;
|
|
150
150
|
}
|
|
@@ -194,7 +194,7 @@ var MessageManager = class {
|
|
|
194
194
|
async processImage(message) {
|
|
195
195
|
try {
|
|
196
196
|
let imageUrl = null;
|
|
197
|
-
logger2.
|
|
197
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, messageId: message.message_id }, "Processing image from message");
|
|
198
198
|
if ("photo" in message && message.photo?.length > 0) {
|
|
199
199
|
const photo = message.photo[message.photo.length - 1];
|
|
200
200
|
const fileLink = await this.bot.telegram.getFileLink(photo.file_id);
|
|
@@ -212,7 +212,7 @@ var MessageManager = class {
|
|
|
212
212
|
${description}]` };
|
|
213
213
|
}
|
|
214
214
|
} catch (error) {
|
|
215
|
-
|
|
215
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error processing image");
|
|
216
216
|
}
|
|
217
217
|
return null;
|
|
218
218
|
}
|
|
@@ -231,9 +231,7 @@ ${description}]` };
|
|
|
231
231
|
const document = message.document;
|
|
232
232
|
const fileLink = await this.bot.telegram.getFileLink(document.file_id);
|
|
233
233
|
const documentUrl = fileLink.toString();
|
|
234
|
-
logger2.
|
|
235
|
-
`Processing document: ${document.file_name} (${document.mime_type}, ${document.file_size} bytes)`
|
|
236
|
-
);
|
|
234
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: document.file_name, mimeType: document.mime_type, fileSize: document.file_size }, "Processing document");
|
|
237
235
|
const documentProcessor = this.getDocumentProcessor(document.mime_type);
|
|
238
236
|
if (documentProcessor) {
|
|
239
237
|
return await documentProcessor(document, documentUrl);
|
|
@@ -249,7 +247,7 @@ Size: ${document.file_size || 0} bytes]`,
|
|
|
249
247
|
fileSize: document.file_size
|
|
250
248
|
};
|
|
251
249
|
} catch (error) {
|
|
252
|
-
logger2.error({ error }, "Error processing document");
|
|
250
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error processing document");
|
|
253
251
|
return null;
|
|
254
252
|
}
|
|
255
253
|
}
|
|
@@ -278,7 +276,7 @@ Size: ${document.file_size || 0} bytes]`,
|
|
|
278
276
|
try {
|
|
279
277
|
const pdfService = this.runtime.getService(ServiceType.PDF);
|
|
280
278
|
if (!pdfService) {
|
|
281
|
-
logger2.warn("PDF service not available, using fallback");
|
|
279
|
+
logger2.warn({ src: "plugin:telegram", agentId: this.runtime.agentId }, "PDF service not available, using fallback");
|
|
282
280
|
return {
|
|
283
281
|
title: `PDF Document: ${document.file_name || "Unknown Document"}`,
|
|
284
282
|
fullText: "",
|
|
@@ -296,7 +294,7 @@ Unable to extract text content]`,
|
|
|
296
294
|
}
|
|
297
295
|
const pdfBuffer = await response.arrayBuffer();
|
|
298
296
|
const text = await pdfService.convertPdfToText(Buffer.from(pdfBuffer));
|
|
299
|
-
logger2.
|
|
297
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: document.file_name, charactersExtracted: text.length }, "PDF processed successfully");
|
|
300
298
|
return {
|
|
301
299
|
title: document.file_name || "Unknown Document",
|
|
302
300
|
fullText: text,
|
|
@@ -308,7 +306,7 @@ Text extracted successfully: ${text.length} characters]`,
|
|
|
308
306
|
fileSize: document.file_size
|
|
309
307
|
};
|
|
310
308
|
} catch (error) {
|
|
311
|
-
logger2.error({ error }, "Error processing PDF document");
|
|
309
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: document.file_name, error: error instanceof Error ? error.message : String(error) }, "Error processing PDF document");
|
|
312
310
|
return {
|
|
313
311
|
title: `PDF Document: ${document.file_name || "Unknown Document"}`,
|
|
314
312
|
fullText: "",
|
|
@@ -331,7 +329,7 @@ Error: Unable to extract text content]`,
|
|
|
331
329
|
throw new Error(`Failed to fetch text document: ${response.status}`);
|
|
332
330
|
}
|
|
333
331
|
const text = await response.text();
|
|
334
|
-
logger2.
|
|
332
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: document.file_name, charactersExtracted: text.length }, "Text document processed successfully");
|
|
335
333
|
return {
|
|
336
334
|
title: document.file_name || "Unknown Document",
|
|
337
335
|
fullText: text,
|
|
@@ -343,7 +341,7 @@ Text extracted successfully: ${text.length} characters]`,
|
|
|
343
341
|
fileSize: document.file_size
|
|
344
342
|
};
|
|
345
343
|
} catch (error) {
|
|
346
|
-
logger2.error({ error }, "Error processing text document");
|
|
344
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: document.file_name, error: error instanceof Error ? error.message : String(error) }, "Error processing text document");
|
|
347
345
|
return {
|
|
348
346
|
title: `Text Document: ${document.file_name || "Unknown Document"}`,
|
|
349
347
|
fullText: "",
|
|
@@ -400,9 +398,9 @@ ${fullText}
|
|
|
400
398
|
description: documentInfo.formattedDescription,
|
|
401
399
|
text: fullText
|
|
402
400
|
});
|
|
403
|
-
logger2.
|
|
401
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: documentInfo.fileName }, "Document processed successfully");
|
|
404
402
|
} catch (error) {
|
|
405
|
-
logger2.error({ error },
|
|
403
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, fileName: documentInfo.fileName, error: error instanceof Error ? error.message : String(error) }, "Error processing document");
|
|
406
404
|
attachments.push({
|
|
407
405
|
id: document.file_id,
|
|
408
406
|
url: "",
|
|
@@ -442,9 +440,7 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
442
440
|
});
|
|
443
441
|
}
|
|
444
442
|
}
|
|
445
|
-
logger2.
|
|
446
|
-
`Message processed - Content: ${processedContent ? "yes" : "no"}, Attachments: ${attachments.length}`
|
|
447
|
-
);
|
|
443
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, hasContent: !!processedContent, attachmentsCount: attachments.length }, "Message processed");
|
|
448
444
|
return { processedContent, attachments };
|
|
449
445
|
}
|
|
450
446
|
/**
|
|
@@ -485,14 +481,14 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
485
481
|
const sentMessages = [];
|
|
486
482
|
const telegramButtons = convertToTelegramButtons(content.buttons ?? []);
|
|
487
483
|
if (!ctx.chat) {
|
|
488
|
-
logger2.error("sendMessageInChunks: ctx.chat is undefined");
|
|
484
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId }, "sendMessageInChunks: ctx.chat is undefined");
|
|
489
485
|
return [];
|
|
490
486
|
}
|
|
491
487
|
await ctx.telegram.sendChatAction(ctx.chat.id, "typing");
|
|
492
488
|
for (let i = 0; i < chunks.length; i++) {
|
|
493
489
|
const chunk = convertMarkdownToTelegram(chunks[i]);
|
|
494
490
|
if (!ctx.chat) {
|
|
495
|
-
logger2.error("sendMessageInChunks loop: ctx.chat is undefined");
|
|
491
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId }, "sendMessageInChunks loop: ctx.chat is undefined");
|
|
496
492
|
continue;
|
|
497
493
|
}
|
|
498
494
|
const sentMessage = await ctx.telegram.sendMessage(ctx.chat.id, chunk, {
|
|
@@ -548,15 +544,9 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
548
544
|
fileStream.destroy();
|
|
549
545
|
}
|
|
550
546
|
}
|
|
551
|
-
logger2.
|
|
552
|
-
`${type.charAt(0).toUpperCase() + type.slice(1)} sent successfully: ${mediaPath}`
|
|
553
|
-
);
|
|
547
|
+
logger2.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, mediaType: type, mediaPath }, "Media sent successfully");
|
|
554
548
|
} catch (error) {
|
|
555
|
-
|
|
556
|
-
logger2.error(
|
|
557
|
-
{ originalError: error },
|
|
558
|
-
`Failed to send ${type}. Path: ${mediaPath}. Error: ${errorMessage}`
|
|
559
|
-
);
|
|
549
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, mediaType: type, mediaPath, error: error instanceof Error ? error.message : String(error) }, "Failed to send media");
|
|
560
550
|
throw error;
|
|
561
551
|
}
|
|
562
552
|
}
|
|
@@ -594,7 +584,7 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
594
584
|
const entityId = createUniqueUuid(this.runtime, ctx.from.id.toString());
|
|
595
585
|
const threadId = "is_topic_message" in message && message.is_topic_message ? message.message_thread_id?.toString() : void 0;
|
|
596
586
|
if (!ctx.chat) {
|
|
597
|
-
logger2.error("handleMessage: ctx.chat is undefined");
|
|
587
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId }, "handleMessage: ctx.chat is undefined");
|
|
598
588
|
return;
|
|
599
589
|
}
|
|
600
590
|
const telegramRoomid = threadId ? `${ctx.chat.id}-${threadId}` : ctx.chat.id.toString();
|
|
@@ -621,7 +611,6 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
621
611
|
name: ctx.from.first_name,
|
|
622
612
|
source: "telegram",
|
|
623
613
|
channelId: telegramRoomid,
|
|
624
|
-
serverId: void 0,
|
|
625
614
|
type: channelType,
|
|
626
615
|
worldId: createUniqueUuid(this.runtime, roomId),
|
|
627
616
|
worldName: telegramRoomid
|
|
@@ -690,27 +679,26 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
690
679
|
}
|
|
691
680
|
return memories;
|
|
692
681
|
} catch (error) {
|
|
693
|
-
logger2.error({ error }, "Error in message callback");
|
|
682
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error in message callback");
|
|
694
683
|
return [];
|
|
695
684
|
}
|
|
696
685
|
};
|
|
697
|
-
if (
|
|
698
|
-
|
|
686
|
+
if (this.runtime.hasElizaOS()) {
|
|
687
|
+
await this.runtime.elizaOS.handleMessage(this.runtime.agentId, memory, {
|
|
688
|
+
onResponse: async (content) => {
|
|
689
|
+
await callback(content);
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
} else if (this.runtime.messageService) {
|
|
693
|
+
await this.runtime.messageService.handleMessage(this.runtime, memory, callback);
|
|
694
|
+
} else {
|
|
695
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId }, "Message service is not available");
|
|
699
696
|
throw new Error(
|
|
700
697
|
"Message service is not initialized. Ensure the message service is properly configured."
|
|
701
698
|
);
|
|
702
699
|
}
|
|
703
|
-
await this.runtime.messageService.handleMessage(this.runtime, memory, callback);
|
|
704
700
|
} catch (error) {
|
|
705
|
-
logger2.error(
|
|
706
|
-
{
|
|
707
|
-
error,
|
|
708
|
-
chatId: ctx.chat?.id,
|
|
709
|
-
messageId: ctx.message?.message_id,
|
|
710
|
-
from: ctx.from?.username || ctx.from?.id
|
|
711
|
-
},
|
|
712
|
-
"Error handling Telegram message"
|
|
713
|
-
);
|
|
701
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId: ctx.chat?.id, messageId: ctx.message?.message_id, from: ctx.from?.username || ctx.from?.id, error: error instanceof Error ? error.message : String(error) }, "Error handling Telegram message");
|
|
714
702
|
throw error;
|
|
715
703
|
}
|
|
716
704
|
}
|
|
@@ -768,7 +756,7 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
768
756
|
};
|
|
769
757
|
return [responseMemory];
|
|
770
758
|
} catch (error) {
|
|
771
|
-
logger2.error({ error }, "Error in reaction callback");
|
|
759
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error in reaction callback");
|
|
772
760
|
return [];
|
|
773
761
|
}
|
|
774
762
|
};
|
|
@@ -795,14 +783,7 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
795
783
|
originalReaction: reaction.new_reaction[0]
|
|
796
784
|
});
|
|
797
785
|
} catch (error) {
|
|
798
|
-
|
|
799
|
-
logger2.error(
|
|
800
|
-
{
|
|
801
|
-
error: errorMessage,
|
|
802
|
-
originalError: error
|
|
803
|
-
},
|
|
804
|
-
"Error handling reaction"
|
|
805
|
-
);
|
|
786
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error handling reaction");
|
|
806
787
|
}
|
|
807
788
|
}
|
|
808
789
|
/**
|
|
@@ -847,28 +828,26 @@ Type: ${document.mime_type || "unknown"}`
|
|
|
847
828
|
await this.runtime.createMemory(memory, "messages");
|
|
848
829
|
memories.push(memory);
|
|
849
830
|
}
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
originalMessages: sentMessages,
|
|
860
|
-
chatId
|
|
861
|
-
});
|
|
862
|
-
return sentMessages;
|
|
863
|
-
} catch (error) {
|
|
864
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
865
|
-
logger2.error(
|
|
831
|
+
if (memories.length > 0) {
|
|
832
|
+
this.runtime.emitEvent(EventType.MESSAGE_SENT, {
|
|
833
|
+
runtime: this.runtime,
|
|
834
|
+
message: memories[0],
|
|
835
|
+
source: "telegram"
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
this.runtime.emitEvent(
|
|
839
|
+
"TELEGRAM_MESSAGE_SENT" /* MESSAGE_SENT */,
|
|
866
840
|
{
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
841
|
+
runtime: this.runtime,
|
|
842
|
+
source: "telegram",
|
|
843
|
+
originalMessages: sentMessages,
|
|
844
|
+
chatId,
|
|
845
|
+
message: memories[0]
|
|
846
|
+
}
|
|
871
847
|
);
|
|
848
|
+
return sentMessages;
|
|
849
|
+
} catch (error) {
|
|
850
|
+
logger2.error({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId, error: error instanceof Error ? error.message : String(error) }, "Error sending message to Telegram");
|
|
872
851
|
return [];
|
|
873
852
|
}
|
|
874
853
|
}
|
|
@@ -889,10 +868,10 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
889
868
|
*/
|
|
890
869
|
constructor(runtime) {
|
|
891
870
|
super(runtime);
|
|
892
|
-
logger3.
|
|
871
|
+
logger3.debug({ src: "plugin:telegram", agentId: runtime.agentId }, "Constructing TelegramService");
|
|
893
872
|
const botToken = runtime.getSetting("TELEGRAM_BOT_TOKEN");
|
|
894
873
|
if (!botToken || botToken.trim() === "") {
|
|
895
|
-
logger3.warn("
|
|
874
|
+
logger3.warn({ src: "plugin:telegram", agentId: runtime.agentId }, "Bot token not provided, Telegram functionality unavailable");
|
|
896
875
|
this.bot = null;
|
|
897
876
|
this.messageManager = null;
|
|
898
877
|
return;
|
|
@@ -905,11 +884,9 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
905
884
|
try {
|
|
906
885
|
this.bot = new Telegraf(botToken, this.options);
|
|
907
886
|
this.messageManager = new MessageManager(this.bot, this.runtime);
|
|
908
|
-
logger3.
|
|
887
|
+
logger3.debug({ src: "plugin:telegram", agentId: runtime.agentId }, "TelegramService constructor completed");
|
|
909
888
|
} catch (error) {
|
|
910
|
-
logger3.error(
|
|
911
|
-
`Error initializing Telegram bot: ${error instanceof Error ? error.message : String(error)}`
|
|
912
|
-
);
|
|
889
|
+
logger3.error({ src: "plugin:telegram", agentId: runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Failed to initialize Telegram bot");
|
|
913
890
|
this.bot = null;
|
|
914
891
|
this.messageManager = null;
|
|
915
892
|
}
|
|
@@ -923,7 +900,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
923
900
|
static async start(runtime) {
|
|
924
901
|
const service = new _TelegramService(runtime);
|
|
925
902
|
if (!service.bot) {
|
|
926
|
-
logger3.warn("
|
|
903
|
+
logger3.warn({ src: "plugin:telegram", agentId: runtime.agentId }, "Service started without bot functionality");
|
|
927
904
|
return service;
|
|
928
905
|
}
|
|
929
906
|
const maxRetries = 5;
|
|
@@ -931,31 +908,25 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
931
908
|
let lastError = null;
|
|
932
909
|
while (retryCount < maxRetries) {
|
|
933
910
|
try {
|
|
934
|
-
logger3.
|
|
935
|
-
`\u2705 Telegram client successfully started for character ${runtime.character.name}`
|
|
936
|
-
);
|
|
937
|
-
logger3.log("\u{1F680} Starting Telegram bot...");
|
|
911
|
+
logger3.info({ src: "plugin:telegram", agentId: runtime.agentId, agentName: runtime.character.name }, "Starting Telegram bot");
|
|
938
912
|
await service.initializeBot();
|
|
939
913
|
service.setupMiddlewares();
|
|
940
914
|
service.setupMessageHandlers();
|
|
941
915
|
await service.bot.telegram.getMe();
|
|
916
|
+
logger3.success({ src: "plugin:telegram", agentId: runtime.agentId, agentName: runtime.character.name }, "Telegram bot started successfully");
|
|
942
917
|
return service;
|
|
943
918
|
} catch (error) {
|
|
944
919
|
lastError = error instanceof Error ? error : new Error(String(error));
|
|
945
|
-
logger3.error(
|
|
946
|
-
`Telegram initialization attempt ${retryCount + 1} failed: ${lastError.message}`
|
|
947
|
-
);
|
|
920
|
+
logger3.error({ src: "plugin:telegram", agentId: runtime.agentId, attempt: retryCount + 1, error: lastError.message }, "Initialization attempt failed");
|
|
948
921
|
retryCount++;
|
|
949
922
|
if (retryCount < maxRetries) {
|
|
950
923
|
const delay = 2 ** retryCount * 1e3;
|
|
951
|
-
logger3.info(
|
|
924
|
+
logger3.info({ src: "plugin:telegram", agentId: runtime.agentId, delaySeconds: delay / 1e3 }, "Retrying initialization");
|
|
952
925
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
953
926
|
}
|
|
954
927
|
}
|
|
955
928
|
}
|
|
956
|
-
logger3.error(
|
|
957
|
-
`Telegram initialization failed after ${maxRetries} attempts. Last error: ${lastError?.message}. Service will continue without Telegram functionality.`
|
|
958
|
-
);
|
|
929
|
+
logger3.error({ src: "plugin:telegram", agentId: runtime.agentId, maxRetries, error: lastError?.message }, "Initialization failed after all attempts");
|
|
959
930
|
return service;
|
|
960
931
|
}
|
|
961
932
|
/**
|
|
@@ -982,17 +953,21 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
982
953
|
*/
|
|
983
954
|
async initializeBot() {
|
|
984
955
|
this.bot?.start((ctx) => {
|
|
985
|
-
this.runtime.emitEvent(
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
956
|
+
this.runtime.emitEvent(
|
|
957
|
+
"TELEGRAM_SLASH_START" /* SLASH_START */,
|
|
958
|
+
{
|
|
959
|
+
ctx,
|
|
960
|
+
runtime: this.runtime,
|
|
961
|
+
source: "telegram"
|
|
962
|
+
}
|
|
963
|
+
);
|
|
989
964
|
});
|
|
990
965
|
this.bot?.launch({
|
|
991
966
|
dropPendingUpdates: true,
|
|
992
967
|
allowedUpdates: ["message", "message_reaction"]
|
|
993
968
|
});
|
|
994
969
|
const botInfo = await this.bot.telegram.getMe();
|
|
995
|
-
logger3.
|
|
970
|
+
logger3.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, botId: botInfo.id, botUsername: botInfo.username }, "Bot info retrieved");
|
|
996
971
|
process.once("SIGINT", () => this.bot?.stop("SIGINT"));
|
|
997
972
|
process.once("SIGTERM", () => this.bot?.stop("SIGTERM"));
|
|
998
973
|
}
|
|
@@ -1026,7 +1001,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1026
1001
|
*/
|
|
1027
1002
|
async authorizationMiddleware(ctx, next) {
|
|
1028
1003
|
if (!await this.isGroupAuthorized(ctx)) {
|
|
1029
|
-
logger3.debug("Chat not authorized, skipping
|
|
1004
|
+
logger3.debug({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId: ctx.chat?.id }, "Chat not authorized, skipping");
|
|
1030
1005
|
return;
|
|
1031
1006
|
}
|
|
1032
1007
|
await next();
|
|
@@ -1066,7 +1041,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1066
1041
|
try {
|
|
1067
1042
|
await this.handleForumTopic(ctx);
|
|
1068
1043
|
} catch (error) {
|
|
1069
|
-
logger3.error({ error },
|
|
1044
|
+
logger3.error({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId: chat.id, error: error instanceof Error ? error.message : String(error) }, "Error handling forum topic");
|
|
1070
1045
|
}
|
|
1071
1046
|
}
|
|
1072
1047
|
if (ctx.from && ctx.chat.type !== "private") {
|
|
@@ -1084,14 +1059,14 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1084
1059
|
try {
|
|
1085
1060
|
await this.messageManager.handleMessage(ctx);
|
|
1086
1061
|
} catch (error) {
|
|
1087
|
-
logger3.error({ error }, "Error handling message");
|
|
1062
|
+
logger3.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error handling message");
|
|
1088
1063
|
}
|
|
1089
1064
|
});
|
|
1090
1065
|
this.bot?.on("message_reaction", async (ctx) => {
|
|
1091
1066
|
try {
|
|
1092
1067
|
await this.messageManager.handleReaction(ctx);
|
|
1093
1068
|
} catch (error) {
|
|
1094
|
-
logger3.error({ error }, "Error handling reaction");
|
|
1069
|
+
logger3.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error handling reaction");
|
|
1095
1070
|
}
|
|
1096
1071
|
});
|
|
1097
1072
|
}
|
|
@@ -1111,7 +1086,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1111
1086
|
const allowedChatsList = JSON.parse(allowedChats);
|
|
1112
1087
|
return allowedChatsList.includes(chatId);
|
|
1113
1088
|
} catch (error) {
|
|
1114
|
-
logger3.error({ error }, "Error parsing TELEGRAM_ALLOWED_CHATS");
|
|
1089
|
+
logger3.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error parsing TELEGRAM_ALLOWED_CHATS");
|
|
1115
1090
|
return false;
|
|
1116
1091
|
}
|
|
1117
1092
|
}
|
|
@@ -1162,7 +1137,6 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1162
1137
|
name: ctx.from.first_name || ctx.from.username || "Unknown User",
|
|
1163
1138
|
source: "telegram",
|
|
1164
1139
|
channelId: chatId,
|
|
1165
|
-
serverId: chatId,
|
|
1166
1140
|
type: ChannelType2.GROUP,
|
|
1167
1141
|
worldId
|
|
1168
1142
|
});
|
|
@@ -1194,18 +1168,24 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1194
1168
|
name: newMember.first_name || newMember.username || "Unknown User",
|
|
1195
1169
|
source: "telegram",
|
|
1196
1170
|
channelId: chatId,
|
|
1197
|
-
serverId: chatId,
|
|
1198
1171
|
type: ChannelType2.GROUP,
|
|
1199
1172
|
worldId
|
|
1200
1173
|
});
|
|
1201
1174
|
this.syncedEntityIds.add(entityId);
|
|
1202
|
-
this.runtime.emitEvent(
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1175
|
+
this.runtime.emitEvent(
|
|
1176
|
+
"TELEGRAM_ENTITY_JOINED" /* ENTITY_JOINED */,
|
|
1177
|
+
{
|
|
1178
|
+
runtime: this.runtime,
|
|
1179
|
+
entityId,
|
|
1180
|
+
worldId,
|
|
1181
|
+
source: "telegram",
|
|
1182
|
+
telegramUser: {
|
|
1183
|
+
id: newMember.id,
|
|
1184
|
+
username: newMember.username,
|
|
1185
|
+
first_name: newMember.first_name
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
);
|
|
1209
1189
|
}
|
|
1210
1190
|
}
|
|
1211
1191
|
/**
|
|
@@ -1300,9 +1280,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1300
1280
|
);
|
|
1301
1281
|
owner = foundOwner || null;
|
|
1302
1282
|
} catch (error) {
|
|
1303
|
-
logger3.warn(
|
|
1304
|
-
`Could not get chat administrators: ${error instanceof Error ? error.message : String(error)}`
|
|
1305
|
-
);
|
|
1283
|
+
logger3.warn({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId, error: error instanceof Error ? error.message : String(error) }, "Could not get chat administrators");
|
|
1306
1284
|
}
|
|
1307
1285
|
}
|
|
1308
1286
|
let ownerId = userId;
|
|
@@ -1355,7 +1333,6 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1355
1333
|
entities,
|
|
1356
1334
|
generalRoom.id,
|
|
1357
1335
|
generalRoom.channelId,
|
|
1358
|
-
generalRoom.serverId,
|
|
1359
1336
|
generalRoom.type,
|
|
1360
1337
|
worldId
|
|
1361
1338
|
);
|
|
@@ -1385,13 +1362,12 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1385
1362
|
* @param {Entity[]} entities - The entities to process
|
|
1386
1363
|
* @param {UUID} roomId - The ID of the room to connect entities to
|
|
1387
1364
|
* @param {string} channelId - The channel ID
|
|
1388
|
-
* @param {string} serverId - The server ID
|
|
1389
1365
|
* @param {ChannelType} roomType - The type of the room
|
|
1390
1366
|
* @param {UUID} worldId - The ID of the world
|
|
1391
1367
|
* @returns {Promise<void>}
|
|
1392
1368
|
* @private
|
|
1393
1369
|
*/
|
|
1394
|
-
async batchProcessEntities(entities, roomId, channelId,
|
|
1370
|
+
async batchProcessEntities(entities, roomId, channelId, roomType, worldId) {
|
|
1395
1371
|
const batchSize = 50;
|
|
1396
1372
|
for (let i = 0; i < entities.length; i += batchSize) {
|
|
1397
1373
|
const entityBatch = entities.slice(i, i + batchSize);
|
|
@@ -1408,18 +1384,15 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1408
1384
|
userId: telegramMetadata?.id,
|
|
1409
1385
|
source: "telegram",
|
|
1410
1386
|
channelId,
|
|
1411
|
-
serverId,
|
|
1412
1387
|
type: roomType,
|
|
1413
1388
|
worldId
|
|
1414
1389
|
});
|
|
1415
1390
|
} else {
|
|
1416
|
-
logger3.warn(
|
|
1417
|
-
`Skipping entity sync due to missing ID: ${JSON.stringify(entity.names)}`
|
|
1418
|
-
);
|
|
1391
|
+
logger3.warn({ src: "plugin:telegram", agentId: this.runtime.agentId, entityNames: entity.names }, "Skipping entity sync due to missing ID");
|
|
1419
1392
|
}
|
|
1420
1393
|
} catch (err) {
|
|
1421
1394
|
const telegramMetadata = entity.metadata?.telegram;
|
|
1422
|
-
logger3.warn(
|
|
1395
|
+
logger3.warn({ src: "plugin:telegram", agentId: this.runtime.agentId, username: telegramMetadata?.username, error: err instanceof Error ? err.message : String(err) }, "Failed to sync user");
|
|
1423
1396
|
}
|
|
1424
1397
|
})
|
|
1425
1398
|
);
|
|
@@ -1515,13 +1488,11 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1515
1488
|
}
|
|
1516
1489
|
}
|
|
1517
1490
|
} catch (error) {
|
|
1518
|
-
logger3.warn(
|
|
1491
|
+
logger3.warn({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId: chat.id, error: error instanceof Error ? error.message : String(error) }, "Could not fetch administrators");
|
|
1519
1492
|
}
|
|
1520
1493
|
}
|
|
1521
1494
|
} catch (error) {
|
|
1522
|
-
logger3.error(
|
|
1523
|
-
`Error building standardized entities: ${error instanceof Error ? error.message : String(error)}`
|
|
1524
|
-
);
|
|
1495
|
+
logger3.error({ src: "plugin:telegram", agentId: this.runtime.agentId, error: error instanceof Error ? error.message : String(error) }, "Error building standardized entities");
|
|
1525
1496
|
}
|
|
1526
1497
|
return entities;
|
|
1527
1498
|
}
|
|
@@ -1571,9 +1542,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1571
1542
|
};
|
|
1572
1543
|
return room;
|
|
1573
1544
|
} catch (error) {
|
|
1574
|
-
logger3.error(
|
|
1575
|
-
`Error building forum topic room: ${error instanceof Error ? error.message : String(error)}`
|
|
1576
|
-
);
|
|
1545
|
+
logger3.error({ src: "plugin:telegram", agentId: this.runtime.agentId, chatId, threadId, error: error instanceof Error ? error.message : String(error) }, "Error building forum topic room");
|
|
1577
1546
|
return null;
|
|
1578
1547
|
}
|
|
1579
1548
|
}
|
|
@@ -1583,14 +1552,14 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1583
1552
|
"telegram",
|
|
1584
1553
|
serviceInstance.handleSendMessage.bind(serviceInstance)
|
|
1585
1554
|
);
|
|
1586
|
-
logger3.info("
|
|
1555
|
+
logger3.info({ src: "plugin:telegram", agentId: runtime.agentId }, "Registered send handler");
|
|
1587
1556
|
} else {
|
|
1588
|
-
logger3.warn("
|
|
1557
|
+
logger3.warn({ src: "plugin:telegram", agentId: runtime.agentId }, "Cannot register send handler, bot not initialized");
|
|
1589
1558
|
}
|
|
1590
1559
|
}
|
|
1591
1560
|
async handleSendMessage(runtime, target, content) {
|
|
1592
1561
|
if (!this.bot || !this.messageManager) {
|
|
1593
|
-
logger3.error("
|
|
1562
|
+
logger3.error({ src: "plugin:telegram", agentId: runtime.agentId }, "Bot not initialized, cannot send messages");
|
|
1594
1563
|
throw new Error("Telegram bot is not initialized. Please provide TELEGRAM_BOT_TOKEN.");
|
|
1595
1564
|
}
|
|
1596
1565
|
let chatId;
|
|
@@ -1602,7 +1571,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1602
1571
|
if (!chatId)
|
|
1603
1572
|
throw new Error(`Could not resolve Telegram chat ID from roomId ${target.roomId}`);
|
|
1604
1573
|
} else if (target.entityId) {
|
|
1605
|
-
logger3.error("
|
|
1574
|
+
logger3.error({ src: "plugin:telegram", agentId: runtime.agentId, entityId: target.entityId }, "Sending DMs via entityId not implemented");
|
|
1606
1575
|
throw new Error("Sending DMs via entityId is not yet supported for Telegram.");
|
|
1607
1576
|
} else {
|
|
1608
1577
|
throw new Error("Telegram SendHandler requires channelId, roomId, or entityId.");
|
|
@@ -1614,15 +1583,9 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
1614
1583
|
}
|
|
1615
1584
|
try {
|
|
1616
1585
|
await this.messageManager.sendMessage(chatId, content);
|
|
1617
|
-
logger3.info(
|
|
1586
|
+
logger3.info({ src: "plugin:telegram", agentId: runtime.agentId, chatId }, "Message sent");
|
|
1618
1587
|
} catch (error) {
|
|
1619
|
-
logger3.error(
|
|
1620
|
-
{
|
|
1621
|
-
target,
|
|
1622
|
-
content
|
|
1623
|
-
},
|
|
1624
|
-
`[Telegram SendHandler] Error sending message: ${error instanceof Error ? error.message : String(error)}`
|
|
1625
|
-
);
|
|
1588
|
+
logger3.error({ src: "plugin:telegram", agentId: runtime.agentId, chatId, error: error instanceof Error ? error.message : String(error) }, "Error sending message");
|
|
1626
1589
|
throw error;
|
|
1627
1590
|
}
|
|
1628
1591
|
}
|
|
@@ -1684,7 +1647,7 @@ var TelegramTestSuite = class {
|
|
|
1684
1647
|
*/
|
|
1685
1648
|
validateChatId(runtime) {
|
|
1686
1649
|
const testChatId = runtime.getSetting("TELEGRAM_TEST_CHAT_ID") || process.env.TELEGRAM_TEST_CHAT_ID;
|
|
1687
|
-
if (!testChatId) {
|
|
1650
|
+
if (!testChatId || typeof testChatId === "boolean") {
|
|
1688
1651
|
throw new Error(
|
|
1689
1652
|
"TELEGRAM_TEST_CHAT_ID is not set. Please provide a valid chat ID in the environment variables."
|
|
1690
1653
|
);
|
|
@@ -1698,7 +1661,7 @@ var TelegramTestSuite = class {
|
|
|
1698
1661
|
throw new Error("Bot is not initialized.");
|
|
1699
1662
|
}
|
|
1700
1663
|
const chat = await this.bot.telegram.getChat(chatId);
|
|
1701
|
-
logger4.
|
|
1664
|
+
logger4.debug({ src: "plugin:telegram", chatId }, "Fetched real chat");
|
|
1702
1665
|
return chat;
|
|
1703
1666
|
} catch (error) {
|
|
1704
1667
|
throw new Error(`Error fetching real Telegram chat: ${error}`);
|
|
@@ -1713,14 +1676,14 @@ var TelegramTestSuite = class {
|
|
|
1713
1676
|
}
|
|
1714
1677
|
this.bot = this.telegramClient.messageManager.bot;
|
|
1715
1678
|
this.messageManager = this.telegramClient.messageManager;
|
|
1716
|
-
logger4.debug("Telegram bot initialized successfully
|
|
1679
|
+
logger4.debug({ src: "plugin:telegram" }, "Telegram bot initialized successfully");
|
|
1717
1680
|
}
|
|
1718
1681
|
async testSendingTextMessage(runtime) {
|
|
1719
1682
|
try {
|
|
1720
1683
|
if (!this.bot) throw new Error("Bot not initialized.");
|
|
1721
1684
|
const chatId = this.validateChatId(runtime);
|
|
1722
1685
|
await this.bot.telegram.sendMessage(chatId, "Testing Telegram message!");
|
|
1723
|
-
logger4.debug("Message sent successfully
|
|
1686
|
+
logger4.debug({ src: "plugin:telegram", chatId }, "Message sent successfully");
|
|
1724
1687
|
} catch (error) {
|
|
1725
1688
|
throw new Error(`Error sending Telegram message: ${error}`);
|
|
1726
1689
|
}
|
|
@@ -1753,7 +1716,7 @@ var TelegramTestSuite = class {
|
|
|
1753
1716
|
mockContext,
|
|
1754
1717
|
messageContent
|
|
1755
1718
|
);
|
|
1756
|
-
logger4.success("Message with image attachment sent successfully
|
|
1719
|
+
logger4.success({ src: "plugin:telegram" }, "Message with image attachment sent successfully");
|
|
1757
1720
|
} catch (error) {
|
|
1758
1721
|
throw new Error(`Error sending Telegram message with attachment: ${error}`);
|
|
1759
1722
|
}
|
|
@@ -1794,7 +1757,7 @@ var TelegramTestSuite = class {
|
|
|
1794
1757
|
if (!this.bot) throw new Error("Bot not initialized.");
|
|
1795
1758
|
if (!this.messageManager) throw new Error("MessageManager not initialized.");
|
|
1796
1759
|
const chatId = this.validateChatId(runtime);
|
|
1797
|
-
const fileId = await this.getFileId(chatId, TEST_IMAGE_URL);
|
|
1760
|
+
const fileId = await this.getFileId(String(chatId), TEST_IMAGE_URL);
|
|
1798
1761
|
const mockMessage = {
|
|
1799
1762
|
message_id: 12345,
|
|
1800
1763
|
chat: { id: chatId, type: "private" },
|
|
@@ -1814,7 +1777,7 @@ var TelegramTestSuite = class {
|
|
|
1814
1777
|
throw new Error("Error processing Telegram image or description not found");
|
|
1815
1778
|
}
|
|
1816
1779
|
const { description } = result;
|
|
1817
|
-
logger4.
|
|
1780
|
+
logger4.debug({ src: "plugin:telegram", description }, "Processing Telegram image successfully");
|
|
1818
1781
|
} catch (error) {
|
|
1819
1782
|
throw new Error(`Error processing Telegram image: ${error}`);
|
|
1820
1783
|
}
|
|
@@ -1830,7 +1793,7 @@ var TelegramTestSuite = class {
|
|
|
1830
1793
|
}
|
|
1831
1794
|
return message.photo[message.photo.length - 1].file_id;
|
|
1832
1795
|
} catch (error) {
|
|
1833
|
-
logger4.error({ error },
|
|
1796
|
+
logger4.error({ src: "plugin:telegram", chatId, error: error instanceof Error ? error.message : String(error) }, "Error sending image");
|
|
1834
1797
|
throw error;
|
|
1835
1798
|
}
|
|
1836
1799
|
}
|