import { type Content, type IAgentRuntime, Service, type TargetInfo } from '@elizaos/core'; import { MessageManager } from './messageManager'; /** * Class representing a Telegram service that allows the agent to send and receive messages on Telegram. * This service handles all Telegram-specific functionality including: * - Initializing and managing the Telegram bot * - Setting up middleware for preprocessing messages * - Handling message and reaction events * - Synchronizing Telegram chats, users, and entities with the agent runtime * - Managing forum topics as separate rooms * * @extends Service */ export declare class TelegramService extends Service { static serviceType: string; capabilityDescription: string; private bot; messageManager: MessageManager | null; private options; private knownChats; private syncedEntityIds; /** * Constructor for TelegramService class. * @param {IAgentRuntime} runtime - The runtime object for the agent. */ constructor(runtime: IAgentRuntime); /** * Starts the Telegram service for the given runtime. * * @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for. * @returns {Promise} A promise that resolves with the initialized TelegramService. */ static start(runtime: IAgentRuntime): Promise; /** * Stops the agent runtime. * @param {IAgentRuntime} runtime - The agent runtime to stop */ static stop(runtime: IAgentRuntime): Promise; /** * Asynchronously stops the bot. * * @returns A Promise that resolves once the bot has stopped. */ stop(): Promise; /** * Initializes the Telegram bot by launching it, getting bot info, and setting up message manager. * @returns {Promise} A Promise that resolves when the initialization is complete. */ private initializeBot; /** * Sets up the middleware chain for preprocessing messages before they reach handlers. * This critical method establishes a sequential processing pipeline that: * * 1. Authorization - Verifies if a chat is allowed to interact with the bot based on configured settings * 2. Chat Discovery - Ensures chat entities and worlds exist in the runtime, creating them if needed * 3. Forum Topics - Handles Telegram forum topics as separate rooms for better conversation management * 4. Entity Synchronization - Ensures message senders are properly synchronized as entities * * The middleware chain runs in sequence for each message, with each step potentially * enriching the context or stopping processing if conditions aren't met. * This preprocessing is essential for maintaining consistent state before message handlers execute. * * @private */ private setupMiddlewares; /** * Authorization middleware - checks if chat is allowed to interact with the bot * based on the TELEGRAM_ALLOWED_CHATS configuration. * * @param {Context} ctx - The context of the incoming update * @param {Function} next - The function to call to proceed to the next middleware * @returns {Promise} * @private */ private authorizationMiddleware; /** * Chat and entity management middleware - handles new chats, forum topics, and entity synchronization. * This middleware implements decision logic to determine which operations are needed based on * the chat type and whether we've seen this chat before. * * @param {Context} ctx - The context of the incoming update * @param {Function} next - The function to call to proceed to the next middleware * @returns {Promise} * @private */ private chatAndEntityMiddleware; /** * Process an existing chat based on chat type and message properties. * Different chat types require different processing steps. * * @param {Context} ctx - The context of the incoming update * @returns {Promise} * @private */ private processExistingChat; /** * Sets up message and reaction handlers for the bot. * Configures event handlers to process incoming messages and reactions. * * @private */ private setupMessageHandlers; /** * Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting. * @param {Context} ctx - The context of the incoming update. * @returns {Promise} A Promise that resolves with a boolean indicating if the group is authorized. */ private isGroupAuthorized; /** * Synchronizes an entity from a message context with the runtime system. * This method handles three cases: * 1. Message sender - most common case * 2. New chat member - when a user joins the chat * 3. Left chat member - when a user leaves the chat * * @param {Context} ctx - The context of the incoming update * @returns {Promise} * @private */ private syncEntity; /** * Synchronizes the message sender entity with the runtime system. * This is the most common entity sync case. * * @param {Context} ctx - The context of the incoming update * @param {UUID} worldId - The ID of the world * @param {UUID} roomId - The ID of the room * @param {string} chatId - The ID of the chat * @returns {Promise} * @private */ private syncMessageSender; /** * Synchronizes a new chat member entity with the runtime system. * Triggered when a user joins the chat. * * @param {Context} ctx - The context of the incoming update * @param {UUID} worldId - The ID of the world * @param {UUID} roomId - The ID of the room * @param {string} chatId - The ID of the chat * @returns {Promise} * @private */ private syncNewChatMember; /** * Updates entity status when a user leaves the chat. * * @param {Context} ctx - The context of the incoming update * @returns {Promise} * @private */ private syncLeftChatMember; /** * Handles forum topics by creating appropriate rooms in the runtime system. * This enables proper conversation management for Telegram's forum feature. * * @param {Context} ctx - The context of the incoming update * @returns {Promise} * @private */ private handleForumTopic; /** * Builds entity for message sender */ private buildMsgSenderEntity; /** * Handles new chat discovery and emits WORLD_JOINED event. * This is a critical function that ensures new chats are properly * registered in the runtime system and appropriate events are emitted. * * @param {Context} ctx - The context of the incoming update * @returns {Promise} * @private */ private handleNewChat; /** * Processes entities in batches to prevent overwhelming the system. * * @param {Entity[]} entities - The entities to process * @param {UUID} roomId - The ID of the room to connect entities to * @param {string} channelId - The channel ID * @param {ChannelType} roomType - The type of the room * @param {UUID} worldId - The ID of the world * @returns {Promise} * @private */ private batchProcessEntities; /** * Gets chat title and channel type based on Telegram chat type. * Maps Telegram-specific chat types to standardized system types. * * @param {any} chat - The Telegram chat object * @returns {Object} Object containing chatTitle and channelType * @private */ private getChatTypeInfo; /** * Builds standardized entity representations from Telegram chat data. * Transforms Telegram-specific user data into system-standard Entity objects. * * @param {any} chat - The Telegram chat object * @returns {Promise} Array of standardized Entity objects * @private */ private buildStandardizedEntities; /** * Extracts and builds the room object for a forum topic from a message context. * This refactored method can be used both in middleware and when handling new chats. * * @param {Context} ctx - The context of the incoming update * @param {UUID} worldId - The ID of the world the topic belongs to * @returns {Promise} A Promise that resolves with the room or null if not a topic * @private */ private buildForumTopicRoom; static registerSendHandlers(runtime: IAgentRuntime, serviceInstance: TelegramService): void; handleSendMessage(runtime: IAgentRuntime, target: TargetInfo, content: Content): Promise; }