MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / resolveChatEntity

Function resolveChatEntity

dme/dme.ts:455–529  ·  view source on GitHub ↗

* 通用实体解析器 - 增强版本

(client: TelegramClient, chatId: string)

Source from the content-addressed store, hash-verified

453 const entity = await getEntityWithHash(client, chatId);
454 console.log(`[DME] getEntityWithHash 解析成功:`, entity.className);
455 return entity;
456 } catch (hashError: any) {
457 console.log(`[DME] getEntityWithHash 解析失败,继续多策略回退:`, hashError?.message || hashError);
458 }
459
460 // 策略2: 直接使用原始chatId尝试
461 try {
462 const entity = await getEntityWithRetry(client, chatId);
463 console.log(`[DME] 直接解析成功:`, entity.className);
464 return entity;
465 } catch (directError: any) {
466 console.log(`[DME] 直接解析失败:`, directError.message);
467 }
468
469 // 策略2: 尝试标准化群组ID格式
470 const normalizedIds = [chatId];
471
472 // 如果是负数ID,尝试不同格式
473 if (chatId.startsWith("-100")) {
474 normalizedIds.push(chatId.substring(4));
475 normalizedIds.push(chatId.substring(1));
476 } else if (chatId.startsWith("-")) {
477 normalizedIds.push(chatId.substring(1));
478 normalizedIds.push(`-100${chatId.substring(1)}`);
479 } else {
480 normalizedIds.push(`-100${chatId}`);
481 normalizedIds.push(`-${chatId}`);
482 }
483
484 // 尝试所有可能的ID格式
485 for (const normalizedId of normalizedIds) {
486 if (normalizedId === chatId) continue;
487
488 try {
489 console.log(`[DME] 尝试标准化ID: ${normalizedId}`);
490 const entity = await getEntityWithRetry(client, normalizedId);
491 console.log(`[DME] 标准化ID解析成功:`, entity.className);
492 return entity;
493 } catch (normalizedError: any) {
494 console.log(`[DME] 标准化ID ${normalizedId} 解析失败:`, normalizedError.message);
495 }
496 }
497
498 // 策略3: 通过对话列表查找匹配的实体
499 try {
500 console.log(`[DME] 尝试通过对话列表查找实体`);
501 const [defaultDialogs, archivedDialogs] = await Promise.all([
502 client.getDialogs({}),
503 client.getDialogs({ folder: 1 }).catch(() => [] as any[]),
504 ]);
505 const dialogs = [...defaultDialogs, ...archivedDialogs];
506 for (const dialog of dialogs) {
507 const dialogId = dialog.id?.toString() || "";
508 const dialogEntity = dialog.entity;
509
510 if (dialogId === chatId ||
511 dialogId === chatId.replace("-100", "") ||
512 dialogId === chatId.replace("-", "")) {

Callers

nothing calls this directly

Calls 1

getEntityWithRetryFunction · 0.85

Tested by

no test coverage detected