@elizaos/plugin-telegram 1.0.0-alpha.4 → 1.0.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +128 -22
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
- package/dist/index.d.ts +0 -33
package/dist/index.js
CHANGED
|
@@ -5,6 +5,20 @@ import {
|
|
|
5
5
|
} from "@elizaos/core";
|
|
6
6
|
import { Telegraf } from "telegraf";
|
|
7
7
|
|
|
8
|
+
// src/constants.ts
|
|
9
|
+
var MESSAGE_CONSTANTS = {
|
|
10
|
+
MAX_MESSAGES: 50,
|
|
11
|
+
RECENT_MESSAGE_COUNT: 5,
|
|
12
|
+
CHAT_HISTORY_COUNT: 10,
|
|
13
|
+
DEFAULT_SIMILARITY_THRESHOLD: 0.6,
|
|
14
|
+
DEFAULT_SIMILARITY_THRESHOLD_FOLLOW_UPS: 0.4,
|
|
15
|
+
INTEREST_DECAY_TIME: 5 * 60 * 1e3,
|
|
16
|
+
// 5 minutes
|
|
17
|
+
PARTIAL_INTEREST_DECAY: 3 * 60 * 1e3
|
|
18
|
+
// 3 minutes
|
|
19
|
+
};
|
|
20
|
+
var TELEGRAM_SERVICE_NAME = "telegram";
|
|
21
|
+
|
|
8
22
|
// ../../node_modules/zod/lib/index.mjs
|
|
9
23
|
var util;
|
|
10
24
|
(function(util2) {
|
|
@@ -4074,10 +4088,10 @@ ${errorMessages}`
|
|
|
4074
4088
|
// src/messageManager.ts
|
|
4075
4089
|
import {
|
|
4076
4090
|
ChannelType,
|
|
4077
|
-
createUniqueUuid,
|
|
4078
|
-
logger,
|
|
4079
4091
|
ModelTypes,
|
|
4080
|
-
Role
|
|
4092
|
+
Role,
|
|
4093
|
+
createUniqueUuid,
|
|
4094
|
+
logger
|
|
4081
4095
|
} from "@elizaos/core";
|
|
4082
4096
|
|
|
4083
4097
|
// src/utils.ts
|
|
@@ -4106,11 +4120,23 @@ var getChannelType = (chat) => {
|
|
|
4106
4120
|
var MessageManager = class {
|
|
4107
4121
|
bot;
|
|
4108
4122
|
runtime;
|
|
4123
|
+
/**
|
|
4124
|
+
* Constructor for creating a new instance of a BotAgent.
|
|
4125
|
+
*
|
|
4126
|
+
* @param {Telegraf<Context>} bot - The Telegraf instance used for interacting with the bot platform.
|
|
4127
|
+
* @param {IAgentRuntime} runtime - The runtime environment for the agent.
|
|
4128
|
+
*/
|
|
4109
4129
|
constructor(bot, runtime) {
|
|
4110
4130
|
this.bot = bot;
|
|
4111
4131
|
this.runtime = runtime;
|
|
4112
4132
|
}
|
|
4113
4133
|
// Process image messages and generate descriptions
|
|
4134
|
+
/**
|
|
4135
|
+
* Process an image from a Telegram message to extract the image URL and description.
|
|
4136
|
+
*
|
|
4137
|
+
* @param {Message} message - The Telegram message object containing the image.
|
|
4138
|
+
* @returns {Promise<{ description: string } | null>} The description of the processed image or null if no image found.
|
|
4139
|
+
*/
|
|
4114
4140
|
async processImage(message) {
|
|
4115
4141
|
var _a, _b, _c;
|
|
4116
4142
|
try {
|
|
@@ -4140,6 +4166,14 @@ ${description}]` };
|
|
|
4140
4166
|
return null;
|
|
4141
4167
|
}
|
|
4142
4168
|
// Send long messages in chunks
|
|
4169
|
+
/**
|
|
4170
|
+
* Sends a message in chunks, handling attachments and splitting the message if necessary
|
|
4171
|
+
*
|
|
4172
|
+
* @param {Context} ctx - The context object representing the current state of the bot
|
|
4173
|
+
* @param {Content} content - The content of the message to be sent
|
|
4174
|
+
* @param {number} [replyToMessageId] - The ID of the message to reply to, if any
|
|
4175
|
+
* @returns {Promise<Message.TextMessage[]>} - An array of TextMessage objects representing the messages sent
|
|
4176
|
+
*/
|
|
4143
4177
|
async sendMessageInChunks(ctx, content, replyToMessageId) {
|
|
4144
4178
|
if (content.attachments && content.attachments.length > 0) {
|
|
4145
4179
|
content.attachments.map(async (attachment) => {
|
|
@@ -4187,6 +4221,16 @@ ${description}]` };
|
|
|
4187
4221
|
return sentMessages;
|
|
4188
4222
|
}
|
|
4189
4223
|
}
|
|
4224
|
+
/**
|
|
4225
|
+
* Sends media to a chat using the Telegram API.
|
|
4226
|
+
*
|
|
4227
|
+
* @param {Context} ctx - The context object containing information about the current chat.
|
|
4228
|
+
* @param {string} mediaPath - The path to the media to be sent, either a URL or a local file path.
|
|
4229
|
+
* @param {MediaType} type - The type of media being sent (PHOTO, VIDEO, DOCUMENT, AUDIO, or ANIMATION).
|
|
4230
|
+
* @param {string} [caption] - Optional caption for the media being sent.
|
|
4231
|
+
*
|
|
4232
|
+
* @returns {Promise<void>} A Promise that resolves when the media is successfully sent.
|
|
4233
|
+
*/
|
|
4190
4234
|
async sendMedia(ctx, mediaPath, type, caption) {
|
|
4191
4235
|
try {
|
|
4192
4236
|
const isUrl = /^(http|https):\/\//.test(mediaPath);
|
|
@@ -4226,6 +4270,12 @@ ${description}]` };
|
|
|
4226
4270
|
}
|
|
4227
4271
|
}
|
|
4228
4272
|
// Split message into smaller parts
|
|
4273
|
+
/**
|
|
4274
|
+
* Splits a given text into an array of strings based on the maximum message length.
|
|
4275
|
+
*
|
|
4276
|
+
* @param {string} text - The text to split into chunks.
|
|
4277
|
+
* @returns {string[]} An array of strings with each element representing a chunk of the original text.
|
|
4278
|
+
*/
|
|
4229
4279
|
splitMessage(text) {
|
|
4230
4280
|
const chunks = [];
|
|
4231
4281
|
let currentChunk = "";
|
|
@@ -4242,6 +4292,11 @@ ${description}]` };
|
|
|
4242
4292
|
return chunks;
|
|
4243
4293
|
}
|
|
4244
4294
|
// Main handler for incoming messages
|
|
4295
|
+
/**
|
|
4296
|
+
* Handle incoming messages from Telegram and process them accordingly.
|
|
4297
|
+
* @param {Context} ctx - The context object containing information about the message.
|
|
4298
|
+
* @returns {Promise<void>}
|
|
4299
|
+
*/
|
|
4245
4300
|
async handleMessage(ctx) {
|
|
4246
4301
|
var _a, _b;
|
|
4247
4302
|
if (!ctx.message || !ctx.from) return;
|
|
@@ -4379,6 +4434,11 @@ ${description}]` };
|
|
|
4379
4434
|
throw error;
|
|
4380
4435
|
}
|
|
4381
4436
|
}
|
|
4437
|
+
/**
|
|
4438
|
+
* Handles the reaction event triggered by a user reacting to a message.
|
|
4439
|
+
* * @param {NarrowedContext<Context<Update>, Update.MessageReactionUpdate>} ctx The context of the message reaction update
|
|
4440
|
+
* @returns {Promise<void>} A Promise that resolves when the reaction handling is complete
|
|
4441
|
+
*/
|
|
4382
4442
|
async handleReaction(ctx) {
|
|
4383
4443
|
if (!ctx.update.message_reaction || !ctx.from) return;
|
|
4384
4444
|
const reaction = ctx.update.message_reaction;
|
|
@@ -4459,6 +4519,14 @@ var TelegramTestSuite = class {
|
|
|
4459
4519
|
bot = null;
|
|
4460
4520
|
messageManager = null;
|
|
4461
4521
|
tests;
|
|
4522
|
+
/**
|
|
4523
|
+
* Constructor for initializing a set of test cases for a Telegram bot.
|
|
4524
|
+
*
|
|
4525
|
+
* @constructor
|
|
4526
|
+
* @property {Array<Object>} tests - An array of test cases with name and corresponding test functions.
|
|
4527
|
+
* @property {string} tests.name - The name of the test case.
|
|
4528
|
+
* @property {function} tests.fn - The test function to be executed.
|
|
4529
|
+
*/
|
|
4462
4530
|
constructor() {
|
|
4463
4531
|
this.tests = [
|
|
4464
4532
|
{
|
|
@@ -4489,6 +4557,13 @@ var TelegramTestSuite = class {
|
|
|
4489
4557
|
* Reference on getting the Telegram chat ID:
|
|
4490
4558
|
* https://stackoverflow.com/a/32572159
|
|
4491
4559
|
*/
|
|
4560
|
+
/**
|
|
4561
|
+
* Validates the chat ID by checking if it is set in the runtime settings or environment variables.
|
|
4562
|
+
* If not set, an error is thrown with a message instructing to provide a valid chat ID.
|
|
4563
|
+
* @param {IAgentRuntime} runtime - The runtime object that provides access to the settings and environment variables.
|
|
4564
|
+
* @throws {Error} If TELEGRAM_TEST_CHAT_ID is not set in the runtime settings or environment variables.
|
|
4565
|
+
* @returns {string} The validated chat ID.
|
|
4566
|
+
*/
|
|
4492
4567
|
validateChatId(runtime) {
|
|
4493
4568
|
const testChatId = runtime.getSetting("TELEGRAM_TEST_CHAT_ID") || process.env.TELEGRAM_TEST_CHAT_ID;
|
|
4494
4569
|
if (!testChatId) {
|
|
@@ -4531,13 +4606,17 @@ var TelegramTestSuite = class {
|
|
|
4531
4606
|
const chat = await this.getChatInfo(runtime);
|
|
4532
4607
|
const mockContext = {
|
|
4533
4608
|
chat,
|
|
4534
|
-
from: { id:
|
|
4609
|
+
from: { id: 123, username: "TestUser" },
|
|
4535
4610
|
telegram: this.bot.telegram
|
|
4536
4611
|
};
|
|
4537
4612
|
const messageContent = {
|
|
4538
4613
|
text: "Here is an image attachment:",
|
|
4539
4614
|
attachments: [
|
|
4540
4615
|
{
|
|
4616
|
+
id: "123",
|
|
4617
|
+
title: "Sample Image",
|
|
4618
|
+
source: TEST_IMAGE_URL,
|
|
4619
|
+
text: "Sample Image",
|
|
4541
4620
|
url: TEST_IMAGE_URL,
|
|
4542
4621
|
contentType: "image/png",
|
|
4543
4622
|
description: "Sample Image"
|
|
@@ -4561,12 +4640,19 @@ var TelegramTestSuite = class {
|
|
|
4561
4640
|
const chat = await this.getChatInfo(runtime);
|
|
4562
4641
|
const mockContext = {
|
|
4563
4642
|
chat,
|
|
4564
|
-
from: {
|
|
4643
|
+
from: {
|
|
4644
|
+
id: 123,
|
|
4645
|
+
username: "TestUser",
|
|
4646
|
+
is_bot: false,
|
|
4647
|
+
first_name: "Test",
|
|
4648
|
+
last_name: "User"
|
|
4649
|
+
},
|
|
4565
4650
|
message: {
|
|
4566
4651
|
message_id: void 0,
|
|
4567
4652
|
text: `@${(_a = this.bot.botInfo) == null ? void 0 : _a.username}! Hello!`,
|
|
4568
4653
|
date: Math.floor(Date.now() / 1e3),
|
|
4569
4654
|
chat
|
|
4655
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
4570
4656
|
},
|
|
4571
4657
|
telegram: this.bot.telegram
|
|
4572
4658
|
};
|
|
@@ -4586,13 +4672,12 @@ var TelegramTestSuite = class {
|
|
|
4586
4672
|
const fileId = await this.getFileId(chatId, TEST_IMAGE_URL);
|
|
4587
4673
|
const mockMessage = {
|
|
4588
4674
|
message_id: void 0,
|
|
4675
|
+
chat: { id: chatId },
|
|
4589
4676
|
date: Math.floor(Date.now() / 1e3),
|
|
4590
4677
|
photo: [{ file_id: fileId }],
|
|
4591
4678
|
text: `@${(_a = this.bot.botInfo) == null ? void 0 : _a.username}!`
|
|
4592
4679
|
};
|
|
4593
|
-
const { description } = await this.messageManager.processImage(
|
|
4594
|
-
mockMessage
|
|
4595
|
-
);
|
|
4680
|
+
const { description } = await this.messageManager.processImage(mockMessage);
|
|
4596
4681
|
if (!description) {
|
|
4597
4682
|
throw new Error("Error processing Telegram image");
|
|
4598
4683
|
}
|
|
@@ -4612,20 +4697,6 @@ var TelegramTestSuite = class {
|
|
|
4612
4697
|
}
|
|
4613
4698
|
};
|
|
4614
4699
|
|
|
4615
|
-
// src/constants.ts
|
|
4616
|
-
var MESSAGE_CONSTANTS = {
|
|
4617
|
-
MAX_MESSAGES: 50,
|
|
4618
|
-
RECENT_MESSAGE_COUNT: 5,
|
|
4619
|
-
CHAT_HISTORY_COUNT: 10,
|
|
4620
|
-
DEFAULT_SIMILARITY_THRESHOLD: 0.6,
|
|
4621
|
-
DEFAULT_SIMILARITY_THRESHOLD_FOLLOW_UPS: 0.4,
|
|
4622
|
-
INTEREST_DECAY_TIME: 5 * 60 * 1e3,
|
|
4623
|
-
// 5 minutes
|
|
4624
|
-
PARTIAL_INTEREST_DECAY: 3 * 60 * 1e3
|
|
4625
|
-
// 3 minutes
|
|
4626
|
-
};
|
|
4627
|
-
var TELEGRAM_SERVICE_NAME = "telegram";
|
|
4628
|
-
|
|
4629
4700
|
// src/index.ts
|
|
4630
4701
|
var TelegramService = class _TelegramService extends Service {
|
|
4631
4702
|
static serviceType = TELEGRAM_SERVICE_NAME;
|
|
@@ -4633,6 +4704,10 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4633
4704
|
bot;
|
|
4634
4705
|
messageManager;
|
|
4635
4706
|
options;
|
|
4707
|
+
/**
|
|
4708
|
+
* Constructor for TelegramService class.
|
|
4709
|
+
* @param {IAgentRuntime} runtime - The runtime object for the agent.
|
|
4710
|
+
*/
|
|
4636
4711
|
constructor(runtime) {
|
|
4637
4712
|
super(runtime);
|
|
4638
4713
|
logger3.log("\u{1F4F1} Constructing new TelegramService...");
|
|
@@ -4646,6 +4721,12 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4646
4721
|
this.messageManager = new MessageManager(this.bot, this.runtime);
|
|
4647
4722
|
logger3.log("\u2705 TelegramService constructor completed");
|
|
4648
4723
|
}
|
|
4724
|
+
/**
|
|
4725
|
+
* Starts the Telegram service for the given runtime.
|
|
4726
|
+
*
|
|
4727
|
+
* @param {IAgentRuntime} runtime - The agent runtime to start the Telegram service for.
|
|
4728
|
+
* @returns {Promise<TelegramService>} A promise that resolves with the initialized TelegramService.
|
|
4729
|
+
*/
|
|
4649
4730
|
static async start(runtime) {
|
|
4650
4731
|
await validateTelegramConfig(runtime);
|
|
4651
4732
|
const tg = new _TelegramService(runtime);
|
|
@@ -4662,15 +4743,28 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4662
4743
|
}
|
|
4663
4744
|
return tg;
|
|
4664
4745
|
}
|
|
4746
|
+
/**
|
|
4747
|
+
* Stops the agent runtime.
|
|
4748
|
+
* @param {IAgentRuntime} runtime - The agent runtime to stop
|
|
4749
|
+
*/
|
|
4665
4750
|
static async stop(runtime) {
|
|
4666
4751
|
const tgClient = runtime.getService(TELEGRAM_SERVICE_NAME);
|
|
4667
4752
|
if (tgClient) {
|
|
4668
4753
|
await tgClient.stop();
|
|
4669
4754
|
}
|
|
4670
4755
|
}
|
|
4756
|
+
/**
|
|
4757
|
+
* Asynchronously stops the bot.
|
|
4758
|
+
*
|
|
4759
|
+
* @returns A Promise that resolves once the bot has stopped.
|
|
4760
|
+
*/
|
|
4671
4761
|
async stop() {
|
|
4672
4762
|
this.bot.stop();
|
|
4673
4763
|
}
|
|
4764
|
+
/**
|
|
4765
|
+
* Initializes the Telegram bot by launching it, getting bot info, and setting up message manager.
|
|
4766
|
+
* @returns {Promise<void>} A Promise that resolves when the initialization is complete.
|
|
4767
|
+
*/
|
|
4674
4768
|
async initializeBot() {
|
|
4675
4769
|
this.bot.launch({
|
|
4676
4770
|
dropPendingUpdates: true,
|
|
@@ -4682,6 +4776,12 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4682
4776
|
logger3.success(`Bot username: @${botInfo.username}`);
|
|
4683
4777
|
this.messageManager.bot = this.bot;
|
|
4684
4778
|
}
|
|
4779
|
+
/**
|
|
4780
|
+
* Checks if the group is authorized based on the Telegram settings.
|
|
4781
|
+
*
|
|
4782
|
+
* @param {Context} ctx - The context object representing the incoming message.
|
|
4783
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating if the group is authorized.
|
|
4784
|
+
*/
|
|
4685
4785
|
async isGroupAuthorized(ctx) {
|
|
4686
4786
|
var _a, _b, _c;
|
|
4687
4787
|
const config = (_a = this.runtime.character.settings) == null ? void 0 : _a.telegram;
|
|
@@ -4708,6 +4808,12 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4708
4808
|
}
|
|
4709
4809
|
return true;
|
|
4710
4810
|
}
|
|
4811
|
+
/**
|
|
4812
|
+
* Set up message handlers for the bot.
|
|
4813
|
+
*
|
|
4814
|
+
* @private
|
|
4815
|
+
* @returns {void}
|
|
4816
|
+
*/
|
|
4711
4817
|
setupMessageHandlers() {
|
|
4712
4818
|
this.bot.on("message", async (ctx) => {
|
|
4713
4819
|
try {
|