@elizaos/plugin-telegram 1.6.4 → 2.0.0-alpha.2
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/LICENSE +1 -1
- package/dist/index.js +12701 -977
- package/dist/index.js.map +87 -1
- package/package.json +15 -19
- package/README.md +0 -143
- package/dist/constants.d.ts +0 -10
- package/dist/environment.d.ts +0 -17
- package/dist/index.d.ts +0 -6
- package/dist/messageManager.d.ts +0 -131
- package/dist/service.d.ts +0 -218
- package/dist/tests.d.ts +0 -52
- package/dist/types.d.ts +0 -99
- package/dist/utils.d.ts +0 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elizaos/plugin-telegram",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -18,39 +18,35 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@elizaos/core": "
|
|
21
|
+
"@elizaos/core": "2.0.0-alpha.2",
|
|
22
22
|
"@telegraf/types": "7.1.0",
|
|
23
|
-
"@types/node": "^
|
|
23
|
+
"@types/node": "^25.0.3",
|
|
24
24
|
"strip-literal": "^3.0.0",
|
|
25
25
|
"telegraf": "4.16.3",
|
|
26
26
|
"type-detect": "^4.1.0",
|
|
27
|
-
"typescript": "^5.
|
|
27
|
+
"typescript": "^5.9.3"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"
|
|
32
|
-
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
|
33
|
-
"@typescript-eslint/parser": "^8.22.0",
|
|
34
|
-
"eslint": "^9.17.0",
|
|
35
|
-
"prettier": "3.5.3",
|
|
36
|
-
"tsup": "8.4.0",
|
|
37
|
-
"vitest": "1.6.1"
|
|
30
|
+
"@biomejs/biome": "^2.3.11",
|
|
31
|
+
"vitest": "^4.0.0"
|
|
38
32
|
},
|
|
39
33
|
"scripts": {
|
|
40
|
-
"build": "
|
|
41
|
-
"dev": "
|
|
34
|
+
"build": "bun run build.ts",
|
|
35
|
+
"dev": "bun --hot build.ts",
|
|
42
36
|
"test": "vitest run",
|
|
43
37
|
"test:watch": "vitest",
|
|
44
|
-
"lint": "
|
|
45
|
-
"lint:check": "eslint ./src",
|
|
38
|
+
"lint": "bunx @biomejs/biome check --write --unsafe .",
|
|
46
39
|
"clean": "rm -rf dist .turbo node_modules .turbo-tsconfig.json tsconfig.tsbuildinfo",
|
|
47
|
-
"format": "
|
|
48
|
-
"format:check": "
|
|
40
|
+
"format": "bunx @biomejs/biome format --write .",
|
|
41
|
+
"format:check": "bunx @biomejs/biome format .",
|
|
42
|
+
"lint:check": "bunx @biomejs/biome check .",
|
|
43
|
+
"build:ts": "bun run build",
|
|
44
|
+
"typecheck": "tsc --noEmit"
|
|
49
45
|
},
|
|
50
46
|
"publishConfig": {
|
|
51
47
|
"access": "public"
|
|
52
48
|
},
|
|
53
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "bc6cac8d36845d7cbde51a64307c6a57c16378ad",
|
|
54
50
|
"repository": {
|
|
55
51
|
"type": "git",
|
|
56
52
|
"url": "git+https://github.com/elizaos-plugins/plugin-telegram.git"
|
package/README.md
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
# Telegram Client Plugin for ElizaOS
|
|
2
|
-
|
|
3
|
-
This plugin integrates a Telegram client with ElizaOS, allowing characters in ElizaOS to interact via Telegram. It provides an easy setup for starting the Telegram client using the provided bot token and includes basic lifecycle management.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **Seamless Telegram Integration**: Connects ElizaOS characters to Telegram through the bot API.
|
|
8
|
-
- **Configuration Validation**: Ensures required settings are properly configured before starting.
|
|
9
|
-
- **Startup Logging**: Logs successful initialization of the Telegram client for better debugging.
|
|
10
|
-
- **Future-proof Design**: Provides a basic structure for stopping the client (currently unsupported).
|
|
11
|
-
|
|
12
|
-
## Configuration Options
|
|
13
|
-
|
|
14
|
-
Here are the available configuration options for the `character.json` file:
|
|
15
|
-
|
|
16
|
-
| Key | Type | Default | Description |
|
|
17
|
-
| ------------------------------- | ------- | -------- | --------------------------------------------------------------------------------------------------- |
|
|
18
|
-
| `clients` | Array | Required | Specifies the client type (e.g., `["telegram"]`). |
|
|
19
|
-
| `allowDirectMessages` | Boolean | `false` | Determines whether the bot should respond to direct messages (DMs). |
|
|
20
|
-
| `shouldOnlyJoinInAllowedGroups` | Boolean | `false` | Ensures the bot only joins and responds in specified groups. |
|
|
21
|
-
| `allowedGroupIds` | Array | `[]` | Lists the group IDs the bot is allowed to interact with (requires `shouldOnlyJoinInAllowedGroups`). |
|
|
22
|
-
| `messageTrackingLimit` | Integer | `100` | Sets the maximum number of messages to track in memory for each chat. |
|
|
23
|
-
| `templates` | Object | `{}` | Allows customization of response templates for different message scenarios. |
|
|
24
|
-
|
|
25
|
-
## Error 409: Conflict in Multiple Agents Environment
|
|
26
|
-
|
|
27
|
-
When you encounter this error in your logs:
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
error: 409: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
This indicates a fundamental architectural limitation with the Telegram Bot API. The Telegram API strictly enforces that only one active connection can exist per bot token at any given time. This is by design to ensure reliable message delivery and prevent message duplication or loss.
|
|
34
|
-
|
|
35
|
-
In ElizaOS multi-agent environments, this error commonly occurs when:
|
|
36
|
-
|
|
37
|
-
1. **Multiple Agents Using Same Token**: Two or more agents (such as "Eliza" and another character) each have the `@elizaos/plugin-telegram` plugin enabled in their configuration
|
|
38
|
-
2. **Simultaneous Initialization**: Each agent independently attempts to initialize its own Telegram service during startup
|
|
39
|
-
3. **Token Collision**: All agents use the same `TELEGRAM_BOT_TOKEN` from your environment configuration
|
|
40
|
-
4. **Connection Rejection**: When a second agent tries to establish a connection while another is already active, Telegram rejects it with a 409 error
|
|
41
|
-
|
|
42
|
-
This is not a bug in ElizaOS or the Telegram plugin, but rather a result of using a shared resource (the bot token) that can only accept one connection at a time.
|
|
43
|
-
|
|
44
|
-
## Example `<charactername>.character.json`
|
|
45
|
-
|
|
46
|
-
Below is an example configuration file with all options:
|
|
47
|
-
|
|
48
|
-
```json
|
|
49
|
-
{
|
|
50
|
-
"clients": ["telegram"],
|
|
51
|
-
"allowDirectMessages": true,
|
|
52
|
-
"shouldOnlyJoinInAllowedGroups": true,
|
|
53
|
-
"allowedGroupIds": ["-123456789", "-987654321"],
|
|
54
|
-
"messageTrackingLimit": 100,
|
|
55
|
-
"templates": {
|
|
56
|
-
"telegramMessageHandlerTemplate": "Your custom template here"
|
|
57
|
-
},
|
|
58
|
-
"secrets": {
|
|
59
|
-
"key": "<your-bot-token>"
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
## How to Modify Settings
|
|
65
|
-
|
|
66
|
-
1. Locate the `character.json` file in your project directory.
|
|
67
|
-
2. Update the file with the desired configuration options as shown in the example above.
|
|
68
|
-
3. Save the file and restart the bot for the changes to take effect.
|
|
69
|
-
|
|
70
|
-
## Best Practices
|
|
71
|
-
|
|
72
|
-
- **Production**: Restrict bot access with `shouldOnlyJoinInAllowedGroups: true` and specify `allowedGroupIds` to ensure security.
|
|
73
|
-
- **Token Management**: Always keep your bot token and backend tokens secure and never expose them in public repositories.
|
|
74
|
-
|
|
75
|
-
## Pre-Requisites
|
|
76
|
-
|
|
77
|
-
1. Add the bot token to the `.env` file in the project root:
|
|
78
|
-
|
|
79
|
-
```env
|
|
80
|
-
TELEGRAM_BOT_TOKEN=your-bot-token
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
2. Add the same token to your character configuration file:
|
|
84
|
-
|
|
85
|
-
Create or modify `characters/your-character.json`:
|
|
86
|
-
|
|
87
|
-
```json
|
|
88
|
-
{
|
|
89
|
-
"clients": ["telegram"],
|
|
90
|
-
"secrets": {
|
|
91
|
-
"key": "<your-bot-token>"
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
## From the project root:
|
|
97
|
-
|
|
98
|
-
```bash
|
|
99
|
-
npm run dev
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
## Or using bun:
|
|
103
|
-
|
|
104
|
-
```bash
|
|
105
|
-
bun start --character="characters/your-character.json"
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Utilizing Telegram Buttons
|
|
109
|
-
|
|
110
|
-
To send a message with native Telegram buttons, include an array of buttons in the message content. The following action demonstrates how to initiate a login flow using a Telegram button.
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
export const initAuthHandshakeAction: Action = {
|
|
114
|
-
name: 'INIT_AUTH_HANDSHAKE',
|
|
115
|
-
description: 'Initiates the identity linking and authentication flow for new users.',
|
|
116
|
-
validate: async (_runtime, _message, _state) => {
|
|
117
|
-
return _message.content.source === 'telegram';
|
|
118
|
-
},
|
|
119
|
-
handler: async (runtime, message, _state, _options, callback): Promise<boolean> => {
|
|
120
|
-
try {
|
|
121
|
-
const user = await getUser(message.userId);
|
|
122
|
-
if (user) return false;
|
|
123
|
-
|
|
124
|
-
callback({
|
|
125
|
-
text: "Let's get you set up with a new account",
|
|
126
|
-
buttons: [
|
|
127
|
-
{
|
|
128
|
-
text: '🔑 Authenticate with Telegram',
|
|
129
|
-
url: `${FRONTEND_URL}/integrations/telegram`,
|
|
130
|
-
kind: 'login',
|
|
131
|
-
},
|
|
132
|
-
],
|
|
133
|
-
}).catch((error) => {
|
|
134
|
-
console.error('Error sending callback:', error);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
return true;
|
|
138
|
-
} catch (error) {
|
|
139
|
-
...
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
```
|
package/dist/constants.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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";
|
package/dist/environment.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
}, z.core.$strip>;
|
|
6
|
-
/**
|
|
7
|
-
* Represents the type definition for configuring a Telegram bot based on the inferred schema.
|
|
8
|
-
*/
|
|
9
|
-
export type TelegramConfig = z.infer<typeof telegramEnvSchema>;
|
|
10
|
-
/**
|
|
11
|
-
* Validates the Telegram configuration by retrieving the Telegram bot token from the runtime settings or environment variables.
|
|
12
|
-
* Returns null if validation fails instead of throwing an error.
|
|
13
|
-
*
|
|
14
|
-
* @param {IAgentRuntime} runtime - The agent runtime used to get the setting.
|
|
15
|
-
* @returns {Promise<TelegramConfig | null>} A promise that resolves with the validated Telegram configuration or null if invalid.
|
|
16
|
-
*/
|
|
17
|
-
export declare function validateTelegramConfig(runtime: IAgentRuntime): Promise<TelegramConfig | null>;
|
package/dist/index.d.ts
DELETED
package/dist/messageManager.d.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { type Content, type IAgentRuntime, type Media } from '@elizaos/core';
|
|
2
|
-
import type { Message, Update } from '@telegraf/types';
|
|
3
|
-
import type { Context, NarrowedContext, Telegraf } from 'telegraf';
|
|
4
|
-
import { type TelegramContent } from './types';
|
|
5
|
-
/**
|
|
6
|
-
* Interface for structured document processing results.
|
|
7
|
-
*/
|
|
8
|
-
interface DocumentProcessingResult {
|
|
9
|
-
title: string;
|
|
10
|
-
fullText: string;
|
|
11
|
-
formattedDescription: string;
|
|
12
|
-
fileName: string;
|
|
13
|
-
mimeType: string | undefined;
|
|
14
|
-
fileSize: number | undefined;
|
|
15
|
-
error?: string;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Enum representing different types of media.
|
|
19
|
-
* @enum { string }
|
|
20
|
-
* @readonly
|
|
21
|
-
*/
|
|
22
|
-
export declare enum MediaType {
|
|
23
|
-
PHOTO = "photo",
|
|
24
|
-
VIDEO = "video",
|
|
25
|
-
DOCUMENT = "document",
|
|
26
|
-
AUDIO = "audio",
|
|
27
|
-
ANIMATION = "animation"
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Class representing a message manager.
|
|
31
|
-
* @class
|
|
32
|
-
*/
|
|
33
|
-
export declare class MessageManager {
|
|
34
|
-
bot: Telegraf<Context>;
|
|
35
|
-
protected runtime: IAgentRuntime;
|
|
36
|
-
/**
|
|
37
|
-
* Constructor for creating a new instance of a BotAgent.
|
|
38
|
-
*
|
|
39
|
-
* @param {Telegraf<Context>} bot - The Telegraf instance used for interacting with the bot platform.
|
|
40
|
-
* @param {IAgentRuntime} runtime - The runtime environment for the agent.
|
|
41
|
-
*/
|
|
42
|
-
constructor(bot: Telegraf<Context>, runtime: IAgentRuntime);
|
|
43
|
-
/**
|
|
44
|
-
* Process an image from a Telegram message to extract the image URL and description.
|
|
45
|
-
*
|
|
46
|
-
* @param {Message} message - The Telegram message object containing the image.
|
|
47
|
-
* @returns {Promise<{ description: string } | null>} The description of the processed image or null if no image found.
|
|
48
|
-
*/
|
|
49
|
-
processImage(message: Message): Promise<{
|
|
50
|
-
description: string;
|
|
51
|
-
} | null>;
|
|
52
|
-
/**
|
|
53
|
-
* Process a document from a Telegram message to extract the document URL and description.
|
|
54
|
-
* Handles PDFs and other document types by converting them to text when possible.
|
|
55
|
-
*
|
|
56
|
-
* @param {Message} message - The Telegram message object containing the document.
|
|
57
|
-
* @returns {Promise<{ description: string } | null>} The description of the processed document or null if no document found.
|
|
58
|
-
*/
|
|
59
|
-
processDocument(message: Message): Promise<DocumentProcessingResult | null>;
|
|
60
|
-
/**
|
|
61
|
-
* Get the appropriate document processor based on MIME type.
|
|
62
|
-
*/
|
|
63
|
-
private getDocumentProcessor;
|
|
64
|
-
/**
|
|
65
|
-
* Process PDF documents by converting them to text.
|
|
66
|
-
*/
|
|
67
|
-
private processPdfDocument;
|
|
68
|
-
/**
|
|
69
|
-
* Process text documents by fetching their content.
|
|
70
|
-
*/
|
|
71
|
-
private processTextDocument;
|
|
72
|
-
/**
|
|
73
|
-
* Processes the message content, documents, and images to generate
|
|
74
|
-
* processed content and media attachments.
|
|
75
|
-
*
|
|
76
|
-
* @param {Message} message The message to process
|
|
77
|
-
* @returns {Promise<{ processedContent: string; attachments: Media[] }>} Processed content and media attachments
|
|
78
|
-
*/
|
|
79
|
-
processMessage(message: Message): Promise<{
|
|
80
|
-
processedContent: string;
|
|
81
|
-
attachments: Media[];
|
|
82
|
-
}>;
|
|
83
|
-
/**
|
|
84
|
-
* Sends a message in chunks, handling attachments and splitting the message if necessary
|
|
85
|
-
*
|
|
86
|
-
* @param {Context} ctx - The context object representing the current state of the bot
|
|
87
|
-
* @param {TelegramContent} content - The content of the message to be sent
|
|
88
|
-
* @param {number} [replyToMessageId] - The ID of the message to reply to, if any
|
|
89
|
-
* @returns {Promise<Message.TextMessage[]>} - An array of TextMessage objects representing the messages sent
|
|
90
|
-
*/
|
|
91
|
-
sendMessageInChunks(ctx: Context, content: TelegramContent, replyToMessageId?: number): Promise<Message.TextMessage[]>;
|
|
92
|
-
/**
|
|
93
|
-
* Sends media to a chat using the Telegram API.
|
|
94
|
-
*
|
|
95
|
-
* @param {Context} ctx - The context object containing information about the current chat.
|
|
96
|
-
* @param {string} mediaPath - The path to the media to be sent, either a URL or a local file path.
|
|
97
|
-
* @param {MediaType} type - The type of media being sent (PHOTO, VIDEO, DOCUMENT, AUDIO, or ANIMATION).
|
|
98
|
-
* @param {string} [caption] - Optional caption for the media being sent.
|
|
99
|
-
*
|
|
100
|
-
* @returns {Promise<void>} A Promise that resolves when the media is successfully sent.
|
|
101
|
-
*/
|
|
102
|
-
sendMedia(ctx: Context, mediaPath: string, type: MediaType, caption?: string): Promise<void>;
|
|
103
|
-
/**
|
|
104
|
-
* Splits a given text into an array of strings based on the maximum message length.
|
|
105
|
-
*
|
|
106
|
-
* @param {string} text - The text to split into chunks.
|
|
107
|
-
* @returns {string[]} An array of strings with each element representing a chunk of the original text.
|
|
108
|
-
*/
|
|
109
|
-
private splitMessage;
|
|
110
|
-
/**
|
|
111
|
-
* Handle incoming messages from Telegram and process them accordingly.
|
|
112
|
-
* @param {Context} ctx - The context object containing information about the message.
|
|
113
|
-
* @returns {Promise<void>}
|
|
114
|
-
*/
|
|
115
|
-
handleMessage(ctx: Context): Promise<void>;
|
|
116
|
-
/**
|
|
117
|
-
* Handles the reaction event triggered by a user reacting to a message.
|
|
118
|
-
* @param {NarrowedContext<Context<Update>, Update.MessageReactionUpdate>} ctx The context of the message reaction update
|
|
119
|
-
* @returns {Promise<void>} A Promise that resolves when the reaction handling is complete
|
|
120
|
-
*/
|
|
121
|
-
handleReaction(ctx: NarrowedContext<Context<Update>, Update.MessageReactionUpdate>): Promise<void>;
|
|
122
|
-
/**
|
|
123
|
-
* Sends a message to a Telegram chat and emits appropriate events
|
|
124
|
-
* @param {number | string} chatId - The Telegram chat ID to send the message to
|
|
125
|
-
* @param {Content} content - The content to send
|
|
126
|
-
* @param {number} [replyToMessageId] - Optional message ID to reply to
|
|
127
|
-
* @returns {Promise<Message.TextMessage[]>} The sent messages
|
|
128
|
-
*/
|
|
129
|
-
sendMessage(chatId: number | string, content: Content, replyToMessageId?: number): Promise<Message.TextMessage[]>;
|
|
130
|
-
}
|
|
131
|
-
export {};
|
package/dist/service.d.ts
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { type Content, type IAgentRuntime, Service, type TargetInfo } from '@elizaos/core';
|
|
2
|
-
import { MessageManager } from './messageManager';
|
|
3
|
-
/**
|
|
4
|
-
* Class representing a Telegram service that allows the agent to send and receive messages on Telegram.
|
|
5
|
-
* This service handles all Telegram-specific functionality including:
|
|
6
|
-
* - Initializing and managing the Telegram bot
|
|
7
|
-
* - Setting up middleware for preprocessing messages
|
|
8
|
-
* - Handling message and reaction events
|
|
9
|
-
* - Synchronizing Telegram chats, users, and entities with the agent runtime
|
|
10
|
-
* - Managing forum topics as separate rooms
|
|
11
|
-
*
|
|
12
|
-
* @extends Service
|
|
13
|
-
*/
|
|
14
|
-
export declare class TelegramService extends Service {
|
|
15
|
-
static serviceType: string;
|
|
16
|
-
capabilityDescription: string;
|
|
17
|
-
private bot;
|
|
18
|
-
messageManager: MessageManager | null;
|
|
19
|
-
private options;
|
|
20
|
-
private knownChats;
|
|
21
|
-
private syncedEntityIds;
|
|
22
|
-
/**
|
|
23
|
-
* Constructor for TelegramService class.
|
|
24
|
-
* @param {IAgentRuntime} runtime - The runtime object for the agent.
|
|
25
|
-
*/
|
|
26
|
-
constructor(runtime: IAgentRuntime);
|
|
27
|
-
/**
|
|
28
|
-
* Starts the Telegram service for the given runtime.
|
|
29
|
-
*
|
|
30
|
-
* @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for.
|
|
31
|
-
* @returns {Promise<TelegramService>} A promise that resolves with the initialized TelegramService.
|
|
32
|
-
*/
|
|
33
|
-
static start(runtime: IAgentRuntime): Promise<TelegramService>;
|
|
34
|
-
/**
|
|
35
|
-
* Stops the agent runtime.
|
|
36
|
-
* @param {IAgentRuntime} runtime - The agent runtime to stop
|
|
37
|
-
*/
|
|
38
|
-
static stop(runtime: IAgentRuntime): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Asynchronously stops the bot.
|
|
41
|
-
*
|
|
42
|
-
* @returns A Promise that resolves once the bot has stopped.
|
|
43
|
-
*/
|
|
44
|
-
stop(): Promise<void>;
|
|
45
|
-
/**
|
|
46
|
-
* Initializes the Telegram bot by launching it, getting bot info, and setting up message manager.
|
|
47
|
-
* @returns {Promise<void>} A Promise that resolves when the initialization is complete.
|
|
48
|
-
*/
|
|
49
|
-
private initializeBot;
|
|
50
|
-
/**
|
|
51
|
-
* Sets up the middleware chain for preprocessing messages before they reach handlers.
|
|
52
|
-
* This critical method establishes a sequential processing pipeline that:
|
|
53
|
-
*
|
|
54
|
-
* 1. Authorization - Verifies if a chat is allowed to interact with the bot based on configured settings
|
|
55
|
-
* 2. Chat Discovery - Ensures chat entities and worlds exist in the runtime, creating them if needed
|
|
56
|
-
* 3. Forum Topics - Handles Telegram forum topics as separate rooms for better conversation management
|
|
57
|
-
* 4. Entity Synchronization - Ensures message senders are properly synchronized as entities
|
|
58
|
-
*
|
|
59
|
-
* The middleware chain runs in sequence for each message, with each step potentially
|
|
60
|
-
* enriching the context or stopping processing if conditions aren't met.
|
|
61
|
-
* This preprocessing is essential for maintaining consistent state before message handlers execute.
|
|
62
|
-
*
|
|
63
|
-
* @private
|
|
64
|
-
*/
|
|
65
|
-
private setupMiddlewares;
|
|
66
|
-
/**
|
|
67
|
-
* Authorization middleware - checks if chat is allowed to interact with the bot
|
|
68
|
-
* based on the TELEGRAM_ALLOWED_CHATS configuration.
|
|
69
|
-
*
|
|
70
|
-
* @param {Context} ctx - The context of the incoming update
|
|
71
|
-
* @param {Function} next - The function to call to proceed to the next middleware
|
|
72
|
-
* @returns {Promise<void>}
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
private authorizationMiddleware;
|
|
76
|
-
/**
|
|
77
|
-
* Chat and entity management middleware - handles new chats, forum topics, and entity synchronization.
|
|
78
|
-
* This middleware implements decision logic to determine which operations are needed based on
|
|
79
|
-
* the chat type and whether we've seen this chat before.
|
|
80
|
-
*
|
|
81
|
-
* @param {Context} ctx - The context of the incoming update
|
|
82
|
-
* @param {Function} next - The function to call to proceed to the next middleware
|
|
83
|
-
* @returns {Promise<void>}
|
|
84
|
-
* @private
|
|
85
|
-
*/
|
|
86
|
-
private chatAndEntityMiddleware;
|
|
87
|
-
/**
|
|
88
|
-
* Process an existing chat based on chat type and message properties.
|
|
89
|
-
* Different chat types require different processing steps.
|
|
90
|
-
*
|
|
91
|
-
* @param {Context} ctx - The context of the incoming update
|
|
92
|
-
* @returns {Promise<void>}
|
|
93
|
-
* @private
|
|
94
|
-
*/
|
|
95
|
-
private processExistingChat;
|
|
96
|
-
/**
|
|
97
|
-
* Sets up message and reaction handlers for the bot.
|
|
98
|
-
* Configures event handlers to process incoming messages and reactions.
|
|
99
|
-
*
|
|
100
|
-
* @private
|
|
101
|
-
*/
|
|
102
|
-
private setupMessageHandlers;
|
|
103
|
-
/**
|
|
104
|
-
* Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting.
|
|
105
|
-
* @param {Context} ctx - The context of the incoming update.
|
|
106
|
-
* @returns {Promise<boolean>} A Promise that resolves with a boolean indicating if the group is authorized.
|
|
107
|
-
*/
|
|
108
|
-
private isGroupAuthorized;
|
|
109
|
-
/**
|
|
110
|
-
* Synchronizes an entity from a message context with the runtime system.
|
|
111
|
-
* This method handles three cases:
|
|
112
|
-
* 1. Message sender - most common case
|
|
113
|
-
* 2. New chat member - when a user joins the chat
|
|
114
|
-
* 3. Left chat member - when a user leaves the chat
|
|
115
|
-
*
|
|
116
|
-
* @param {Context} ctx - The context of the incoming update
|
|
117
|
-
* @returns {Promise<void>}
|
|
118
|
-
* @private
|
|
119
|
-
*/
|
|
120
|
-
private syncEntity;
|
|
121
|
-
/**
|
|
122
|
-
* Synchronizes the message sender entity with the runtime system.
|
|
123
|
-
* This is the most common entity sync case.
|
|
124
|
-
*
|
|
125
|
-
* @param {Context} ctx - The context of the incoming update
|
|
126
|
-
* @param {UUID} worldId - The ID of the world
|
|
127
|
-
* @param {UUID} roomId - The ID of the room
|
|
128
|
-
* @param {string} chatId - The ID of the chat
|
|
129
|
-
* @returns {Promise<void>}
|
|
130
|
-
* @private
|
|
131
|
-
*/
|
|
132
|
-
private syncMessageSender;
|
|
133
|
-
/**
|
|
134
|
-
* Synchronizes a new chat member entity with the runtime system.
|
|
135
|
-
* Triggered when a user joins the chat.
|
|
136
|
-
*
|
|
137
|
-
* @param {Context} ctx - The context of the incoming update
|
|
138
|
-
* @param {UUID} worldId - The ID of the world
|
|
139
|
-
* @param {UUID} roomId - The ID of the room
|
|
140
|
-
* @param {string} chatId - The ID of the chat
|
|
141
|
-
* @returns {Promise<void>}
|
|
142
|
-
* @private
|
|
143
|
-
*/
|
|
144
|
-
private syncNewChatMember;
|
|
145
|
-
/**
|
|
146
|
-
* Updates entity status when a user leaves the chat.
|
|
147
|
-
*
|
|
148
|
-
* @param {Context} ctx - The context of the incoming update
|
|
149
|
-
* @returns {Promise<void>}
|
|
150
|
-
* @private
|
|
151
|
-
*/
|
|
152
|
-
private syncLeftChatMember;
|
|
153
|
-
/**
|
|
154
|
-
* Handles forum topics by creating appropriate rooms in the runtime system.
|
|
155
|
-
* This enables proper conversation management for Telegram's forum feature.
|
|
156
|
-
*
|
|
157
|
-
* @param {Context} ctx - The context of the incoming update
|
|
158
|
-
* @returns {Promise<void>}
|
|
159
|
-
* @private
|
|
160
|
-
*/
|
|
161
|
-
private handleForumTopic;
|
|
162
|
-
/**
|
|
163
|
-
* Builds entity for message sender
|
|
164
|
-
*/
|
|
165
|
-
private buildMsgSenderEntity;
|
|
166
|
-
/**
|
|
167
|
-
* Handles new chat discovery and emits WORLD_JOINED event.
|
|
168
|
-
* This is a critical function that ensures new chats are properly
|
|
169
|
-
* registered in the runtime system and appropriate events are emitted.
|
|
170
|
-
*
|
|
171
|
-
* @param {Context} ctx - The context of the incoming update
|
|
172
|
-
* @returns {Promise<void>}
|
|
173
|
-
* @private
|
|
174
|
-
*/
|
|
175
|
-
private handleNewChat;
|
|
176
|
-
/**
|
|
177
|
-
* Processes entities in batches to prevent overwhelming the system.
|
|
178
|
-
*
|
|
179
|
-
* @param {Entity[]} entities - The entities to process
|
|
180
|
-
* @param {UUID} roomId - The ID of the room to connect entities to
|
|
181
|
-
* @param {string} channelId - The channel ID
|
|
182
|
-
* @param {ChannelType} roomType - The type of the room
|
|
183
|
-
* @param {UUID} worldId - The ID of the world
|
|
184
|
-
* @returns {Promise<void>}
|
|
185
|
-
* @private
|
|
186
|
-
*/
|
|
187
|
-
private batchProcessEntities;
|
|
188
|
-
/**
|
|
189
|
-
* Gets chat title and channel type based on Telegram chat type.
|
|
190
|
-
* Maps Telegram-specific chat types to standardized system types.
|
|
191
|
-
*
|
|
192
|
-
* @param {any} chat - The Telegram chat object
|
|
193
|
-
* @returns {Object} Object containing chatTitle and channelType
|
|
194
|
-
* @private
|
|
195
|
-
*/
|
|
196
|
-
private getChatTypeInfo;
|
|
197
|
-
/**
|
|
198
|
-
* Builds standardized entity representations from Telegram chat data.
|
|
199
|
-
* Transforms Telegram-specific user data into system-standard Entity objects.
|
|
200
|
-
*
|
|
201
|
-
* @param {any} chat - The Telegram chat object
|
|
202
|
-
* @returns {Promise<Entity[]>} Array of standardized Entity objects
|
|
203
|
-
* @private
|
|
204
|
-
*/
|
|
205
|
-
private buildStandardizedEntities;
|
|
206
|
-
/**
|
|
207
|
-
* Extracts and builds the room object for a forum topic from a message context.
|
|
208
|
-
* This refactored method can be used both in middleware and when handling new chats.
|
|
209
|
-
*
|
|
210
|
-
* @param {Context} ctx - The context of the incoming update
|
|
211
|
-
* @param {UUID} worldId - The ID of the world the topic belongs to
|
|
212
|
-
* @returns {Promise<Room | null>} A Promise that resolves with the room or null if not a topic
|
|
213
|
-
* @private
|
|
214
|
-
*/
|
|
215
|
-
private buildForumTopicRoom;
|
|
216
|
-
static registerSendHandlers(runtime: IAgentRuntime, serviceInstance: TelegramService): void;
|
|
217
|
-
handleSendMessage(runtime: IAgentRuntime, target: TargetInfo, content: Content): Promise<void>;
|
|
218
|
-
}
|
package/dist/tests.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { type IAgentRuntime, type TestSuite } from '@elizaos/core';
|
|
2
|
-
import type { Context } from 'telegraf';
|
|
3
|
-
/**
|
|
4
|
-
* Represents a test suite for testing Telegram functionality.
|
|
5
|
-
*
|
|
6
|
-
* This test suite includes methods to initialize and validate a Telegram bot connection,
|
|
7
|
-
* send basic text messages to a Telegram chat, send text messages with image attachments,
|
|
8
|
-
* handle and process incoming Telegram messages, and process and validate image attachments
|
|
9
|
-
* in incoming messages.
|
|
10
|
-
*
|
|
11
|
-
* @implements {TestSuite}
|
|
12
|
-
*/
|
|
13
|
-
export declare class TelegramTestSuite implements TestSuite {
|
|
14
|
-
name: string;
|
|
15
|
-
private telegramClient;
|
|
16
|
-
private bot;
|
|
17
|
-
private messageManager;
|
|
18
|
-
tests: {
|
|
19
|
-
name: string;
|
|
20
|
-
fn: (runtime: IAgentRuntime) => Promise<void>;
|
|
21
|
-
}[];
|
|
22
|
-
/**
|
|
23
|
-
* Constructor for initializing a set of test cases for a Telegram bot.
|
|
24
|
-
*
|
|
25
|
-
* @constructor
|
|
26
|
-
* @property {Array<Object>} tests - An array of test cases with name and corresponding test functions.
|
|
27
|
-
* @property {string} tests.name - The name of the test case.
|
|
28
|
-
* @property {function} tests.fn - The test function to be executed.
|
|
29
|
-
*/
|
|
30
|
-
constructor();
|
|
31
|
-
/**
|
|
32
|
-
* Retrieves the Telegram test chat ID from environment variables.
|
|
33
|
-
*
|
|
34
|
-
* Reference on getting the Telegram chat ID:
|
|
35
|
-
* https://stackoverflow.com/a/32572159
|
|
36
|
-
*/
|
|
37
|
-
/**
|
|
38
|
-
* Validates the chat ID by checking if it is set in the runtime settings or environment variables.
|
|
39
|
-
* If not set, an error is thrown with a message instructing to provide a valid chat ID.
|
|
40
|
-
* @param {IAgentRuntime} runtime - The runtime object that provides access to the settings and environment variables.
|
|
41
|
-
* @throws {Error} If TELEGRAM_TEST_CHAT_ID is not set in the runtime settings or environment variables.
|
|
42
|
-
* @returns {string} The validated chat ID.
|
|
43
|
-
*/
|
|
44
|
-
validateChatId(runtime: IAgentRuntime): string | number;
|
|
45
|
-
getChatInfo(runtime: IAgentRuntime): Promise<Context['chat']>;
|
|
46
|
-
testCreatingTelegramBot(runtime: IAgentRuntime): Promise<void>;
|
|
47
|
-
testSendingTextMessage(runtime: IAgentRuntime): Promise<void>;
|
|
48
|
-
testSendingMessageWithAttachment(runtime: IAgentRuntime): Promise<void>;
|
|
49
|
-
testHandlingMessage(runtime: IAgentRuntime): Promise<void>;
|
|
50
|
-
testProcessingImages(runtime: IAgentRuntime): Promise<void>;
|
|
51
|
-
getFileId(chatId: string, imageUrl: string): Promise<string>;
|
|
52
|
-
}
|