@elizaos/plugin-telegram 1.0.0-alpha.20 → 1.0.0-alpha.22
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 +409 -87
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import {
|
|
3
|
+
ChannelType as ChannelType2,
|
|
4
|
+
EventTypes as EventTypes2,
|
|
5
|
+
Role,
|
|
3
6
|
Service,
|
|
7
|
+
createUniqueUuid as createUniqueUuid2,
|
|
4
8
|
logger as logger3
|
|
5
9
|
} from "@elizaos/core";
|
|
6
10
|
import { Telegraf } from "telegraf";
|
|
@@ -4088,8 +4092,8 @@ ${errorMessages}`
|
|
|
4088
4092
|
// src/messageManager.ts
|
|
4089
4093
|
import {
|
|
4090
4094
|
ChannelType,
|
|
4095
|
+
EventTypes,
|
|
4091
4096
|
ModelTypes,
|
|
4092
|
-
Role,
|
|
4093
4097
|
createUniqueUuid,
|
|
4094
4098
|
logger
|
|
4095
4099
|
} from "@elizaos/core";
|
|
@@ -4322,28 +4326,11 @@ ${description}]` };
|
|
|
4322
4326
|
}
|
|
4323
4327
|
const fullText = imageInfo ? `${messageText} ${imageInfo.description}` : messageText;
|
|
4324
4328
|
if (!fullText) return;
|
|
4325
|
-
const memory = {
|
|
4326
|
-
id: messageId,
|
|
4327
|
-
entityId,
|
|
4328
|
-
agentId: this.runtime.agentId,
|
|
4329
|
-
roomId,
|
|
4330
|
-
content: {
|
|
4331
|
-
text: fullText,
|
|
4332
|
-
source: "telegram",
|
|
4333
|
-
channelType: getChannelType(message.chat),
|
|
4334
|
-
// name: userName,
|
|
4335
|
-
// userName: userName,
|
|
4336
|
-
// Safely access reply_to_message with type guard
|
|
4337
|
-
inReplyTo: "reply_to_message" in message && message.reply_to_message ? createUniqueUuid(
|
|
4338
|
-
this.runtime,
|
|
4339
|
-
message.reply_to_message.message_id.toString()
|
|
4340
|
-
) : void 0
|
|
4341
|
-
},
|
|
4342
|
-
createdAt: message.date * 1e3
|
|
4343
|
-
};
|
|
4344
4329
|
const chat = message.chat;
|
|
4345
|
-
const
|
|
4330
|
+
const channelType = getChannelType(chat);
|
|
4331
|
+
const worldName = chat.type === "supergroup" ? chat.title : chat.type === "channel" ? chat.title : chat.type === "private" ? `Chat with ${chat.first_name || "Unknown"}` : "Telegram";
|
|
4346
4332
|
const roomName = chat.type === "private" ? chat.first_name : chat.type === "supergroup" ? chat.title : chat.type === "channel" ? chat.title : chat.type === "group" ? chat.title : "Unknown Group";
|
|
4333
|
+
const worldId = createUniqueUuid(this.runtime, chat.id.toString());
|
|
4347
4334
|
await this.runtime.ensureConnection({
|
|
4348
4335
|
entityId,
|
|
4349
4336
|
roomId,
|
|
@@ -4352,10 +4339,9 @@ ${description}]` };
|
|
|
4352
4339
|
source: "telegram",
|
|
4353
4340
|
channelId: ctx.chat.id.toString(),
|
|
4354
4341
|
serverId: chat.id.toString(),
|
|
4355
|
-
type:
|
|
4342
|
+
type: channelType,
|
|
4343
|
+
worldId
|
|
4356
4344
|
});
|
|
4357
|
-
const channelType = getChannelType(chat);
|
|
4358
|
-
const worldId = createUniqueUuid(this.runtime, chat.id.toString());
|
|
4359
4345
|
const room = {
|
|
4360
4346
|
id: roomId,
|
|
4361
4347
|
name: roomName,
|
|
@@ -4365,24 +4351,23 @@ ${description}]` };
|
|
|
4365
4351
|
serverId: ctx.chat.id.toString(),
|
|
4366
4352
|
worldId
|
|
4367
4353
|
};
|
|
4368
|
-
const ownerId = chat.id;
|
|
4369
|
-
if (channelType === ChannelType.GROUP) {
|
|
4370
|
-
await this.runtime.ensureWorldExists({
|
|
4371
|
-
id: worldId,
|
|
4372
|
-
name: worldName,
|
|
4373
|
-
serverId: chat.id.toString(),
|
|
4374
|
-
agentId: this.runtime.agentId,
|
|
4375
|
-
metadata: {
|
|
4376
|
-
ownership: chat.type === "supergroup" ? { ownerId: chat.id.toString() } : void 0,
|
|
4377
|
-
roles: {
|
|
4378
|
-
// TODO: chat.id is probably wrong key for this
|
|
4379
|
-
[ownerId]: Role.OWNER
|
|
4380
|
-
}
|
|
4381
|
-
}
|
|
4382
|
-
});
|
|
4383
|
-
room.worldId = worldId;
|
|
4384
|
-
}
|
|
4385
4354
|
await this.runtime.ensureRoomExists(room);
|
|
4355
|
+
const memory = {
|
|
4356
|
+
id: messageId,
|
|
4357
|
+
entityId,
|
|
4358
|
+
agentId: this.runtime.agentId,
|
|
4359
|
+
roomId,
|
|
4360
|
+
content: {
|
|
4361
|
+
text: fullText,
|
|
4362
|
+
source: "telegram",
|
|
4363
|
+
channelType,
|
|
4364
|
+
inReplyTo: "reply_to_message" in message && message.reply_to_message ? createUniqueUuid(
|
|
4365
|
+
this.runtime,
|
|
4366
|
+
message.reply_to_message.message_id.toString()
|
|
4367
|
+
) : void 0
|
|
4368
|
+
},
|
|
4369
|
+
createdAt: message.date * 1e3
|
|
4370
|
+
};
|
|
4386
4371
|
const callback = async (content, _files) => {
|
|
4387
4372
|
try {
|
|
4388
4373
|
const sentMessages = await this.sendMessageInChunks(
|
|
@@ -4407,7 +4392,7 @@ ${description}]` };
|
|
|
4407
4392
|
...content,
|
|
4408
4393
|
text: sentMessage.text,
|
|
4409
4394
|
inReplyTo: messageId,
|
|
4410
|
-
channelType
|
|
4395
|
+
channelType
|
|
4411
4396
|
},
|
|
4412
4397
|
createdAt: sentMessage.date * 1e3
|
|
4413
4398
|
};
|
|
@@ -4421,11 +4406,23 @@ ${description}]` };
|
|
|
4421
4406
|
}
|
|
4422
4407
|
};
|
|
4423
4408
|
this.runtime.emitEvent(
|
|
4424
|
-
|
|
4409
|
+
EventTypes.MESSAGE_RECEIVED,
|
|
4425
4410
|
{
|
|
4426
4411
|
runtime: this.runtime,
|
|
4427
4412
|
message: memory,
|
|
4428
|
-
callback
|
|
4413
|
+
callback,
|
|
4414
|
+
source: "telegram"
|
|
4415
|
+
}
|
|
4416
|
+
);
|
|
4417
|
+
this.runtime.emitEvent(
|
|
4418
|
+
"TELEGRAM_MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */,
|
|
4419
|
+
{
|
|
4420
|
+
runtime: this.runtime,
|
|
4421
|
+
message: memory,
|
|
4422
|
+
callback,
|
|
4423
|
+
source: "telegram",
|
|
4424
|
+
ctx,
|
|
4425
|
+
originalMessage: message
|
|
4429
4426
|
}
|
|
4430
4427
|
);
|
|
4431
4428
|
} catch (error) {
|
|
@@ -4450,6 +4447,7 @@ ${description}]` };
|
|
|
4450
4447
|
ctx.from.id.toString()
|
|
4451
4448
|
);
|
|
4452
4449
|
const roomId = createUniqueUuid(this.runtime, ctx.chat.id.toString());
|
|
4450
|
+
const worldId = createUniqueUuid(this.runtime, ctx.chat.id.toString());
|
|
4453
4451
|
const reactionId = createUniqueUuid(
|
|
4454
4452
|
this.runtime,
|
|
4455
4453
|
`${reaction.message_id}-${ctx.from.id}-${Date.now()}`
|
|
@@ -4463,8 +4461,6 @@ ${description}]` };
|
|
|
4463
4461
|
channelType: getChannelType(reaction.chat),
|
|
4464
4462
|
text: `Reacted with: ${reactionType === "emoji" ? reactionEmoji : reactionType}`,
|
|
4465
4463
|
source: "telegram",
|
|
4466
|
-
// name: ctx.from.first_name,
|
|
4467
|
-
// userName: ctx.from.username,
|
|
4468
4464
|
inReplyTo: createUniqueUuid(
|
|
4469
4465
|
this.runtime,
|
|
4470
4466
|
reaction.message_id.toString()
|
|
@@ -4472,7 +4468,6 @@ ${description}]` };
|
|
|
4472
4468
|
},
|
|
4473
4469
|
createdAt: Date.now()
|
|
4474
4470
|
};
|
|
4475
|
-
await this.runtime.getMemoryManager("messages").createMemory(memory);
|
|
4476
4471
|
const callback = async (content) => {
|
|
4477
4472
|
try {
|
|
4478
4473
|
const sentMessage = await ctx.reply(content.text);
|
|
@@ -4497,17 +4492,94 @@ ${description}]` };
|
|
|
4497
4492
|
}
|
|
4498
4493
|
};
|
|
4499
4494
|
this.runtime.emitEvent(
|
|
4500
|
-
|
|
4495
|
+
EventTypes.REACTION_RECEIVED,
|
|
4501
4496
|
{
|
|
4502
4497
|
runtime: this.runtime,
|
|
4503
4498
|
message: memory,
|
|
4504
|
-
callback
|
|
4499
|
+
callback,
|
|
4500
|
+
source: "telegram"
|
|
4501
|
+
}
|
|
4502
|
+
);
|
|
4503
|
+
this.runtime.emitEvent(
|
|
4504
|
+
"TELEGRAM_REACTION_RECEIVED" /* REACTION_RECEIVED */,
|
|
4505
|
+
{
|
|
4506
|
+
runtime: this.runtime,
|
|
4507
|
+
message: memory,
|
|
4508
|
+
callback,
|
|
4509
|
+
source: "telegram",
|
|
4510
|
+
ctx,
|
|
4511
|
+
reactionString: reactionType === "emoji" ? reactionEmoji : reactionType,
|
|
4512
|
+
originalReaction: reaction.new_reaction[0]
|
|
4505
4513
|
}
|
|
4506
4514
|
);
|
|
4507
4515
|
} catch (error) {
|
|
4508
4516
|
logger.error("Error handling reaction:", error);
|
|
4509
4517
|
}
|
|
4510
4518
|
}
|
|
4519
|
+
/**
|
|
4520
|
+
* Sends a message to a Telegram chat and emits appropriate events
|
|
4521
|
+
* @param {number | string} chatId - The Telegram chat ID to send the message to
|
|
4522
|
+
* @param {Content} content - The content to send
|
|
4523
|
+
* @param {number} [replyToMessageId] - Optional message ID to reply to
|
|
4524
|
+
* @returns {Promise<Message.TextMessage[]>} The sent messages
|
|
4525
|
+
*/
|
|
4526
|
+
async sendMessage(chatId, content, replyToMessageId) {
|
|
4527
|
+
try {
|
|
4528
|
+
const ctx = {
|
|
4529
|
+
chat: { id: chatId },
|
|
4530
|
+
telegram: this.bot.telegram
|
|
4531
|
+
};
|
|
4532
|
+
const sentMessages = await this.sendMessageInChunks(
|
|
4533
|
+
ctx,
|
|
4534
|
+
content,
|
|
4535
|
+
replyToMessageId
|
|
4536
|
+
);
|
|
4537
|
+
if (!(sentMessages == null ? void 0 : sentMessages.length)) return [];
|
|
4538
|
+
const roomId = createUniqueUuid(this.runtime, chatId.toString());
|
|
4539
|
+
const memories = [];
|
|
4540
|
+
for (const sentMessage of sentMessages) {
|
|
4541
|
+
const memory = {
|
|
4542
|
+
id: createUniqueUuid(this.runtime, sentMessage.message_id.toString()),
|
|
4543
|
+
entityId: this.runtime.agentId,
|
|
4544
|
+
agentId: this.runtime.agentId,
|
|
4545
|
+
roomId,
|
|
4546
|
+
content: {
|
|
4547
|
+
...content,
|
|
4548
|
+
text: sentMessage.text,
|
|
4549
|
+
source: "telegram",
|
|
4550
|
+
channelType: getChannelType({
|
|
4551
|
+
id: typeof chatId === "string" ? Number.parseInt(chatId, 10) : chatId,
|
|
4552
|
+
type: "private"
|
|
4553
|
+
// Default to private, will be overridden if in context
|
|
4554
|
+
})
|
|
4555
|
+
},
|
|
4556
|
+
createdAt: sentMessage.date * 1e3
|
|
4557
|
+
};
|
|
4558
|
+
await this.runtime.getMemoryManager("messages").createMemory(memory);
|
|
4559
|
+
memories.push(memory);
|
|
4560
|
+
}
|
|
4561
|
+
this.runtime.emitEvent(
|
|
4562
|
+
EventTypes.MESSAGE_SENT,
|
|
4563
|
+
{
|
|
4564
|
+
runtime: this.runtime,
|
|
4565
|
+
messages: memories,
|
|
4566
|
+
roomId,
|
|
4567
|
+
source: "telegram"
|
|
4568
|
+
}
|
|
4569
|
+
);
|
|
4570
|
+
this.runtime.emitEvent(
|
|
4571
|
+
"TELEGRAM_MESSAGE_SENT" /* MESSAGE_SENT */,
|
|
4572
|
+
{
|
|
4573
|
+
originalMessages: sentMessages,
|
|
4574
|
+
chatId
|
|
4575
|
+
}
|
|
4576
|
+
);
|
|
4577
|
+
return sentMessages;
|
|
4578
|
+
} catch (error) {
|
|
4579
|
+
logger.error("Error sending message to Telegram:", error);
|
|
4580
|
+
return [];
|
|
4581
|
+
}
|
|
4582
|
+
}
|
|
4511
4583
|
};
|
|
4512
4584
|
|
|
4513
4585
|
// src/tests.ts
|
|
@@ -4704,6 +4776,7 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4704
4776
|
bot;
|
|
4705
4777
|
messageManager;
|
|
4706
4778
|
options;
|
|
4779
|
+
knownChats = /* @__PURE__ */ new Map();
|
|
4707
4780
|
/**
|
|
4708
4781
|
* Constructor for TelegramService class.
|
|
4709
4782
|
* @param {IAgentRuntime} runtime - The runtime object for the agent.
|
|
@@ -4729,19 +4802,32 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4729
4802
|
*/
|
|
4730
4803
|
static async start(runtime) {
|
|
4731
4804
|
await validateTelegramConfig(runtime);
|
|
4732
|
-
const
|
|
4733
|
-
|
|
4734
|
-
|
|
4735
|
-
)
|
|
4736
|
-
|
|
4737
|
-
|
|
4738
|
-
|
|
4739
|
-
|
|
4740
|
-
|
|
4741
|
-
|
|
4742
|
-
|
|
4805
|
+
const maxRetries = 5;
|
|
4806
|
+
let retryCount = 0;
|
|
4807
|
+
let lastError = null;
|
|
4808
|
+
while (retryCount < maxRetries) {
|
|
4809
|
+
try {
|
|
4810
|
+
const service = new _TelegramService(runtime);
|
|
4811
|
+
logger3.success(
|
|
4812
|
+
`\u2705 Telegram client successfully started for character ${runtime.character.name}`
|
|
4813
|
+
);
|
|
4814
|
+
logger3.log("\u{1F680} Starting Telegram bot...");
|
|
4815
|
+
await service.initializeBot();
|
|
4816
|
+
service.setupMessageHandlers();
|
|
4817
|
+
await service.bot.telegram.getMe();
|
|
4818
|
+
return service;
|
|
4819
|
+
} catch (error) {
|
|
4820
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
4821
|
+
logger3.error(`Telegram initialization attempt ${retryCount + 1} failed: ${lastError.message}`);
|
|
4822
|
+
retryCount++;
|
|
4823
|
+
if (retryCount < maxRetries) {
|
|
4824
|
+
const delay = 2 ** retryCount * 1e3;
|
|
4825
|
+
logger3.info(`Retrying Telegram initialization in ${delay / 1e3} seconds...`);
|
|
4826
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
4827
|
+
}
|
|
4828
|
+
}
|
|
4743
4829
|
}
|
|
4744
|
-
|
|
4830
|
+
throw new Error(`Telegram initialization failed after ${maxRetries} attempts. Last error: ${lastError == null ? void 0 : lastError.message}`);
|
|
4745
4831
|
}
|
|
4746
4832
|
/**
|
|
4747
4833
|
* Stops the agent runtime.
|
|
@@ -4770,46 +4856,168 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4770
4856
|
dropPendingUpdates: true,
|
|
4771
4857
|
allowedUpdates: ["message", "message_reaction"]
|
|
4772
4858
|
});
|
|
4773
|
-
logger3.log("\u2728 Telegram bot successfully launched and is running!");
|
|
4774
4859
|
const botInfo = await this.bot.telegram.getMe();
|
|
4775
|
-
|
|
4776
|
-
|
|
4777
|
-
|
|
4860
|
+
logger3.log(`Bot info: ${JSON.stringify(botInfo)}`);
|
|
4861
|
+
process.once("SIGINT", () => this.bot.stop("SIGINT"));
|
|
4862
|
+
process.once("SIGTERM", () => this.bot.stop("SIGTERM"));
|
|
4778
4863
|
}
|
|
4779
4864
|
/**
|
|
4780
|
-
* Checks if
|
|
4781
|
-
*
|
|
4782
|
-
* @
|
|
4783
|
-
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating if the group is authorized.
|
|
4865
|
+
* Checks if a group is authorized, based on the TELEGRAM_ALLOWED_CHATS setting.
|
|
4866
|
+
* @param {Context} ctx - The context of the incoming update.
|
|
4867
|
+
* @returns {Promise<boolean>} A Promise that resolves with a boolean indicating if the group is authorized.
|
|
4784
4868
|
*/
|
|
4785
4869
|
async isGroupAuthorized(ctx) {
|
|
4786
|
-
var _a
|
|
4787
|
-
const
|
|
4788
|
-
if (
|
|
4789
|
-
|
|
4870
|
+
var _a;
|
|
4871
|
+
const chatId = (_a = ctx.chat) == null ? void 0 : _a.id.toString();
|
|
4872
|
+
if (!chatId) return false;
|
|
4873
|
+
if (!this.knownChats.has(chatId)) {
|
|
4874
|
+
await this.handleNewChat(ctx);
|
|
4790
4875
|
}
|
|
4791
|
-
|
|
4876
|
+
const allowedChats = this.runtime.getSetting("TELEGRAM_ALLOWED_CHATS");
|
|
4877
|
+
if (!allowedChats) {
|
|
4792
4878
|
return true;
|
|
4793
4879
|
}
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
4880
|
+
try {
|
|
4881
|
+
const allowedChatsList = JSON.parse(allowedChats);
|
|
4882
|
+
return allowedChatsList.includes(chatId);
|
|
4883
|
+
} catch (error) {
|
|
4884
|
+
logger3.error("Error parsing TELEGRAM_ALLOWED_CHATS:", error);
|
|
4885
|
+
return false;
|
|
4886
|
+
}
|
|
4887
|
+
}
|
|
4888
|
+
/**
|
|
4889
|
+
* Handles new chat discovery and emits WORLD_JOINED event
|
|
4890
|
+
* @param {Context} ctx - The context of the incoming update
|
|
4891
|
+
*/
|
|
4892
|
+
async handleNewChat(ctx) {
|
|
4893
|
+
if (!ctx.chat) return;
|
|
4894
|
+
const chat = ctx.chat;
|
|
4895
|
+
const chatId = chat.id.toString();
|
|
4896
|
+
this.knownChats.set(chatId, chat);
|
|
4897
|
+
let chatTitle;
|
|
4898
|
+
let channelType;
|
|
4899
|
+
switch (chat.type) {
|
|
4900
|
+
case "private":
|
|
4901
|
+
chatTitle = `Chat with ${chat.first_name || "Unknown User"}`;
|
|
4902
|
+
channelType = ChannelType2.DM;
|
|
4903
|
+
break;
|
|
4904
|
+
case "group":
|
|
4905
|
+
chatTitle = chat.title || "Unknown Group";
|
|
4906
|
+
channelType = ChannelType2.GROUP;
|
|
4907
|
+
break;
|
|
4908
|
+
case "supergroup":
|
|
4909
|
+
chatTitle = chat.title || "Unknown Supergroup";
|
|
4910
|
+
channelType = ChannelType2.GROUP;
|
|
4911
|
+
break;
|
|
4912
|
+
case "channel":
|
|
4913
|
+
chatTitle = chat.title || "Unknown Channel";
|
|
4914
|
+
channelType = ChannelType2.FEED;
|
|
4915
|
+
break;
|
|
4916
|
+
default:
|
|
4917
|
+
chatTitle = "Unknown Chat";
|
|
4918
|
+
channelType = ChannelType2.GROUP;
|
|
4919
|
+
}
|
|
4920
|
+
const worldId = createUniqueUuid2(this.runtime, chatId);
|
|
4921
|
+
const roomId = createUniqueUuid2(this.runtime, chatId);
|
|
4922
|
+
const world = {
|
|
4923
|
+
id: worldId,
|
|
4924
|
+
name: chatTitle,
|
|
4925
|
+
agentId: this.runtime.agentId,
|
|
4926
|
+
serverId: chatId,
|
|
4927
|
+
metadata: {
|
|
4928
|
+
source: "telegram",
|
|
4929
|
+
ownership: { ownerId: chatId },
|
|
4930
|
+
roles: {
|
|
4931
|
+
[chatId]: Role.OWNER
|
|
4932
|
+
}
|
|
4933
|
+
}
|
|
4934
|
+
};
|
|
4935
|
+
const room = {
|
|
4936
|
+
id: roomId,
|
|
4937
|
+
name: chatTitle,
|
|
4938
|
+
source: "telegram",
|
|
4939
|
+
type: channelType,
|
|
4940
|
+
channelId: chatId,
|
|
4941
|
+
serverId: chatId,
|
|
4942
|
+
worldId
|
|
4943
|
+
};
|
|
4944
|
+
const users = [];
|
|
4945
|
+
if (chat.type === "private" && chat.id) {
|
|
4946
|
+
const userId = createUniqueUuid2(this.runtime, chat.id.toString());
|
|
4947
|
+
users.push({
|
|
4948
|
+
id: userId,
|
|
4949
|
+
names: [chat.first_name || "Unknown User"],
|
|
4950
|
+
agentId: this.runtime.agentId,
|
|
4951
|
+
metadata: {
|
|
4952
|
+
telegram: {
|
|
4953
|
+
id: chat.id.toString(),
|
|
4954
|
+
username: chat.username || "unknown",
|
|
4955
|
+
name: chat.first_name || "Unknown User"
|
|
4956
|
+
},
|
|
4957
|
+
source: "telegram"
|
|
4958
|
+
}
|
|
4959
|
+
});
|
|
4960
|
+
} else if (chat.type === "group" || chat.type === "supergroup") {
|
|
4798
4961
|
try {
|
|
4799
|
-
await
|
|
4800
|
-
|
|
4962
|
+
const admins = await this.bot.telegram.getChatAdministrators(chat.id);
|
|
4963
|
+
if (admins && admins.length > 0) {
|
|
4964
|
+
for (const admin of admins) {
|
|
4965
|
+
const userId = createUniqueUuid2(this.runtime, admin.user.id.toString());
|
|
4966
|
+
users.push({
|
|
4967
|
+
id: userId,
|
|
4968
|
+
names: [admin.user.first_name || admin.user.username || "Unknown Admin"],
|
|
4969
|
+
agentId: this.runtime.agentId,
|
|
4970
|
+
metadata: {
|
|
4971
|
+
telegram: {
|
|
4972
|
+
id: admin.user.id.toString(),
|
|
4973
|
+
username: admin.user.username || "unknown",
|
|
4974
|
+
name: admin.user.first_name || "Unknown Admin",
|
|
4975
|
+
isAdmin: true,
|
|
4976
|
+
adminTitle: admin.custom_title || (admin.status === "creator" ? "Owner" : "Admin")
|
|
4977
|
+
},
|
|
4978
|
+
source: "telegram",
|
|
4979
|
+
roles: [admin.status === "creator" ? Role.OWNER : Role.ADMIN]
|
|
4980
|
+
}
|
|
4981
|
+
});
|
|
4982
|
+
}
|
|
4983
|
+
}
|
|
4984
|
+
try {
|
|
4985
|
+
const chatInfo = await this.bot.telegram.getChat(chat.id);
|
|
4986
|
+
if (chatInfo && "member_count" in chatInfo) {
|
|
4987
|
+
world.metadata.memberCount = chatInfo.member_count;
|
|
4988
|
+
}
|
|
4989
|
+
} catch (countError) {
|
|
4990
|
+
logger3.warn(`Could not get member count for chat ${chatId}: ${countError}`);
|
|
4991
|
+
}
|
|
4801
4992
|
} catch (error) {
|
|
4802
|
-
logger3.error
|
|
4803
|
-
`Error leaving unauthorized group ${currentGroupId}:`,
|
|
4804
|
-
error
|
|
4805
|
-
);
|
|
4993
|
+
logger3.warn(`Could not fetch administrators for chat ${chatId}: ${error}`);
|
|
4806
4994
|
}
|
|
4807
|
-
return false;
|
|
4808
4995
|
}
|
|
4809
|
-
|
|
4996
|
+
const worldPayload = {
|
|
4997
|
+
runtime: this.runtime,
|
|
4998
|
+
world,
|
|
4999
|
+
rooms: [room],
|
|
5000
|
+
entities: users,
|
|
5001
|
+
source: "telegram"
|
|
5002
|
+
};
|
|
5003
|
+
const telegramWorldPayload = {
|
|
5004
|
+
...worldPayload,
|
|
5005
|
+
chat
|
|
5006
|
+
};
|
|
5007
|
+
this.runtime.emitEvent(
|
|
5008
|
+
EventTypes2.WORLD_JOINED,
|
|
5009
|
+
worldPayload
|
|
5010
|
+
);
|
|
5011
|
+
this.runtime.emitEvent(
|
|
5012
|
+
"TELEGRAM_WORLD_JOINED" /* WORLD_JOINED */,
|
|
5013
|
+
telegramWorldPayload
|
|
5014
|
+
);
|
|
5015
|
+
if (chat.type === "group" || chat.type === "supergroup") {
|
|
5016
|
+
this.setupEntityTracking(chat.id);
|
|
5017
|
+
}
|
|
4810
5018
|
}
|
|
4811
5019
|
/**
|
|
4812
|
-
*
|
|
5020
|
+
* Sets up message and reaction handlers for the bot.
|
|
4813
5021
|
*
|
|
4814
5022
|
* @private
|
|
4815
5023
|
* @returns {void}
|
|
@@ -4832,6 +5040,120 @@ var TelegramService = class _TelegramService extends Service {
|
|
|
4832
5040
|
}
|
|
4833
5041
|
});
|
|
4834
5042
|
}
|
|
5043
|
+
/**
|
|
5044
|
+
* Sets up tracking for new entities in a group chat to sync them as entities
|
|
5045
|
+
* @param {number} chatId - The Telegram chat ID to track entities for
|
|
5046
|
+
*/
|
|
5047
|
+
setupEntityTracking(chatId) {
|
|
5048
|
+
const syncedEntityIds = /* @__PURE__ */ new Set();
|
|
5049
|
+
this.bot.on("message", async (ctx) => {
|
|
5050
|
+
if (!ctx.chat || ctx.chat.id !== chatId || !ctx.from) return;
|
|
5051
|
+
const entityId = ctx.from.id.toString();
|
|
5052
|
+
if (syncedEntityIds.has(entityId)) return;
|
|
5053
|
+
syncedEntityIds.add(entityId);
|
|
5054
|
+
const entityUuid = createUniqueUuid2(this.runtime, entityId);
|
|
5055
|
+
const worldId = createUniqueUuid2(this.runtime, chatId.toString());
|
|
5056
|
+
const chatIdStr = chatId.toString();
|
|
5057
|
+
try {
|
|
5058
|
+
await this.runtime.ensureConnection({
|
|
5059
|
+
entityId: entityUuid,
|
|
5060
|
+
roomId: createUniqueUuid2(this.runtime, chatIdStr),
|
|
5061
|
+
userName: ctx.from.username || ctx.from.first_name || "Unknown Entity",
|
|
5062
|
+
name: ctx.from.first_name || ctx.from.username || "Unknown Entity",
|
|
5063
|
+
source: "telegram",
|
|
5064
|
+
channelId: chatIdStr,
|
|
5065
|
+
serverId: chatIdStr,
|
|
5066
|
+
type: ChannelType2.GROUP,
|
|
5067
|
+
worldId
|
|
5068
|
+
});
|
|
5069
|
+
const entityJoinedPayload = {
|
|
5070
|
+
runtime: this.runtime,
|
|
5071
|
+
entityId: entityUuid,
|
|
5072
|
+
entity: {
|
|
5073
|
+
id: entityId,
|
|
5074
|
+
username: ctx.from.username || ctx.from.first_name || "Unknown Entity",
|
|
5075
|
+
displayName: ctx.from.first_name || ctx.from.username || "Unknown Entity"
|
|
5076
|
+
},
|
|
5077
|
+
worldId,
|
|
5078
|
+
source: "telegram",
|
|
5079
|
+
metadata: {
|
|
5080
|
+
joinedAt: Date.now()
|
|
5081
|
+
}
|
|
5082
|
+
};
|
|
5083
|
+
const telegramEntityJoinedPayload = {
|
|
5084
|
+
...entityJoinedPayload,
|
|
5085
|
+
telegramUser: {
|
|
5086
|
+
id: ctx.from.id,
|
|
5087
|
+
username: ctx.from.username,
|
|
5088
|
+
first_name: ctx.from.first_name
|
|
5089
|
+
}
|
|
5090
|
+
};
|
|
5091
|
+
this.runtime.emitEvent(
|
|
5092
|
+
EventTypes2.ENTITY_JOINED,
|
|
5093
|
+
entityJoinedPayload
|
|
5094
|
+
);
|
|
5095
|
+
this.runtime.emitEvent(
|
|
5096
|
+
"TELEGRAM_ENTITY_JOINED" /* ENTITY_JOINED */,
|
|
5097
|
+
telegramEntityJoinedPayload
|
|
5098
|
+
);
|
|
5099
|
+
logger3.info(`Tracked new Telegram entity: ${ctx.from.username || ctx.from.first_name || entityId}`);
|
|
5100
|
+
} catch (error) {
|
|
5101
|
+
logger3.error(`Error syncing new Telegram entity ${entityId} from chat ${chatId}:`, error);
|
|
5102
|
+
}
|
|
5103
|
+
});
|
|
5104
|
+
this.bot.on("left_chat_member", async (ctx) => {
|
|
5105
|
+
var _a, _b;
|
|
5106
|
+
if (!((_a = ctx.message) == null ? void 0 : _a.left_chat_member) || ((_b = ctx.chat) == null ? void 0 : _b.id) !== chatId) return;
|
|
5107
|
+
const leftUser = ctx.message.left_chat_member;
|
|
5108
|
+
const entityId = createUniqueUuid2(this.runtime, leftUser.id.toString());
|
|
5109
|
+
const chatIdStr = chatId.toString();
|
|
5110
|
+
const worldId = createUniqueUuid2(this.runtime, chatIdStr);
|
|
5111
|
+
try {
|
|
5112
|
+
const entity = await this.runtime.getEntityById(entityId);
|
|
5113
|
+
if (entity) {
|
|
5114
|
+
entity.metadata = {
|
|
5115
|
+
...entity.metadata,
|
|
5116
|
+
status: "INACTIVE",
|
|
5117
|
+
leftAt: Date.now()
|
|
5118
|
+
};
|
|
5119
|
+
await this.runtime.updateEntity(entity);
|
|
5120
|
+
const entityLeftPayload = {
|
|
5121
|
+
runtime: this.runtime,
|
|
5122
|
+
entityId,
|
|
5123
|
+
entity: {
|
|
5124
|
+
id: leftUser.id.toString(),
|
|
5125
|
+
username: leftUser.username || leftUser.first_name || "Unknown Entity",
|
|
5126
|
+
displayName: leftUser.first_name || leftUser.username || "Unknown Entity"
|
|
5127
|
+
},
|
|
5128
|
+
worldId,
|
|
5129
|
+
source: "telegram",
|
|
5130
|
+
metadata: {
|
|
5131
|
+
leftAt: Date.now()
|
|
5132
|
+
}
|
|
5133
|
+
};
|
|
5134
|
+
const telegramEntityLeftPayload = {
|
|
5135
|
+
...entityLeftPayload,
|
|
5136
|
+
telegramUser: {
|
|
5137
|
+
id: leftUser.id,
|
|
5138
|
+
username: leftUser.username,
|
|
5139
|
+
first_name: leftUser.first_name
|
|
5140
|
+
}
|
|
5141
|
+
};
|
|
5142
|
+
this.runtime.emitEvent(
|
|
5143
|
+
EventTypes2.ENTITY_LEFT,
|
|
5144
|
+
entityLeftPayload
|
|
5145
|
+
);
|
|
5146
|
+
this.runtime.emitEvent(
|
|
5147
|
+
"TELEGRAM_ENTITY_LEFT" /* ENTITY_LEFT */,
|
|
5148
|
+
telegramEntityLeftPayload
|
|
5149
|
+
);
|
|
5150
|
+
logger3.info(`Entity ${leftUser.username || leftUser.first_name || leftUser.id} left chat ${chatId}`);
|
|
5151
|
+
}
|
|
5152
|
+
} catch (error) {
|
|
5153
|
+
logger3.error(`Error handling Telegram entity leaving chat ${chatId}:`, error);
|
|
5154
|
+
}
|
|
5155
|
+
});
|
|
5156
|
+
}
|
|
4835
5157
|
};
|
|
4836
5158
|
var telegramPlugin = {
|
|
4837
5159
|
name: TELEGRAM_SERVICE_NAME,
|