@elizaos/plugin-telegram 1.0.2 → 1.0.9
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/constants.d.ts +10 -0
- package/dist/environment.d.ts +21 -0
- package/dist/index.d.ts +5 -327
- package/dist/index.js +107 -60
- package/dist/index.js.map +1 -1
- package/dist/messageManager.d.ts +87 -0
- package/dist/service.d.ts +219 -0
- package/dist/tests.d.ts +52 -0
- package/dist/types.d.ts +99 -0
- package/dist/utils.d.ts +29 -0
- package/package.json +4 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const MESSAGE_CONSTANTS: {
|
|
2
|
+
readonly MAX_MESSAGES: 50;
|
|
3
|
+
readonly RECENT_MESSAGE_COUNT: 5;
|
|
4
|
+
readonly CHAT_HISTORY_COUNT: 10;
|
|
5
|
+
readonly DEFAULT_SIMILARITY_THRESHOLD: 0.6;
|
|
6
|
+
readonly DEFAULT_SIMILARITY_THRESHOLD_FOLLOW_UPS: 0.4;
|
|
7
|
+
readonly INTEREST_DECAY_TIME: number;
|
|
8
|
+
readonly PARTIAL_INTEREST_DECAY: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const TELEGRAM_SERVICE_NAME = "telegram";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IAgentRuntime } from '@elizaos/core';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const telegramEnvSchema: z.ZodObject<{
|
|
4
|
+
TELEGRAM_BOT_TOKEN: z.ZodString;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
TELEGRAM_BOT_TOKEN: string;
|
|
7
|
+
}, {
|
|
8
|
+
TELEGRAM_BOT_TOKEN: string;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Represents the type definition for configuring a Telegram bot based on the inferred schema.
|
|
12
|
+
*/
|
|
13
|
+
export type TelegramConfig = z.infer<typeof telegramEnvSchema>;
|
|
14
|
+
/**
|
|
15
|
+
* Validates the Telegram configuration by retrieving the Telegram bot token from the runtime settings or environment variables.
|
|
16
|
+
* Returns null if validation fails instead of throwing an error.
|
|
17
|
+
*
|
|
18
|
+
* @param {IAgentRuntime} runtime - The agent runtime used to get the setting.
|
|
19
|
+
* @returns {Promise<TelegramConfig | null>} A promise that resolves with the validated Telegram configuration or null if invalid.
|
|
20
|
+
*/
|
|
21
|
+
export declare function validateTelegramConfig(runtime: IAgentRuntime): Promise<TelegramConfig | null>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,328 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Extention of the core Content type just for Telegram
|
|
7
|
-
*/
|
|
8
|
-
interface TelegramContent extends Content {
|
|
9
|
-
/** Array of buttons */
|
|
10
|
-
buttons?: Button[];
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Represents a flexible button configuration
|
|
14
|
-
*/
|
|
15
|
-
type Button = {
|
|
16
|
-
/** The type of button */
|
|
17
|
-
kind: 'login' | 'url';
|
|
18
|
-
/** The text to display on the button */
|
|
19
|
-
text: string;
|
|
20
|
-
/** The URL or endpoint the button should link to */
|
|
21
|
-
url: string;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Enum representing different types of media.
|
|
26
|
-
* @enum { string }
|
|
27
|
-
* @readonly
|
|
28
|
-
*/
|
|
29
|
-
declare enum MediaType {
|
|
30
|
-
PHOTO = "photo",
|
|
31
|
-
VIDEO = "video",
|
|
32
|
-
DOCUMENT = "document",
|
|
33
|
-
AUDIO = "audio",
|
|
34
|
-
ANIMATION = "animation"
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Class representing a message manager.
|
|
38
|
-
* @class
|
|
39
|
-
*/
|
|
40
|
-
declare class MessageManager {
|
|
41
|
-
bot: Telegraf<Context>;
|
|
42
|
-
protected runtime: IAgentRuntime;
|
|
43
|
-
/**
|
|
44
|
-
* Constructor for creating a new instance of a BotAgent.
|
|
45
|
-
*
|
|
46
|
-
* @param {Telegraf<Context>} bot - The Telegraf instance used for interacting with the bot platform.
|
|
47
|
-
* @param {IAgentRuntime} runtime - The runtime environment for the agent.
|
|
48
|
-
*/
|
|
49
|
-
constructor(bot: Telegraf<Context>, runtime: IAgentRuntime);
|
|
50
|
-
/**
|
|
51
|
-
* Process an image from a Telegram message to extract the image URL and description.
|
|
52
|
-
*
|
|
53
|
-
* @param {Message} message - The Telegram message object containing the image.
|
|
54
|
-
* @returns {Promise<{ description: string } | null>} The description of the processed image or null if no image found.
|
|
55
|
-
*/
|
|
56
|
-
processImage(message: Message): Promise<{
|
|
57
|
-
description: string;
|
|
58
|
-
} | null>;
|
|
59
|
-
/**
|
|
60
|
-
* Sends a message in chunks, handling attachments and splitting the message if necessary
|
|
61
|
-
*
|
|
62
|
-
* @param {Context} ctx - The context object representing the current state of the bot
|
|
63
|
-
* @param {TelegramContent} content - The content of the message to be sent
|
|
64
|
-
* @param {number} [replyToMessageId] - The ID of the message to reply to, if any
|
|
65
|
-
* @returns {Promise<Message.TextMessage[]>} - An array of TextMessage objects representing the messages sent
|
|
66
|
-
*/
|
|
67
|
-
sendMessageInChunks(ctx: Context, content: TelegramContent, replyToMessageId?: number): Promise<Message.TextMessage[]>;
|
|
68
|
-
/**
|
|
69
|
-
* Sends media to a chat using the Telegram API.
|
|
70
|
-
*
|
|
71
|
-
* @param {Context} ctx - The context object containing information about the current chat.
|
|
72
|
-
* @param {string} mediaPath - The path to the media to be sent, either a URL or a local file path.
|
|
73
|
-
* @param {MediaType} type - The type of media being sent (PHOTO, VIDEO, DOCUMENT, AUDIO, or ANIMATION).
|
|
74
|
-
* @param {string} [caption] - Optional caption for the media being sent.
|
|
75
|
-
*
|
|
76
|
-
* @returns {Promise<void>} A Promise that resolves when the media is successfully sent.
|
|
77
|
-
*/
|
|
78
|
-
sendMedia(ctx: Context, mediaPath: string, type: MediaType, caption?: string): Promise<void>;
|
|
79
|
-
/**
|
|
80
|
-
* Splits a given text into an array of strings based on the maximum message length.
|
|
81
|
-
*
|
|
82
|
-
* @param {string} text - The text to split into chunks.
|
|
83
|
-
* @returns {string[]} An array of strings with each element representing a chunk of the original text.
|
|
84
|
-
*/
|
|
85
|
-
private splitMessage;
|
|
86
|
-
/**
|
|
87
|
-
* Handle incoming messages from Telegram and process them accordingly.
|
|
88
|
-
* @param {Context} ctx - The context object containing information about the message.
|
|
89
|
-
* @returns {Promise<void>}
|
|
90
|
-
*/
|
|
91
|
-
handleMessage(ctx: Context): Promise<void>;
|
|
92
|
-
/**
|
|
93
|
-
* Handles the reaction event triggered by a user reacting to a message.
|
|
94
|
-
* @param {NarrowedContext<Context<Update>, Update.MessageReactionUpdate>} ctx The context of the message reaction update
|
|
95
|
-
* @returns {Promise<void>} A Promise that resolves when the reaction handling is complete
|
|
96
|
-
*/
|
|
97
|
-
handleReaction(ctx: NarrowedContext<Context<Update>, Update.MessageReactionUpdate>): Promise<void>;
|
|
98
|
-
/**
|
|
99
|
-
* Sends a message to a Telegram chat and emits appropriate events
|
|
100
|
-
* @param {number | string} chatId - The Telegram chat ID to send the message to
|
|
101
|
-
* @param {Content} content - The content to send
|
|
102
|
-
* @param {number} [replyToMessageId] - Optional message ID to reply to
|
|
103
|
-
* @returns {Promise<Message.TextMessage[]>} The sent messages
|
|
104
|
-
*/
|
|
105
|
-
sendMessage(chatId: number | string, content: Content, replyToMessageId?: number): Promise<Message.TextMessage[]>;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Class representing a Telegram service that allows the agent to send and receive messages on Telegram.
|
|
110
|
-
* This service handles all Telegram-specific functionality including:
|
|
111
|
-
* - Initializing and managing the Telegram bot
|
|
112
|
-
* - Setting up middleware for preprocessing messages
|
|
113
|
-
* - Handling message and reaction events
|
|
114
|
-
* - Synchronizing Telegram chats, users, and entities with the agent runtime
|
|
115
|
-
* - Managing forum topics as separate rooms
|
|
116
|
-
*
|
|
117
|
-
* @extends Service
|
|
118
|
-
*/
|
|
119
|
-
declare class TelegramService extends Service {
|
|
120
|
-
static serviceType: string;
|
|
121
|
-
capabilityDescription: string;
|
|
122
|
-
private bot;
|
|
123
|
-
messageManager: MessageManager;
|
|
124
|
-
private options;
|
|
125
|
-
private knownChats;
|
|
126
|
-
private syncedEntityIds;
|
|
127
|
-
/**
|
|
128
|
-
* Constructor for TelegramService class.
|
|
129
|
-
* @param {IAgentRuntime} runtime - The runtime object for the agent.
|
|
130
|
-
*/
|
|
131
|
-
constructor(runtime: IAgentRuntime);
|
|
132
|
-
/**
|
|
133
|
-
* Starts the Telegram service for the given runtime.
|
|
134
|
-
*
|
|
135
|
-
* @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for.
|
|
136
|
-
* @returns {Promise<TelegramService>} A promise that resolves with the initialized TelegramService.
|
|
137
|
-
*/
|
|
138
|
-
static start(runtime: IAgentRuntime): Promise<TelegramService>;
|
|
139
|
-
/**
|
|
140
|
-
* Stops the agent runtime.
|
|
141
|
-
* @param {IAgentRuntime} runtime - The agent runtime to stop
|
|
142
|
-
*/
|
|
143
|
-
static stop(runtime: IAgentRuntime): Promise<void>;
|
|
144
|
-
/**
|
|
145
|
-
* Asynchronously stops the bot.
|
|
146
|
-
*
|
|
147
|
-
* @returns A Promise that resolves once the bot has stopped.
|
|
148
|
-
*/
|
|
149
|
-
stop(): Promise<void>;
|
|
150
|
-
/**
|
|
151
|
-
* Initializes the Telegram bot by launching it, getting bot info, and setting up message manager.
|
|
152
|
-
* @returns {Promise<void>} A Promise that resolves when the initialization is complete.
|
|
153
|
-
*/
|
|
154
|
-
private initializeBot;
|
|
155
|
-
/**
|
|
156
|
-
* Sets up the middleware chain for preprocessing messages before they reach handlers.
|
|
157
|
-
* This critical method establishes a sequential processing pipeline that:
|
|
158
|
-
*
|
|
159
|
-
* 1. Authorization - Verifies if a chat is allowed to interact with the bot based on configured settings
|
|
160
|
-
* 2. Chat Discovery - Ensures chat entities and worlds exist in the runtime, creating them if needed
|
|
161
|
-
* 3. Forum Topics - Handles Telegram forum topics as separate rooms for better conversation management
|
|
162
|
-
* 4. Entity Synchronization - Ensures message senders are properly synchronized as entities
|
|
163
|
-
*
|
|
164
|
-
* The middleware chain runs in sequence for each message, with each step potentially
|
|
165
|
-
* enriching the context or stopping processing if conditions aren't met.
|
|
166
|
-
* This preprocessing is essential for maintaining consistent state before message handlers execute.
|
|
167
|
-
*
|
|
168
|
-
* @private
|
|
169
|
-
*/
|
|
170
|
-
private setupMiddlewares;
|
|
171
|
-
/**
|
|
172
|
-
* Authorization middleware - checks if chat is allowed to interact with the bot
|
|
173
|
-
* based on the TELEGRAM_ALLOWED_CHATS configuration.
|
|
174
|
-
*
|
|
175
|
-
* @param {Context} ctx - The context of the incoming update
|
|
176
|
-
* @param {Function} next - The function to call to proceed to the next middleware
|
|
177
|
-
* @returns {Promise<void>}
|
|
178
|
-
* @private
|
|
179
|
-
*/
|
|
180
|
-
private authorizationMiddleware;
|
|
181
|
-
/**
|
|
182
|
-
* Chat and entity management middleware - handles new chats, forum topics, and entity synchronization.
|
|
183
|
-
* This middleware implements decision logic to determine which operations are needed based on
|
|
184
|
-
* the chat type and whether we've seen this chat before.
|
|
185
|
-
*
|
|
186
|
-
* @param {Context} ctx - The context of the incoming update
|
|
187
|
-
* @param {Function} next - The function to call to proceed to the next middleware
|
|
188
|
-
* @returns {Promise<void>}
|
|
189
|
-
* @private
|
|
190
|
-
*/
|
|
191
|
-
private chatAndEntityMiddleware;
|
|
192
|
-
/**
|
|
193
|
-
* Process an existing chat based on chat type and message properties.
|
|
194
|
-
* Different chat types require different processing steps.
|
|
195
|
-
*
|
|
196
|
-
* @param {Context} ctx - The context of the incoming update
|
|
197
|
-
* @returns {Promise<void>}
|
|
198
|
-
* @private
|
|
199
|
-
*/
|
|
200
|
-
private processExistingChat;
|
|
201
|
-
/**
|
|
202
|
-
* Sets up message and reaction handlers for the bot.
|
|
203
|
-
* Configures event handlers to process incoming messages and reactions.
|
|
204
|
-
*
|
|
205
|
-
* @private
|
|
206
|
-
*/
|
|
207
|
-
private setupMessageHandlers;
|
|
208
|
-
/**
|
|
209
|
-
* Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting.
|
|
210
|
-
* @param {Context} ctx - The context of the incoming update.
|
|
211
|
-
* @returns {Promise<boolean>} A Promise that resolves with a boolean indicating if the group is authorized.
|
|
212
|
-
*/
|
|
213
|
-
private isGroupAuthorized;
|
|
214
|
-
/**
|
|
215
|
-
* Synchronizes an entity from a message context with the runtime system.
|
|
216
|
-
* This method handles three cases:
|
|
217
|
-
* 1. Message sender - most common case
|
|
218
|
-
* 2. New chat member - when a user joins the chat
|
|
219
|
-
* 3. Left chat member - when a user leaves the chat
|
|
220
|
-
*
|
|
221
|
-
* @param {Context} ctx - The context of the incoming update
|
|
222
|
-
* @returns {Promise<void>}
|
|
223
|
-
* @private
|
|
224
|
-
*/
|
|
225
|
-
private syncEntity;
|
|
226
|
-
/**
|
|
227
|
-
* Synchronizes the message sender entity with the runtime system.
|
|
228
|
-
* This is the most common entity sync case.
|
|
229
|
-
*
|
|
230
|
-
* @param {Context} ctx - The context of the incoming update
|
|
231
|
-
* @param {UUID} worldId - The ID of the world
|
|
232
|
-
* @param {UUID} roomId - The ID of the room
|
|
233
|
-
* @param {string} chatId - The ID of the chat
|
|
234
|
-
* @returns {Promise<void>}
|
|
235
|
-
* @private
|
|
236
|
-
*/
|
|
237
|
-
private syncMessageSender;
|
|
238
|
-
/**
|
|
239
|
-
* Synchronizes a new chat member entity with the runtime system.
|
|
240
|
-
* Triggered when a user joins the chat.
|
|
241
|
-
*
|
|
242
|
-
* @param {Context} ctx - The context of the incoming update
|
|
243
|
-
* @param {UUID} worldId - The ID of the world
|
|
244
|
-
* @param {UUID} roomId - The ID of the room
|
|
245
|
-
* @param {string} chatId - The ID of the chat
|
|
246
|
-
* @returns {Promise<void>}
|
|
247
|
-
* @private
|
|
248
|
-
*/
|
|
249
|
-
private syncNewChatMember;
|
|
250
|
-
/**
|
|
251
|
-
* Updates entity status when a user leaves the chat.
|
|
252
|
-
*
|
|
253
|
-
* @param {Context} ctx - The context of the incoming update
|
|
254
|
-
* @returns {Promise<void>}
|
|
255
|
-
* @private
|
|
256
|
-
*/
|
|
257
|
-
private syncLeftChatMember;
|
|
258
|
-
/**
|
|
259
|
-
* Handles forum topics by creating appropriate rooms in the runtime system.
|
|
260
|
-
* This enables proper conversation management for Telegram's forum feature.
|
|
261
|
-
*
|
|
262
|
-
* @param {Context} ctx - The context of the incoming update
|
|
263
|
-
* @returns {Promise<void>}
|
|
264
|
-
* @private
|
|
265
|
-
*/
|
|
266
|
-
private handleForumTopic;
|
|
267
|
-
/**
|
|
268
|
-
* Builds entity for message sender
|
|
269
|
-
*/
|
|
270
|
-
private buildMsgSenderEntity;
|
|
271
|
-
/**
|
|
272
|
-
* Handles new chat discovery and emits WORLD_JOINED event.
|
|
273
|
-
* This is a critical function that ensures new chats are properly
|
|
274
|
-
* registered in the runtime system and appropriate events are emitted.
|
|
275
|
-
*
|
|
276
|
-
* @param {Context} ctx - The context of the incoming update
|
|
277
|
-
* @returns {Promise<void>}
|
|
278
|
-
* @private
|
|
279
|
-
*/
|
|
280
|
-
private handleNewChat;
|
|
281
|
-
/**
|
|
282
|
-
* Processes entities in batches to prevent overwhelming the system.
|
|
283
|
-
*
|
|
284
|
-
* @param {Entity[]} entities - The entities to process
|
|
285
|
-
* @param {UUID} roomId - The ID of the room to connect entities to
|
|
286
|
-
* @param {string} channelId - The channel ID
|
|
287
|
-
* @param {string} serverId - The server ID
|
|
288
|
-
* @param {ChannelType} roomType - The type of the room
|
|
289
|
-
* @param {UUID} worldId - The ID of the world
|
|
290
|
-
* @returns {Promise<void>}
|
|
291
|
-
* @private
|
|
292
|
-
*/
|
|
293
|
-
private batchProcessEntities;
|
|
294
|
-
/**
|
|
295
|
-
* Gets chat title and channel type based on Telegram chat type.
|
|
296
|
-
* Maps Telegram-specific chat types to standardized system types.
|
|
297
|
-
*
|
|
298
|
-
* @param {any} chat - The Telegram chat object
|
|
299
|
-
* @returns {Object} Object containing chatTitle and channelType
|
|
300
|
-
* @private
|
|
301
|
-
*/
|
|
302
|
-
private getChatTypeInfo;
|
|
303
|
-
/**
|
|
304
|
-
* Builds standardized entity representations from Telegram chat data.
|
|
305
|
-
* Transforms Telegram-specific user data into system-standard Entity objects.
|
|
306
|
-
*
|
|
307
|
-
* @param {any} chat - The Telegram chat object
|
|
308
|
-
* @returns {Promise<Entity[]>} Array of standardized Entity objects
|
|
309
|
-
* @private
|
|
310
|
-
*/
|
|
311
|
-
private buildStandardizedEntities;
|
|
312
|
-
/**
|
|
313
|
-
* Extracts and builds the room object for a forum topic from a message context.
|
|
314
|
-
* This refactored method can be used both in middleware and when handling new chats.
|
|
315
|
-
*
|
|
316
|
-
* @param {Context} ctx - The context of the incoming update
|
|
317
|
-
* @param {UUID} worldId - The ID of the world the topic belongs to
|
|
318
|
-
* @returns {Promise<Room | null>} A Promise that resolves with the room or null if not a topic
|
|
319
|
-
* @private
|
|
320
|
-
*/
|
|
321
|
-
private buildForumTopicRoom;
|
|
322
|
-
static registerSendHandlers(runtime: IAgentRuntime, serviceInstance: TelegramService): void;
|
|
323
|
-
handleSendMessage(runtime: IAgentRuntime, target: TargetInfo, content: Content): Promise<void>;
|
|
324
|
-
}
|
|
325
|
-
|
|
1
|
+
import type { Plugin } from '@elizaos/core';
|
|
2
|
+
import { TelegramService } from './service';
|
|
3
|
+
import { MessageManager } from './messageManager';
|
|
326
4
|
declare const telegramPlugin: Plugin;
|
|
327
|
-
|
|
328
|
-
export
|
|
5
|
+
export { TelegramService, MessageManager };
|
|
6
|
+
export default telegramPlugin;
|