@framers/agentos-ext-telegram 1.1.0
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 +21 -0
- package/README.md +317 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +227 -0
- package/dist/index.js.map +1 -0
- package/dist/services/telegramBot.d.ts +167 -0
- package/dist/services/telegramBot.d.ts.map +1 -0
- package/dist/services/telegramBot.js +302 -0
- package/dist/services/telegramBot.js.map +1 -0
- package/dist/tools/getChatInfo.d.ts +77 -0
- package/dist/tools/getChatInfo.d.ts.map +1 -0
- package/dist/tools/getChatInfo.js +119 -0
- package/dist/tools/getChatInfo.js.map +1 -0
- package/dist/tools/handleCallback.d.ts +55 -0
- package/dist/tools/handleCallback.d.ts.map +1 -0
- package/dist/tools/handleCallback.js +78 -0
- package/dist/tools/handleCallback.js.map +1 -0
- package/dist/tools/manageGroup.d.ts +54 -0
- package/dist/tools/manageGroup.d.ts.map +1 -0
- package/dist/tools/manageGroup.js +105 -0
- package/dist/tools/manageGroup.js.map +1 -0
- package/dist/tools/sendDocument.d.ts +59 -0
- package/dist/tools/sendDocument.d.ts.map +1 -0
- package/dist/tools/sendDocument.js +86 -0
- package/dist/tools/sendDocument.js.map +1 -0
- package/dist/tools/sendMessage.d.ts +94 -0
- package/dist/tools/sendMessage.d.ts.map +1 -0
- package/dist/tools/sendMessage.js +142 -0
- package/dist/tools/sendMessage.js.map +1 -0
- package/dist/tools/sendPhoto.d.ts +54 -0
- package/dist/tools/sendPhoto.d.ts.map +1 -0
- package/dist/tools/sendPhoto.js +80 -0
- package/dist/tools/sendPhoto.js.map +1 -0
- package/manifest.json +176 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Frame.dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# Telegram Bot Extension for AgentOS
|
|
2
|
+
|
|
3
|
+
Professional Telegram Bot API integration for AgentOS agents.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 📤 **Message Sending**: Text, photos, documents with formatting
|
|
8
|
+
- 📊 **Group Management**: Pin messages, change settings (requires admin)
|
|
9
|
+
- 🔍 **Chat Information**: Get chat details and member counts
|
|
10
|
+
- 🎛️ **Inline Keyboards**: Handle callback queries from buttons
|
|
11
|
+
- 🔐 **Flexible Auth**: Multiple ways to provide bot token
|
|
12
|
+
- ⚡ **Rate Limiting**: Automatic queue management
|
|
13
|
+
- 🔄 **Webhook/Polling**: Both modes supported
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @framers/agentos-ext-telegram
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configuration
|
|
22
|
+
|
|
23
|
+
### Environment Variables
|
|
24
|
+
|
|
25
|
+
The extension supports multiple ways to provide your bot token:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# .env file
|
|
29
|
+
|
|
30
|
+
# Method 1: Default environment variable
|
|
31
|
+
TELEGRAM_BOT_TOKEN=your-bot-token-here
|
|
32
|
+
|
|
33
|
+
# Method 2: Custom environment variable
|
|
34
|
+
MY_TELEGRAM_TOKEN=your-bot-token-here
|
|
35
|
+
|
|
36
|
+
# Method 3: Multiple bot support
|
|
37
|
+
TELEGRAM_MAIN_BOT=token1
|
|
38
|
+
TELEGRAM_SUPPORT_BOT=token2
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Usage Examples
|
|
42
|
+
|
|
43
|
+
#### Basic Setup
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { createExtensionPack } from '@framers/agentos-ext-telegram';
|
|
47
|
+
|
|
48
|
+
// Auto-detect from environment
|
|
49
|
+
const telegramExt = createExtensionPack({
|
|
50
|
+
options: {
|
|
51
|
+
// Automatically reads TELEGRAM_BOT_TOKEN
|
|
52
|
+
},
|
|
53
|
+
logger: console
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Custom environment variable
|
|
57
|
+
const telegramExt = createExtensionPack({
|
|
58
|
+
options: {
|
|
59
|
+
botTokenEnv: 'MY_TELEGRAM_TOKEN'
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Direct token (not recommended for production)
|
|
64
|
+
const telegramExt = createExtensionPack({
|
|
65
|
+
options: {
|
|
66
|
+
botToken: 'your-token-here'
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Advanced Configuration
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const telegramExt = createExtensionPack({
|
|
75
|
+
options: {
|
|
76
|
+
// Webhook mode (for production)
|
|
77
|
+
webhookUrl: 'https://your-domain.com/telegram-webhook',
|
|
78
|
+
|
|
79
|
+
// Or polling mode (for development)
|
|
80
|
+
pollingInterval: 300,
|
|
81
|
+
|
|
82
|
+
// Message formatting
|
|
83
|
+
defaultParseMode: 'Markdown', // or 'HTML'
|
|
84
|
+
enableTypingAction: true, // Show "typing..." indicator
|
|
85
|
+
|
|
86
|
+
// Rate limiting
|
|
87
|
+
rateLimit: {
|
|
88
|
+
maxRequests: 30, // Telegram's limit
|
|
89
|
+
windowMs: 1000 // Per second
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Available Tools
|
|
96
|
+
|
|
97
|
+
### 1. Send Message
|
|
98
|
+
|
|
99
|
+
Send formatted text messages with optional keyboards.
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
const result = await telegramSendMessage.execute({
|
|
103
|
+
chatId: '@mychannel', // or numeric chat ID
|
|
104
|
+
text: '*Bold* _italic_ `code`',
|
|
105
|
+
parseMode: 'Markdown',
|
|
106
|
+
replyMarkup: {
|
|
107
|
+
inline_keyboard: [[
|
|
108
|
+
{ text: 'Button 1', callback_data: 'btn1' },
|
|
109
|
+
{ text: 'Button 2', callback_data: 'btn2' }
|
|
110
|
+
]]
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 2. Send Photo
|
|
116
|
+
|
|
117
|
+
Send images with captions.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const result = await telegramSendPhoto.execute({
|
|
121
|
+
chatId: '@mychannel',
|
|
122
|
+
photo: 'https://example.com/image.jpg', // URL, file path, or base64
|
|
123
|
+
caption: 'Check out this image!',
|
|
124
|
+
parseMode: 'Markdown'
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 3. Send Document
|
|
129
|
+
|
|
130
|
+
Send files of any type.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
const result = await telegramSendDocument.execute({
|
|
134
|
+
chatId: '@mychannel',
|
|
135
|
+
document: './report.pdf', // URL, file path, or base64
|
|
136
|
+
caption: 'Monthly Report',
|
|
137
|
+
filename: 'Report_December_2024.pdf'
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 4. Get Chat Info
|
|
142
|
+
|
|
143
|
+
Retrieve information about a chat.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
const result = await telegramGetChatInfo.execute({
|
|
147
|
+
chatId: '@mychannel',
|
|
148
|
+
includeMemberCount: true
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Returns: { id, type, title, username, memberCount }
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 5. Manage Group
|
|
155
|
+
|
|
156
|
+
Perform admin actions (requires bot to be admin).
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
// Pin a message
|
|
160
|
+
await telegramManageGroup.execute({
|
|
161
|
+
chatId: -123456789,
|
|
162
|
+
action: 'pinMessage',
|
|
163
|
+
messageId: 42,
|
|
164
|
+
disableNotification: false
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Set group description
|
|
168
|
+
await telegramManageGroup.execute({
|
|
169
|
+
chatId: -123456789,
|
|
170
|
+
action: 'setDescription',
|
|
171
|
+
text: 'Welcome to our awesome group!'
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### 6. Handle Callbacks
|
|
176
|
+
|
|
177
|
+
Respond to inline keyboard button presses.
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
const result = await telegramHandleCallback.execute({
|
|
181
|
+
callbackQueryId: 'query123',
|
|
182
|
+
text: 'Button clicked!',
|
|
183
|
+
showAlert: true // Shows as popup
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Agency Integration
|
|
188
|
+
|
|
189
|
+
### Multi-GMI Collaboration
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// Research GMI finds information
|
|
193
|
+
const researcher = await createGMI({
|
|
194
|
+
tools: ['webSearch', 'factCheck']
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Communications GMI sends to Telegram
|
|
198
|
+
const communicator = await createGMI({
|
|
199
|
+
tools: ['telegramSendMessage', 'telegramSendPhoto']
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Workflow orchestrates both
|
|
203
|
+
const workflow = {
|
|
204
|
+
tasks: [
|
|
205
|
+
{
|
|
206
|
+
executor: 'researcher',
|
|
207
|
+
tool: 'webSearch',
|
|
208
|
+
inputs: { query: '{{topic}}' }
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
executor: 'communicator',
|
|
212
|
+
tool: 'telegramSendMessage',
|
|
213
|
+
inputs: {
|
|
214
|
+
chatId: '{{telegram_channel}}',
|
|
215
|
+
text: '📊 Research Results:\n{{results.research}}'
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
]
|
|
219
|
+
};
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Error Handling
|
|
223
|
+
|
|
224
|
+
The extension handles common Telegram API errors gracefully:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
try {
|
|
228
|
+
const result = await tool.execute(args);
|
|
229
|
+
if (!result.success) {
|
|
230
|
+
console.error('Telegram error:', result.error);
|
|
231
|
+
// Handle specific error codes
|
|
232
|
+
if (result.details?.code === 403) {
|
|
233
|
+
console.log('Bot was blocked by user');
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.error('System error:', error);
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Rate Limiting
|
|
242
|
+
|
|
243
|
+
The extension automatically manages rate limits:
|
|
244
|
+
|
|
245
|
+
- Queues messages to respect Telegram's 30 msg/sec limit
|
|
246
|
+
- Configurable rate limiting per chat
|
|
247
|
+
- Automatic retry with exponential backoff
|
|
248
|
+
|
|
249
|
+
## Security Best Practices
|
|
250
|
+
|
|
251
|
+
1. **Never hardcode bot tokens** - Use environment variables
|
|
252
|
+
2. **Validate webhook signatures** in production
|
|
253
|
+
3. **Sanitize user input** before sending
|
|
254
|
+
4. **Use HTTPS** for webhooks
|
|
255
|
+
5. **Restrict bot permissions** to minimum needed
|
|
256
|
+
6. **Monitor for abuse** and implement user rate limits
|
|
257
|
+
|
|
258
|
+
## Bot Setup
|
|
259
|
+
|
|
260
|
+
1. Create a bot with [@BotFather](https://t.me/botfather)
|
|
261
|
+
2. Get your bot token
|
|
262
|
+
3. Set bot commands (optional):
|
|
263
|
+
```
|
|
264
|
+
/start - Start the bot
|
|
265
|
+
/help - Show help
|
|
266
|
+
/status - Check status
|
|
267
|
+
```
|
|
268
|
+
4. Configure privacy mode as needed
|
|
269
|
+
5. Add bot to groups/channels with appropriate permissions
|
|
270
|
+
|
|
271
|
+
## Testing
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
// Mock the Telegram service for testing
|
|
275
|
+
const mockService = {
|
|
276
|
+
sendMessage: jest.fn().mockResolvedValue({
|
|
277
|
+
message_id: 1,
|
|
278
|
+
chat: { id: 123 },
|
|
279
|
+
date: Date.now(),
|
|
280
|
+
text: 'Test'
|
|
281
|
+
})
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const tool = new SendMessageTool(mockService);
|
|
285
|
+
const result = await tool.execute({
|
|
286
|
+
chatId: 123,
|
|
287
|
+
text: 'Test message'
|
|
288
|
+
}, context);
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Troubleshooting
|
|
292
|
+
|
|
293
|
+
### Bot not responding
|
|
294
|
+
- Check bot token is correct
|
|
295
|
+
- Ensure bot is not blocked
|
|
296
|
+
- Verify network connectivity
|
|
297
|
+
- Check webhook URL is accessible
|
|
298
|
+
|
|
299
|
+
### Rate limit errors
|
|
300
|
+
- Reduce message frequency
|
|
301
|
+
- Implement message batching
|
|
302
|
+
- Use queue management
|
|
303
|
+
|
|
304
|
+
### Permission errors
|
|
305
|
+
- Ensure bot is admin for group management
|
|
306
|
+
- Check channel posting permissions
|
|
307
|
+
- Verify user hasn't blocked bot
|
|
308
|
+
|
|
309
|
+
## Resources
|
|
310
|
+
|
|
311
|
+
- [Telegram Bot API Documentation](https://core.telegram.org/bots/api)
|
|
312
|
+
- [BotFather](https://t.me/botfather) - Create and manage bots
|
|
313
|
+
- [Telegram Bot Examples](https://github.com/telegram-bot-sdk/telegram-bot-sdk)
|
|
314
|
+
|
|
315
|
+
## License
|
|
316
|
+
|
|
317
|
+
MIT - See LICENSE file
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentOS Telegram Bot Extension
|
|
3
|
+
*
|
|
4
|
+
* Provides Telegram Bot API capabilities for AgentOS agents.
|
|
5
|
+
*
|
|
6
|
+
* @module @framers/agentos-ext-telegram
|
|
7
|
+
* @version 1.0.0
|
|
8
|
+
* @license MIT
|
|
9
|
+
*/
|
|
10
|
+
import { ExtensionContext, ExtensionPack } from '@framers/agentos';
|
|
11
|
+
import { SendMessageTool } from './tools/sendMessage';
|
|
12
|
+
import { SendPhotoTool } from './tools/sendPhoto';
|
|
13
|
+
import { SendDocumentTool } from './tools/sendDocument';
|
|
14
|
+
import { GetChatInfoTool } from './tools/getChatInfo';
|
|
15
|
+
import { ManageGroupTool } from './tools/manageGroup';
|
|
16
|
+
import { HandleCallbackTool } from './tools/handleCallback';
|
|
17
|
+
import { TelegramBotService, TelegramConfig } from './services/telegramBot';
|
|
18
|
+
/**
|
|
19
|
+
* Extension configuration options
|
|
20
|
+
*/
|
|
21
|
+
export interface TelegramExtensionOptions {
|
|
22
|
+
/** Telegram Bot Token - can be provided directly or via env */
|
|
23
|
+
botToken?: string;
|
|
24
|
+
/** Environment variable name for bot token (default: TELEGRAM_BOT_TOKEN) */
|
|
25
|
+
botTokenEnv?: string;
|
|
26
|
+
/** Webhook URL for receiving updates */
|
|
27
|
+
webhookUrl?: string;
|
|
28
|
+
/** Polling interval in ms (if not using webhook) */
|
|
29
|
+
pollingInterval?: number;
|
|
30
|
+
/** Default parse mode for messages (Markdown/HTML) */
|
|
31
|
+
defaultParseMode?: 'Markdown' | 'HTML';
|
|
32
|
+
/** Enable typing action before sending messages */
|
|
33
|
+
enableTypingAction?: boolean;
|
|
34
|
+
/** Rate limiting configuration */
|
|
35
|
+
rateLimit?: {
|
|
36
|
+
maxRequests: number;
|
|
37
|
+
windowMs: number;
|
|
38
|
+
};
|
|
39
|
+
/** Extension priority in the stack */
|
|
40
|
+
priority?: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates the Telegram extension pack
|
|
44
|
+
*
|
|
45
|
+
* @param {ExtensionContext} context - The extension context
|
|
46
|
+
* @returns {ExtensionPack} The configured extension pack
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* import { createExtensionPack } from '@framers/agentos-ext-telegram';
|
|
51
|
+
*
|
|
52
|
+
* // Using environment variable
|
|
53
|
+
* const pack = createExtensionPack({
|
|
54
|
+
* options: {
|
|
55
|
+
* // Will read from TELEGRAM_BOT_TOKEN env var
|
|
56
|
+
* },
|
|
57
|
+
* logger: console
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* // Using direct token
|
|
61
|
+
* const pack = createExtensionPack({
|
|
62
|
+
* options: {
|
|
63
|
+
* botToken: 'your-bot-token-here'
|
|
64
|
+
* },
|
|
65
|
+
* logger: console
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* // Using custom env var
|
|
69
|
+
* const pack = createExtensionPack({
|
|
70
|
+
* options: {
|
|
71
|
+
* botTokenEnv: 'MY_TELEGRAM_TOKEN'
|
|
72
|
+
* },
|
|
73
|
+
* logger: console
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function createExtensionPack(context: ExtensionContext): ExtensionPack;
|
|
78
|
+
export { SendMessageTool, SendPhotoTool, SendDocumentTool, GetChatInfoTool, ManageGroupTool, HandleCallbackTool, TelegramBotService, TelegramConfig };
|
|
79
|
+
export default createExtensionPack;
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAM5E;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,wCAAwC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oDAAoD;IACpD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,sDAAsD;IACtD,gBAAgB,CAAC,EAAE,UAAU,GAAG,MAAM,CAAC;IACvC,mDAAmD;IACnD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,kCAAkC;IAClC,SAAS,CAAC,EAAE;QACV,WAAW,EAAE,MAAM,CAAC;QACpB,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;IACF,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAkDD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,GAAG,aAAa,CA2F5E;AAGD,OAAO,EACL,eAAe,EACf,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACf,CAAC;AAGF,eAAe,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* AgentOS Telegram Bot Extension
|
|
4
|
+
*
|
|
5
|
+
* Provides Telegram Bot API capabilities for AgentOS agents.
|
|
6
|
+
*
|
|
7
|
+
* @module @framers/agentos-ext-telegram
|
|
8
|
+
* @version 1.0.0
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
14
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
15
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
16
|
+
}
|
|
17
|
+
Object.defineProperty(o, k2, desc);
|
|
18
|
+
}) : (function(o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
o[k2] = m[k];
|
|
21
|
+
}));
|
|
22
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
23
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
24
|
+
}) : function(o, v) {
|
|
25
|
+
o["default"] = v;
|
|
26
|
+
});
|
|
27
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
28
|
+
var ownKeys = function(o) {
|
|
29
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
30
|
+
var ar = [];
|
|
31
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
32
|
+
return ar;
|
|
33
|
+
};
|
|
34
|
+
return ownKeys(o);
|
|
35
|
+
};
|
|
36
|
+
return function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
40
|
+
__setModuleDefault(result, mod);
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
})();
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.TelegramBotService = exports.HandleCallbackTool = exports.ManageGroupTool = exports.GetChatInfoTool = exports.SendDocumentTool = exports.SendPhotoTool = exports.SendMessageTool = void 0;
|
|
46
|
+
exports.createExtensionPack = createExtensionPack;
|
|
47
|
+
const sendMessage_1 = require("./tools/sendMessage");
|
|
48
|
+
Object.defineProperty(exports, "SendMessageTool", { enumerable: true, get: function () { return sendMessage_1.SendMessageTool; } });
|
|
49
|
+
const sendPhoto_1 = require("./tools/sendPhoto");
|
|
50
|
+
Object.defineProperty(exports, "SendPhotoTool", { enumerable: true, get: function () { return sendPhoto_1.SendPhotoTool; } });
|
|
51
|
+
const sendDocument_1 = require("./tools/sendDocument");
|
|
52
|
+
Object.defineProperty(exports, "SendDocumentTool", { enumerable: true, get: function () { return sendDocument_1.SendDocumentTool; } });
|
|
53
|
+
const getChatInfo_1 = require("./tools/getChatInfo");
|
|
54
|
+
Object.defineProperty(exports, "GetChatInfoTool", { enumerable: true, get: function () { return getChatInfo_1.GetChatInfoTool; } });
|
|
55
|
+
const manageGroup_1 = require("./tools/manageGroup");
|
|
56
|
+
Object.defineProperty(exports, "ManageGroupTool", { enumerable: true, get: function () { return manageGroup_1.ManageGroupTool; } });
|
|
57
|
+
const handleCallback_1 = require("./tools/handleCallback");
|
|
58
|
+
Object.defineProperty(exports, "HandleCallbackTool", { enumerable: true, get: function () { return handleCallback_1.HandleCallbackTool; } });
|
|
59
|
+
const telegramBot_1 = require("./services/telegramBot");
|
|
60
|
+
Object.defineProperty(exports, "TelegramBotService", { enumerable: true, get: function () { return telegramBot_1.TelegramBotService; } });
|
|
61
|
+
const dotenv = __importStar(require("dotenv"));
|
|
62
|
+
// Load environment variables
|
|
63
|
+
dotenv.config();
|
|
64
|
+
/**
|
|
65
|
+
* Resolves the bot token from options or environment
|
|
66
|
+
*
|
|
67
|
+
* @private
|
|
68
|
+
* @param {TelegramExtensionOptions} options - Extension options
|
|
69
|
+
* @returns {string} The resolved bot token
|
|
70
|
+
* @throws {Error} If no bot token is found
|
|
71
|
+
*/
|
|
72
|
+
function resolveBotToken(options) {
|
|
73
|
+
// Priority order:
|
|
74
|
+
// 1. Direct botToken in options
|
|
75
|
+
// 2. Custom environment variable specified in options
|
|
76
|
+
// 3. Default TELEGRAM_BOT_TOKEN environment variable
|
|
77
|
+
// 4. Common variations of the env var name
|
|
78
|
+
if (options.botToken) {
|
|
79
|
+
return options.botToken;
|
|
80
|
+
}
|
|
81
|
+
const envName = options.botTokenEnv || 'TELEGRAM_BOT_TOKEN';
|
|
82
|
+
const envValue = process.env[envName];
|
|
83
|
+
if (envValue) {
|
|
84
|
+
return envValue;
|
|
85
|
+
}
|
|
86
|
+
// Try common variations
|
|
87
|
+
const variations = [
|
|
88
|
+
'TELEGRAM_BOT_TOKEN',
|
|
89
|
+
'TELEGRAM_TOKEN',
|
|
90
|
+
'BOT_TOKEN',
|
|
91
|
+
'TELEGRAM_API_KEY',
|
|
92
|
+
'TELEGRAM_KEY'
|
|
93
|
+
];
|
|
94
|
+
for (const varName of variations) {
|
|
95
|
+
const value = process.env[varName];
|
|
96
|
+
if (value) {
|
|
97
|
+
console.log(`Found Telegram bot token in ${varName}`);
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
throw new Error('Telegram bot token not found. Please provide it via options.botToken or set the TELEGRAM_BOT_TOKEN environment variable');
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates the Telegram extension pack
|
|
105
|
+
*
|
|
106
|
+
* @param {ExtensionContext} context - The extension context
|
|
107
|
+
* @returns {ExtensionPack} The configured extension pack
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* import { createExtensionPack } from '@framers/agentos-ext-telegram';
|
|
112
|
+
*
|
|
113
|
+
* // Using environment variable
|
|
114
|
+
* const pack = createExtensionPack({
|
|
115
|
+
* options: {
|
|
116
|
+
* // Will read from TELEGRAM_BOT_TOKEN env var
|
|
117
|
+
* },
|
|
118
|
+
* logger: console
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* // Using direct token
|
|
122
|
+
* const pack = createExtensionPack({
|
|
123
|
+
* options: {
|
|
124
|
+
* botToken: 'your-bot-token-here'
|
|
125
|
+
* },
|
|
126
|
+
* logger: console
|
|
127
|
+
* });
|
|
128
|
+
*
|
|
129
|
+
* // Using custom env var
|
|
130
|
+
* const pack = createExtensionPack({
|
|
131
|
+
* options: {
|
|
132
|
+
* botTokenEnv: 'MY_TELEGRAM_TOKEN'
|
|
133
|
+
* },
|
|
134
|
+
* logger: console
|
|
135
|
+
* });
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
function createExtensionPack(context) {
|
|
139
|
+
const options = context.options || {};
|
|
140
|
+
// Resolve bot token from various sources
|
|
141
|
+
const botToken = resolveBotToken(options);
|
|
142
|
+
// Initialize Telegram service with configuration
|
|
143
|
+
const telegramConfig = {
|
|
144
|
+
botToken,
|
|
145
|
+
webhookUrl: options.webhookUrl,
|
|
146
|
+
pollingInterval: options.pollingInterval,
|
|
147
|
+
defaultParseMode: options.defaultParseMode || 'Markdown',
|
|
148
|
+
enableTypingAction: options.enableTypingAction !== false,
|
|
149
|
+
rateLimit: options.rateLimit || {
|
|
150
|
+
maxRequests: 30,
|
|
151
|
+
windowMs: 1000 // Telegram's rate limit is ~30 msgs/sec
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const telegramService = new telegramBot_1.TelegramBotService(telegramConfig);
|
|
155
|
+
// Create tool instances
|
|
156
|
+
const sendMessageTool = new sendMessage_1.SendMessageTool(telegramService);
|
|
157
|
+
const sendPhotoTool = new sendPhoto_1.SendPhotoTool(telegramService);
|
|
158
|
+
const sendDocumentTool = new sendDocument_1.SendDocumentTool(telegramService);
|
|
159
|
+
const getChatInfoTool = new getChatInfo_1.GetChatInfoTool(telegramService);
|
|
160
|
+
const manageGroupTool = new manageGroup_1.ManageGroupTool(telegramService);
|
|
161
|
+
const handleCallbackTool = new handleCallback_1.HandleCallbackTool(telegramService);
|
|
162
|
+
return {
|
|
163
|
+
name: '@framers/agentos-ext-telegram',
|
|
164
|
+
version: '1.0.0',
|
|
165
|
+
descriptors: [
|
|
166
|
+
{
|
|
167
|
+
id: 'telegramSendMessage',
|
|
168
|
+
kind: 'tool',
|
|
169
|
+
priority: options.priority || 50,
|
|
170
|
+
payload: sendMessageTool
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
id: 'telegramSendPhoto',
|
|
174
|
+
kind: 'tool',
|
|
175
|
+
priority: options.priority || 50,
|
|
176
|
+
payload: sendPhotoTool
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
id: 'telegramSendDocument',
|
|
180
|
+
kind: 'tool',
|
|
181
|
+
priority: options.priority || 50,
|
|
182
|
+
payload: sendDocumentTool
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
id: 'telegramGetChatInfo',
|
|
186
|
+
kind: 'tool',
|
|
187
|
+
priority: options.priority || 50,
|
|
188
|
+
payload: getChatInfoTool
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
id: 'telegramManageGroup',
|
|
192
|
+
kind: 'tool',
|
|
193
|
+
priority: options.priority || 50,
|
|
194
|
+
payload: manageGroupTool
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
id: 'telegramHandleCallback',
|
|
198
|
+
kind: 'tool',
|
|
199
|
+
priority: options.priority || 50,
|
|
200
|
+
payload: handleCallbackTool
|
|
201
|
+
}
|
|
202
|
+
],
|
|
203
|
+
/**
|
|
204
|
+
* Called when extension is activated
|
|
205
|
+
*/
|
|
206
|
+
onActivate: async () => {
|
|
207
|
+
await telegramService.initialize();
|
|
208
|
+
if (context.onActivate) {
|
|
209
|
+
await context.onActivate();
|
|
210
|
+
}
|
|
211
|
+
context.logger?.info('Telegram Extension activated');
|
|
212
|
+
},
|
|
213
|
+
/**
|
|
214
|
+
* Called when extension is deactivated
|
|
215
|
+
*/
|
|
216
|
+
onDeactivate: async () => {
|
|
217
|
+
await telegramService.shutdown();
|
|
218
|
+
if (context.onDeactivate) {
|
|
219
|
+
await context.onDeactivate();
|
|
220
|
+
}
|
|
221
|
+
context.logger?.info('Telegram Extension deactivated');
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
// Default export for convenience
|
|
226
|
+
exports.default = createExtensionPack;
|
|
227
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2HH,kDA2FC;AAnND,qDAAsD;AAuNpD,gGAvNO,6BAAe,OAuNP;AAtNjB,iDAAkD;AAuNhD,8FAvNO,yBAAa,OAuNP;AAtNf,uDAAwD;AAuNtD,iGAvNO,+BAAgB,OAuNP;AAtNlB,qDAAsD;AAuNpD,gGAvNO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