@elizaos/plugin-telegram 2.0.0-alpha.9 → 2.0.0-beta.1
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/README.md +1 -1
- package/dist/account-auth-service.d.ts +100 -0
- package/dist/account-auth-service.js +25 -0
- package/dist/account-auth-service.js.map +1 -0
- package/dist/chunk-KFEFF6RI.js +642 -0
- package/dist/chunk-KFEFF6RI.js.map +1 -0
- package/dist/index.d.ts +622 -1
- package/dist/index.js +2686 -339
- package/dist/index.js.map +1 -1
- package/package.json +19 -21
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,622 @@
|
|
|
1
|
-
|
|
1
|
+
import { MessagePayload, WorldPayload, EventPayload, EntityPayload, Content, IAgentRuntime, Media, Service, MessageConnectorQueryContext, MessageConnectorTarget, TargetInfo, Memory, MessageConnectorChatContext, UUID, MessageConnectorUserContext, ConnectorAccountProvider, Plugin } from '@elizaos/core';
|
|
2
|
+
import { Message, ReactionType, Chat, Update } from '@telegraf/types';
|
|
3
|
+
import { Context, Telegraf, NarrowedContext } from 'telegraf';
|
|
4
|
+
export { TelegramAccountApiCredentials, TelegramAccountAuthAccount, TelegramAccountAuthSession, TelegramAccountAuthSessionLike, TelegramAccountAuthSnapshot, TelegramAccountAuthStatus, TelegramAccountConnectorConfig, clearTelegramAccountAuthState, clearTelegramAccountSession, defaultTelegramAccountDeviceModel, defaultTelegramAccountSystemVersion, loadTelegramAccountSessionString, resolveTelegramAccountSessionFile, saveTelegramAccountSessionString, telegramAccountAuthStateExists, telegramAccountSessionExists } from './account-auth-service.js';
|
|
5
|
+
import 'telegram';
|
|
6
|
+
import 'telegram/sessions/index.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Telegram account (user-account) auth HTTP routes.
|
|
10
|
+
*
|
|
11
|
+
* Provides a multi-step login flow for linking a personal Telegram account
|
|
12
|
+
* (as opposed to a bot token) using the `telegram` library (GramJS):
|
|
13
|
+
*
|
|
14
|
+
* GET /api/telegram-account/status current auth/connection status
|
|
15
|
+
* POST /api/telegram-account/auth/start begin login (phone + optional app creds)
|
|
16
|
+
* POST /api/telegram-account/auth/submit submit provisioning code, telegram code, or 2FA password
|
|
17
|
+
* POST /api/telegram-account/disconnect tear down session + clear saved credentials
|
|
18
|
+
*
|
|
19
|
+
* These routes are registered with `rawPath: true` so they mount at their
|
|
20
|
+
* legacy paths without the plugin-name prefix.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** Called on plugin shutdown to clean up the auth session. */
|
|
24
|
+
declare function stopTelegramAccountAuthSession(): Promise<void>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Extention of the core Content type just for Telegram
|
|
28
|
+
*/
|
|
29
|
+
interface TelegramContent extends Content {
|
|
30
|
+
/** Array of buttons */
|
|
31
|
+
buttons?: Button[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Represents a flexible button configuration
|
|
35
|
+
*/
|
|
36
|
+
type Button = {
|
|
37
|
+
/** The type of button */
|
|
38
|
+
kind: "login" | "url";
|
|
39
|
+
/** The text to display on the button */
|
|
40
|
+
text: string;
|
|
41
|
+
/** The URL or endpoint the button should link to */
|
|
42
|
+
url: string;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Telegram-specific event types
|
|
46
|
+
*/
|
|
47
|
+
declare enum TelegramEventTypes {
|
|
48
|
+
WORLD_JOINED = "TELEGRAM_WORLD_JOINED",
|
|
49
|
+
WORLD_CONNECTED = "TELEGRAM_WORLD_CONNECTED",
|
|
50
|
+
WORLD_LEFT = "TELEGRAM_WORLD_LEFT",
|
|
51
|
+
ENTITY_JOINED = "TELEGRAM_ENTITY_JOINED",
|
|
52
|
+
ENTITY_LEFT = "TELEGRAM_ENTITY_LEFT",
|
|
53
|
+
ENTITY_UPDATED = "TELEGRAM_ENTITY_UPDATED",
|
|
54
|
+
MESSAGE_RECEIVED = "TELEGRAM_MESSAGE_RECEIVED",
|
|
55
|
+
MESSAGE_SENT = "TELEGRAM_MESSAGE_SENT",
|
|
56
|
+
REACTION_RECEIVED = "TELEGRAM_REACTION_RECEIVED",
|
|
57
|
+
INTERACTION_RECEIVED = "TELEGRAM_INTERACTION_RECEIVED",
|
|
58
|
+
SLASH_START = "TELEGRAM_SLASH_START"
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Telegram-specific event payload map
|
|
62
|
+
*/
|
|
63
|
+
interface TelegramEventPayloadMap {
|
|
64
|
+
[TelegramEventTypes.MESSAGE_RECEIVED]: TelegramMessageReceivedPayload;
|
|
65
|
+
[TelegramEventTypes.MESSAGE_SENT]: TelegramMessageSentPayload;
|
|
66
|
+
[TelegramEventTypes.REACTION_RECEIVED]: TelegramReactionReceivedPayload;
|
|
67
|
+
[TelegramEventTypes.WORLD_JOINED]: TelegramWorldPayload;
|
|
68
|
+
[TelegramEventTypes.WORLD_CONNECTED]: TelegramWorldPayload;
|
|
69
|
+
[TelegramEventTypes.WORLD_LEFT]: TelegramWorldPayload;
|
|
70
|
+
[TelegramEventTypes.SLASH_START]: TelegramSlashStartPayload;
|
|
71
|
+
[TelegramEventTypes.ENTITY_JOINED]: TelegramEntityPayload;
|
|
72
|
+
[TelegramEventTypes.ENTITY_LEFT]: TelegramEntityPayload;
|
|
73
|
+
[TelegramEventTypes.ENTITY_UPDATED]: TelegramEntityPayload;
|
|
74
|
+
[TelegramEventTypes.INTERACTION_RECEIVED]: TelegramReactionReceivedPayload;
|
|
75
|
+
}
|
|
76
|
+
declare module "@elizaos/core" {
|
|
77
|
+
interface EventPayloadMap extends TelegramEventPayloadMap {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
interface TelegramSlashStartPayload extends EventPayload {
|
|
81
|
+
ctx: Context;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Telegram-specific message received payload
|
|
85
|
+
*/
|
|
86
|
+
interface TelegramMessageReceivedPayload extends MessagePayload {
|
|
87
|
+
/** The original Telegram context */
|
|
88
|
+
ctx: Context;
|
|
89
|
+
/** The original Telegram message */
|
|
90
|
+
originalMessage: Message;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Telegram-specific message sent payload
|
|
94
|
+
*/
|
|
95
|
+
interface TelegramMessageSentPayload extends MessagePayload {
|
|
96
|
+
/** The original Telegram messages */
|
|
97
|
+
originalMessages: Message[];
|
|
98
|
+
/** The chat ID where the message was sent */
|
|
99
|
+
chatId: number | string;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Telegram-specific reaction received payload
|
|
103
|
+
*/
|
|
104
|
+
interface TelegramReactionReceivedPayload extends TelegramMessageReceivedPayload {
|
|
105
|
+
/** The reaction type as a string */
|
|
106
|
+
reactionString: string;
|
|
107
|
+
/** The original reaction object */
|
|
108
|
+
originalReaction: ReactionType;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Telegram-specific world payload
|
|
112
|
+
*/
|
|
113
|
+
interface TelegramWorldPayload extends WorldPayload {
|
|
114
|
+
chat: Chat;
|
|
115
|
+
botUsername?: string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Telegram-specific entity payload
|
|
119
|
+
*/
|
|
120
|
+
interface TelegramEntityPayload extends EntityPayload {
|
|
121
|
+
telegramUser: {
|
|
122
|
+
id: number;
|
|
123
|
+
username?: string;
|
|
124
|
+
first_name?: string;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Interface for structured document processing results.
|
|
130
|
+
*/
|
|
131
|
+
interface DocumentProcessingResult {
|
|
132
|
+
title: string;
|
|
133
|
+
fullText: string;
|
|
134
|
+
formattedDescription: string;
|
|
135
|
+
fileName: string;
|
|
136
|
+
mimeType: string | undefined;
|
|
137
|
+
fileSize: number | undefined;
|
|
138
|
+
error?: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Enum representing different types of media.
|
|
142
|
+
* @enum { string }
|
|
143
|
+
* @readonly
|
|
144
|
+
*/
|
|
145
|
+
declare enum MediaType {
|
|
146
|
+
PHOTO = "photo",
|
|
147
|
+
VIDEO = "video",
|
|
148
|
+
DOCUMENT = "document",
|
|
149
|
+
AUDIO = "audio",
|
|
150
|
+
ANIMATION = "animation"
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Class representing a message manager.
|
|
154
|
+
* @class
|
|
155
|
+
*/
|
|
156
|
+
declare class MessageManager {
|
|
157
|
+
bot: Telegraf<Context>;
|
|
158
|
+
protected runtime: IAgentRuntime;
|
|
159
|
+
protected accountId: string;
|
|
160
|
+
/**
|
|
161
|
+
* Constructor for creating a new instance of a BotAgent.
|
|
162
|
+
*
|
|
163
|
+
* @param {Telegraf<Context>} bot - The Telegraf instance used for interacting with the bot platform.
|
|
164
|
+
* @param {IAgentRuntime} runtime - The runtime environment for the agent.
|
|
165
|
+
*/
|
|
166
|
+
constructor(bot: Telegraf<Context>, runtime: IAgentRuntime, accountId?: string);
|
|
167
|
+
private scopedTelegramKey;
|
|
168
|
+
/**
|
|
169
|
+
* Process an image from a Telegram message to extract the image URL and description.
|
|
170
|
+
*
|
|
171
|
+
* @param {Message} message - The Telegram message object containing the image.
|
|
172
|
+
* @returns {Promise<{ description: string } | null>} The description of the processed image or null if no image found.
|
|
173
|
+
*/
|
|
174
|
+
processImage(message: Message): Promise<{
|
|
175
|
+
description: string;
|
|
176
|
+
} | null>;
|
|
177
|
+
/**
|
|
178
|
+
* Process a document from a Telegram message to extract the document URL and description.
|
|
179
|
+
* Handles PDFs and other document types by converting them to text when possible.
|
|
180
|
+
*
|
|
181
|
+
* @param {Message} message - The Telegram message object containing the document.
|
|
182
|
+
* @returns {Promise<{ description: string } | null>} The description of the processed document or null if no document found.
|
|
183
|
+
*/
|
|
184
|
+
processDocument(message: Message): Promise<DocumentProcessingResult | null>;
|
|
185
|
+
/**
|
|
186
|
+
* Get the appropriate document processor based on MIME type.
|
|
187
|
+
*/
|
|
188
|
+
private getDocumentProcessor;
|
|
189
|
+
/**
|
|
190
|
+
* Process PDF documents by converting them to text.
|
|
191
|
+
*/
|
|
192
|
+
private processPdfDocument;
|
|
193
|
+
/**
|
|
194
|
+
* Process text documents by fetching their content.
|
|
195
|
+
*/
|
|
196
|
+
private processTextDocument;
|
|
197
|
+
/**
|
|
198
|
+
* Processes the message content, documents, and images to generate
|
|
199
|
+
* processed content and media attachments.
|
|
200
|
+
*
|
|
201
|
+
* @param {Message} message The message to process
|
|
202
|
+
* @returns {Promise<{ processedContent: string; attachments: Media[] }>} Processed content and media attachments
|
|
203
|
+
*/
|
|
204
|
+
processMessage(message: Message): Promise<{
|
|
205
|
+
processedContent: string;
|
|
206
|
+
attachments: Media[];
|
|
207
|
+
}>;
|
|
208
|
+
/**
|
|
209
|
+
* Sends a message in chunks, handling attachments and splitting the message if necessary
|
|
210
|
+
*
|
|
211
|
+
* @param {Context} ctx - The context object representing the current state of the bot
|
|
212
|
+
* @param {TelegramContent} content - The content of the message to be sent
|
|
213
|
+
* @param {number} [replyToMessageId] - The ID of the message to reply to, if any
|
|
214
|
+
* @returns {Promise<Message.TextMessage[]>} - An array of TextMessage objects representing the messages sent
|
|
215
|
+
*/
|
|
216
|
+
sendMessageInChunks(ctx: Context, content: TelegramContent, replyToMessageId?: number, messageThreadId?: number): Promise<Message.TextMessage[]>;
|
|
217
|
+
/**
|
|
218
|
+
* Sends media to a chat using the Telegram API.
|
|
219
|
+
*
|
|
220
|
+
* @param {Context} ctx - The context object containing information about the current chat.
|
|
221
|
+
* @param {string} mediaPath - The path to the media to be sent, either a URL or a local file path.
|
|
222
|
+
* @param {MediaType} type - The type of media being sent (PHOTO, VIDEO, DOCUMENT, AUDIO, or ANIMATION).
|
|
223
|
+
* @param {string} [caption] - Optional caption for the media being sent.
|
|
224
|
+
*
|
|
225
|
+
* @returns {Promise<void>} A Promise that resolves when the media is successfully sent.
|
|
226
|
+
*/
|
|
227
|
+
sendMedia(ctx: Context, mediaPath: string, type: MediaType, caption?: string): Promise<void>;
|
|
228
|
+
/**
|
|
229
|
+
* Splits a given text into an array of strings based on the maximum message length.
|
|
230
|
+
*
|
|
231
|
+
* @param {string} text - The text to split into chunks.
|
|
232
|
+
* @returns {string[]} An array of strings with each element representing a chunk of the original text.
|
|
233
|
+
*/
|
|
234
|
+
private splitMessage;
|
|
235
|
+
/**
|
|
236
|
+
* Handle incoming messages from Telegram and process them accordingly.
|
|
237
|
+
* @param {Context} ctx - The context object containing information about the message.
|
|
238
|
+
* @returns {Promise<void>}
|
|
239
|
+
*/
|
|
240
|
+
handleMessage(ctx: Context): Promise<void>;
|
|
241
|
+
/**
|
|
242
|
+
* Handles the reaction event triggered by a user reacting to a message.
|
|
243
|
+
* @param {NarrowedContext<Context<Update>, Update.MessageReactionUpdate>} ctx The context of the message reaction update
|
|
244
|
+
* @returns {Promise<void>} A Promise that resolves when the reaction handling is complete
|
|
245
|
+
*/
|
|
246
|
+
handleReaction(ctx: NarrowedContext<Context<Update>, Update.MessageReactionUpdate>): Promise<void>;
|
|
247
|
+
/**
|
|
248
|
+
* Sends a message to a Telegram chat and emits appropriate events
|
|
249
|
+
* @param {number | string} chatId - The Telegram chat ID to send the message to
|
|
250
|
+
* @param {Content} content - The content to send
|
|
251
|
+
* @param {number} [replyToMessageId] - Optional message ID to reply to
|
|
252
|
+
* @returns {Promise<Message.TextMessage[]>} The sent messages
|
|
253
|
+
*/
|
|
254
|
+
sendMessage(chatId: number | string, content: Content, replyToMessageId?: number, messageThreadId?: number): Promise<Message.TextMessage[]>;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* TelegramOwnerPairingService
|
|
259
|
+
*
|
|
260
|
+
* Implements the connector side of the owner-pairing flow for Telegram:
|
|
261
|
+
* - `/eliza_pair <code>` bot command: relays a 6-digit pair code to the
|
|
262
|
+
* backend `verifyOwnerBindFromConnector` service and reports the result.
|
|
263
|
+
* - `sendOwnerLoginDmLink({ externalId, link })`: called by the backend's
|
|
264
|
+
* `/api/auth/login/owner/dm-link/request` handler to DM a login link to
|
|
265
|
+
* the Telegram user identified by their numeric user ID.
|
|
266
|
+
*
|
|
267
|
+
* Hard rules:
|
|
268
|
+
* - Backend is the authority. The connector only relays; it never decides
|
|
269
|
+
* whether a binding succeeds.
|
|
270
|
+
* - Fail closed: if the backend service is unreachable, we reply with an
|
|
271
|
+
* explicit error message and do NOT silently succeed.
|
|
272
|
+
* - Per-user rate limit on `/eliza_pair` invocations: 5 attempts per minute.
|
|
273
|
+
* - DM-link sender never pre-fetches or auto-redeems the link.
|
|
274
|
+
*
|
|
275
|
+
* Telegram command naming: underscores instead of hyphens, per Telegram bot
|
|
276
|
+
* command conventions (commands must match [a-z0-9_]{1,32}).
|
|
277
|
+
*/
|
|
278
|
+
|
|
279
|
+
/** Service type string used by the backend to look up this service. */
|
|
280
|
+
declare const TELEGRAM_OWNER_PAIRING_SERVICE_TYPE = "OWNER_PAIRING_TELEGRAM";
|
|
281
|
+
/**
|
|
282
|
+
* Public service interface exposed via the runtime service registry.
|
|
283
|
+
* The backend's `owner-binding.ts` calls `sendOwnerLoginDmLink` when the
|
|
284
|
+
* user requests a DM login link via the dashboard.
|
|
285
|
+
*/
|
|
286
|
+
interface TelegramOwnerPairingService {
|
|
287
|
+
/**
|
|
288
|
+
* DMs the Telegram user identified by `externalId` (a numeric Telegram user
|
|
289
|
+
* ID as a string) with a login link. The link is presented as-is; this
|
|
290
|
+
* method never pre-fetches or auto-redeems it.
|
|
291
|
+
*
|
|
292
|
+
* Throws if the DM cannot be delivered (Telegram API error, bot blocked by
|
|
293
|
+
* user, etc.). The caller is responsible for surfacing the error.
|
|
294
|
+
*/
|
|
295
|
+
sendOwnerLoginDmLink(params: {
|
|
296
|
+
externalId: string;
|
|
297
|
+
link: string;
|
|
298
|
+
}): Promise<void>;
|
|
299
|
+
}
|
|
300
|
+
declare class TelegramOwnerPairingServiceImpl extends Service implements TelegramOwnerPairingService {
|
|
301
|
+
static serviceType: string;
|
|
302
|
+
capabilityDescription: string;
|
|
303
|
+
static start(runtime: IAgentRuntime): Promise<Service>;
|
|
304
|
+
stop(): Promise<void>;
|
|
305
|
+
/**
|
|
306
|
+
* Registers the /eliza_pair command with the active Telegraf bot instance
|
|
307
|
+
* by looking up the TelegramService from the runtime service registry.
|
|
308
|
+
* Called during `start`; it is safe to call this before or after the bot
|
|
309
|
+
* has finished initialising because Telegraf accepts handler registration
|
|
310
|
+
* at any point before `launch()`.
|
|
311
|
+
*
|
|
312
|
+
* If the TelegramService is unavailable, the command is not registered.
|
|
313
|
+
*/
|
|
314
|
+
private registerPairCommand;
|
|
315
|
+
sendOwnerLoginDmLink(params: {
|
|
316
|
+
externalId: string;
|
|
317
|
+
link: string;
|
|
318
|
+
}): Promise<void>;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
type TelegramConnectorReadParams = {
|
|
322
|
+
target?: TargetInfo;
|
|
323
|
+
limit?: number;
|
|
324
|
+
query?: string;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Class representing a Telegram service that allows the agent to send and receive messages on Telegram.
|
|
328
|
+
* This service handles all Telegram-specific functionality including:
|
|
329
|
+
* - Initializing and managing the Telegram bot
|
|
330
|
+
* - Setting up middleware for preprocessing messages
|
|
331
|
+
* - Handling message and reaction events
|
|
332
|
+
* - Synchronizing Telegram chats, users, and entities with the agent runtime
|
|
333
|
+
* - Managing forum topics as separate rooms
|
|
334
|
+
*
|
|
335
|
+
* @extends Service
|
|
336
|
+
*/
|
|
337
|
+
declare class TelegramService extends Service {
|
|
338
|
+
static serviceType: string;
|
|
339
|
+
capabilityDescription: string;
|
|
340
|
+
private bot;
|
|
341
|
+
messageManager: MessageManager | null;
|
|
342
|
+
private knownChats;
|
|
343
|
+
private syncedEntityIds;
|
|
344
|
+
private botToken;
|
|
345
|
+
private defaultAccountId;
|
|
346
|
+
private accountStates;
|
|
347
|
+
/**
|
|
348
|
+
* Constructor for TelegramService class.
|
|
349
|
+
* @param {IAgentRuntime} runtime - The runtime object for the agent.
|
|
350
|
+
*/
|
|
351
|
+
constructor(runtime?: IAgentRuntime);
|
|
352
|
+
private createAccountRuntime;
|
|
353
|
+
private setDefaultAccountState;
|
|
354
|
+
private getDefaultAccountState;
|
|
355
|
+
private getAccountState;
|
|
356
|
+
private getAccountIds;
|
|
357
|
+
private resolveAccountIdFromContext;
|
|
358
|
+
private resolveAccountIdForTarget;
|
|
359
|
+
private scopedTelegramKey;
|
|
360
|
+
private knownChatKeyMatchesAccount;
|
|
361
|
+
/**
|
|
362
|
+
* Starts the Telegram service for the given runtime.
|
|
363
|
+
*
|
|
364
|
+
* @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for.
|
|
365
|
+
* @returns {Promise<TelegramService>} A promise that resolves with the initialized TelegramService.
|
|
366
|
+
*/
|
|
367
|
+
static start(runtime: IAgentRuntime): Promise<TelegramService>;
|
|
368
|
+
/**
|
|
369
|
+
* Stops the agent runtime.
|
|
370
|
+
* @param {IAgentRuntime} runtime - The agent runtime to stop
|
|
371
|
+
*/
|
|
372
|
+
static stop(runtime: IAgentRuntime): Promise<void>;
|
|
373
|
+
/**
|
|
374
|
+
* Asynchronously stops the bot.
|
|
375
|
+
*
|
|
376
|
+
* @returns A Promise that resolves once the bot has stopped.
|
|
377
|
+
*/
|
|
378
|
+
stop(): Promise<void>;
|
|
379
|
+
/**
|
|
380
|
+
* Initializes the Telegram bot by launching it, getting bot info, and setting up message manager.
|
|
381
|
+
* @returns {Promise<void>} A Promise that resolves when the initialization is complete.
|
|
382
|
+
*/
|
|
383
|
+
private initializeBot;
|
|
384
|
+
/**
|
|
385
|
+
* Sets up the middleware chain for preprocessing messages before they reach handlers.
|
|
386
|
+
* This critical method establishes a sequential processing pipeline that:
|
|
387
|
+
*
|
|
388
|
+
* 1. Authorization - Verifies if a chat is allowed to interact with the bot based on configured settings
|
|
389
|
+
* 2. Chat Discovery - Ensures chat entities and worlds exist in the runtime, creating them if needed
|
|
390
|
+
* 3. Forum Topics - Handles Telegram forum topics as separate rooms for better conversation management
|
|
391
|
+
* 4. Entity Synchronization - Ensures message senders are properly synchronized as entities
|
|
392
|
+
*
|
|
393
|
+
* The middleware chain runs in sequence for each message, with each step potentially
|
|
394
|
+
* enriching the context or stopping processing if conditions aren't met.
|
|
395
|
+
* This preprocessing is essential for maintaining consistent state before message handlers execute.
|
|
396
|
+
*
|
|
397
|
+
* @private
|
|
398
|
+
*/
|
|
399
|
+
private setupMiddlewares;
|
|
400
|
+
/**
|
|
401
|
+
* Authorization middleware - checks if chat is allowed to interact with the bot
|
|
402
|
+
* based on the TELEGRAM_ALLOWED_CHATS configuration.
|
|
403
|
+
*
|
|
404
|
+
* @param {Context} ctx - The context of the incoming update
|
|
405
|
+
* @param {Function} next - The function to call to proceed to the next middleware
|
|
406
|
+
* @returns {Promise<void>}
|
|
407
|
+
* @private
|
|
408
|
+
*/
|
|
409
|
+
private authorizationMiddleware;
|
|
410
|
+
/**
|
|
411
|
+
* Chat and entity management middleware - handles new chats, forum topics, and entity synchronization.
|
|
412
|
+
* This middleware implements decision logic to determine which operations are needed based on
|
|
413
|
+
* the chat type and whether we've seen this chat before.
|
|
414
|
+
*
|
|
415
|
+
* @param {Context} ctx - The context of the incoming update
|
|
416
|
+
* @param {Function} next - The function to call to proceed to the next middleware
|
|
417
|
+
* @returns {Promise<void>}
|
|
418
|
+
* @private
|
|
419
|
+
*/
|
|
420
|
+
private chatAndEntityMiddleware;
|
|
421
|
+
/**
|
|
422
|
+
* Process an existing chat based on chat type and message properties.
|
|
423
|
+
* Different chat types require different processing steps.
|
|
424
|
+
*
|
|
425
|
+
* @param {Context} ctx - The context of the incoming update
|
|
426
|
+
* @returns {Promise<void>}
|
|
427
|
+
* @private
|
|
428
|
+
*/
|
|
429
|
+
private processExistingChat;
|
|
430
|
+
/**
|
|
431
|
+
* Sets up message and reaction handlers for the bot.
|
|
432
|
+
* Configures event handlers to process incoming messages and reactions.
|
|
433
|
+
*
|
|
434
|
+
* @private
|
|
435
|
+
*/
|
|
436
|
+
private setupMessageHandlers;
|
|
437
|
+
/**
|
|
438
|
+
* Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting.
|
|
439
|
+
* @param {Context} ctx - The context of the incoming update.
|
|
440
|
+
* @returns {Promise<boolean>} A Promise that resolves with a boolean indicating if the group is authorized.
|
|
441
|
+
*/
|
|
442
|
+
private isGroupAuthorized;
|
|
443
|
+
/**
|
|
444
|
+
* Synchronizes an entity from a message context with the runtime system.
|
|
445
|
+
* This method handles three cases:
|
|
446
|
+
* 1. Message sender - most common case
|
|
447
|
+
* 2. New chat member - when a user joins the chat
|
|
448
|
+
* 3. Left chat member - when a user leaves the chat
|
|
449
|
+
*
|
|
450
|
+
* @param {Context} ctx - The context of the incoming update
|
|
451
|
+
* @returns {Promise<void>}
|
|
452
|
+
* @private
|
|
453
|
+
*/
|
|
454
|
+
private syncEntity;
|
|
455
|
+
/**
|
|
456
|
+
* Synchronizes the message sender entity with the runtime system.
|
|
457
|
+
* This is the most common entity sync case.
|
|
458
|
+
*
|
|
459
|
+
* @param {Context} ctx - The context of the incoming update
|
|
460
|
+
* @param {UUID} worldId - The ID of the world
|
|
461
|
+
* @param {UUID} roomId - The ID of the room
|
|
462
|
+
* @param {string} chatId - The ID of the chat
|
|
463
|
+
* @returns {Promise<void>}
|
|
464
|
+
* @private
|
|
465
|
+
*/
|
|
466
|
+
private syncMessageSender;
|
|
467
|
+
/**
|
|
468
|
+
* Synchronizes a new chat member entity with the runtime system.
|
|
469
|
+
* Triggered when a user joins the chat.
|
|
470
|
+
*
|
|
471
|
+
* @param {Context} ctx - The context of the incoming update
|
|
472
|
+
* @param {UUID} worldId - The ID of the world
|
|
473
|
+
* @param {UUID} roomId - The ID of the room
|
|
474
|
+
* @param {string} chatId - The ID of the chat
|
|
475
|
+
* @returns {Promise<void>}
|
|
476
|
+
* @private
|
|
477
|
+
*/
|
|
478
|
+
private syncNewChatMember;
|
|
479
|
+
/**
|
|
480
|
+
* Updates entity status when a user leaves the chat.
|
|
481
|
+
*
|
|
482
|
+
* @param {Context} ctx - The context of the incoming update
|
|
483
|
+
* @returns {Promise<void>}
|
|
484
|
+
* @private
|
|
485
|
+
*/
|
|
486
|
+
private syncLeftChatMember;
|
|
487
|
+
/**
|
|
488
|
+
* Handles forum topics by creating appropriate rooms in the runtime system.
|
|
489
|
+
* This enables proper conversation management for Telegram's forum feature.
|
|
490
|
+
*
|
|
491
|
+
* @param {Context} ctx - The context of the incoming update
|
|
492
|
+
* @returns {Promise<void>}
|
|
493
|
+
* @private
|
|
494
|
+
*/
|
|
495
|
+
private handleForumTopic;
|
|
496
|
+
/**
|
|
497
|
+
* Builds entity for message sender
|
|
498
|
+
*/
|
|
499
|
+
private buildMsgSenderEntity;
|
|
500
|
+
/**
|
|
501
|
+
* Handles new chat discovery and emits WORLD_JOINED event.
|
|
502
|
+
* This is a critical function that ensures new chats are properly
|
|
503
|
+
* registered in the runtime system and appropriate events are emitted.
|
|
504
|
+
*
|
|
505
|
+
* @param {Context} ctx - The context of the incoming update
|
|
506
|
+
* @returns {Promise<void>}
|
|
507
|
+
* @private
|
|
508
|
+
*/
|
|
509
|
+
private handleNewChat;
|
|
510
|
+
/**
|
|
511
|
+
* Processes entities in batches to prevent overwhelming the system.
|
|
512
|
+
*
|
|
513
|
+
* @param {Entity[]} entities - The entities to process
|
|
514
|
+
* @param {UUID} roomId - The ID of the room to connect entities to
|
|
515
|
+
* @param {string} channelId - The channel ID
|
|
516
|
+
* @param {ChannelType} roomType - The type of the room
|
|
517
|
+
* @param {UUID} worldId - The ID of the world
|
|
518
|
+
* @returns {Promise<void>}
|
|
519
|
+
* @private
|
|
520
|
+
*/
|
|
521
|
+
private batchProcessEntities;
|
|
522
|
+
/**
|
|
523
|
+
* Gets chat title and channel type based on Telegram chat type.
|
|
524
|
+
* Maps Telegram-specific chat types to standardized system types.
|
|
525
|
+
*
|
|
526
|
+
* @param {any} chat - The Telegram chat object
|
|
527
|
+
* @returns {Object} Object containing chatTitle and channelType
|
|
528
|
+
* @private
|
|
529
|
+
*/
|
|
530
|
+
private getChatTypeInfo;
|
|
531
|
+
/**
|
|
532
|
+
* Builds standardized entity representations from Telegram chat data.
|
|
533
|
+
* Transforms Telegram-specific user data into system-standard Entity objects.
|
|
534
|
+
*
|
|
535
|
+
* @param {any} chat - The Telegram chat object
|
|
536
|
+
* @returns {Promise<Entity[]>} Array of standardized Entity objects
|
|
537
|
+
* @private
|
|
538
|
+
*/
|
|
539
|
+
private buildStandardizedEntities;
|
|
540
|
+
/**
|
|
541
|
+
* Extracts and builds the room object for a forum topic from a message context.
|
|
542
|
+
* This refactored method can be used both in middleware and when handling new chats.
|
|
543
|
+
*
|
|
544
|
+
* @param {Context} ctx - The context of the incoming update
|
|
545
|
+
* @param {UUID} worldId - The ID of the world the topic belongs to
|
|
546
|
+
* @returns {Promise<Room | null>} A Promise that resolves with the room or null if not a topic
|
|
547
|
+
* @private
|
|
548
|
+
*/
|
|
549
|
+
private buildForumTopicRoom;
|
|
550
|
+
private buildConnectorChatTarget;
|
|
551
|
+
private buildConnectorRoomTarget;
|
|
552
|
+
private dedupeConnectorTargets;
|
|
553
|
+
private getTelegramChatForTarget;
|
|
554
|
+
resolveConnectorTargets(query: string, context: MessageConnectorQueryContext): Promise<MessageConnectorTarget[]>;
|
|
555
|
+
listConnectorRooms(context: MessageConnectorQueryContext): Promise<MessageConnectorTarget[]>;
|
|
556
|
+
listRecentConnectorTargets(context: MessageConnectorQueryContext): Promise<MessageConnectorTarget[]>;
|
|
557
|
+
fetchConnectorMessages(context: MessageConnectorQueryContext, params?: TelegramConnectorReadParams): Promise<Memory[]>;
|
|
558
|
+
searchConnectorMessages(context: MessageConnectorQueryContext, params: TelegramConnectorReadParams & {
|
|
559
|
+
query: string;
|
|
560
|
+
}): Promise<Memory[]>;
|
|
561
|
+
getConnectorChatContext(target: TargetInfo, context: MessageConnectorQueryContext): Promise<MessageConnectorChatContext | null>;
|
|
562
|
+
getConnectorUserContext(entityId: UUID | string, context: MessageConnectorQueryContext): Promise<MessageConnectorUserContext | null>;
|
|
563
|
+
static registerSendHandlers(runtime: IAgentRuntime, serviceInstance: TelegramService): void;
|
|
564
|
+
handleSendMessage(runtime: IAgentRuntime, target: TargetInfo, content: Content): Promise<void>;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
declare const DEFAULT_ACCOUNT_ID = "default";
|
|
568
|
+
interface TelegramAccountConfig {
|
|
569
|
+
name?: string;
|
|
570
|
+
enabled?: boolean;
|
|
571
|
+
botToken?: string;
|
|
572
|
+
apiRoot?: string;
|
|
573
|
+
allowedChats?: string[];
|
|
574
|
+
autoReply?: boolean;
|
|
575
|
+
personal?: {
|
|
576
|
+
phone?: string;
|
|
577
|
+
appId?: string;
|
|
578
|
+
appHash?: string;
|
|
579
|
+
session?: string;
|
|
580
|
+
enabled?: boolean;
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
interface TelegramMultiAccountConfig {
|
|
584
|
+
enabled?: boolean;
|
|
585
|
+
botToken?: string;
|
|
586
|
+
apiRoot?: string;
|
|
587
|
+
accounts?: Record<string, TelegramAccountConfig>;
|
|
588
|
+
}
|
|
589
|
+
interface ResolvedTelegramAccount {
|
|
590
|
+
accountId: string;
|
|
591
|
+
enabled: boolean;
|
|
592
|
+
name?: string;
|
|
593
|
+
botToken?: string;
|
|
594
|
+
apiRoot: string;
|
|
595
|
+
config: TelegramAccountConfig;
|
|
596
|
+
}
|
|
597
|
+
declare function normalizeTelegramAccountId(accountId?: string | null): string;
|
|
598
|
+
declare function getTelegramMultiAccountConfig(runtime: IAgentRuntime): TelegramMultiAccountConfig;
|
|
599
|
+
declare function listTelegramAccountIds(runtime: IAgentRuntime): string[];
|
|
600
|
+
declare function resolveDefaultTelegramAccountId(runtime: IAgentRuntime): string;
|
|
601
|
+
declare function resolveTelegramAccount(runtime: IAgentRuntime, accountId?: string | null): ResolvedTelegramAccount;
|
|
602
|
+
declare function listEnabledTelegramAccounts(runtime: IAgentRuntime): ResolvedTelegramAccount[];
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Telegram ConnectorAccountManager provider.
|
|
606
|
+
*
|
|
607
|
+
* Bridges plugin-telegram to the @elizaos/core ConnectorAccountManager so the
|
|
608
|
+
* generic HTTP CRUD surface can list, create, patch, and delete Telegram
|
|
609
|
+
* accounts. Telegram bots authenticate via a long-lived bot token (no OAuth);
|
|
610
|
+
* `startOAuth` / `completeOAuth` are intentionally not implemented.
|
|
611
|
+
*
|
|
612
|
+
* Single-account env-only configurations (TELEGRAM_BOT_TOKEN) are surfaced as
|
|
613
|
+
* a synthesized 'default' account with role 'AGENT' so downstream consumers
|
|
614
|
+
* see a uniform list. Multi-account configs declared on character.settings.telegram
|
|
615
|
+
* are surfaced verbatim from manager-owned storage.
|
|
616
|
+
*/
|
|
617
|
+
|
|
618
|
+
declare function createTelegramConnectorAccountProvider(runtime: IAgentRuntime): ConnectorAccountProvider;
|
|
619
|
+
|
|
620
|
+
declare const telegramPlugin: Plugin;
|
|
621
|
+
|
|
622
|
+
export { DEFAULT_ACCOUNT_ID, MessageManager, type ResolvedTelegramAccount, TELEGRAM_OWNER_PAIRING_SERVICE_TYPE, type TelegramAccountConfig, type TelegramMultiAccountConfig, type TelegramOwnerPairingService, TelegramOwnerPairingServiceImpl, TelegramService, createTelegramConnectorAccountProvider, telegramPlugin as default, getTelegramMultiAccountConfig, listEnabledTelegramAccounts, listTelegramAccountIds, normalizeTelegramAccountId, resolveDefaultTelegramAccountId, resolveTelegramAccount, stopTelegramAccountAuthSession };
|