( task: SummaryTask, messageData: MessageData[] )
| 666 | const doc = message.media.document; |
| 667 | // 从 attributes 中查找文件名 |
| 668 | if (doc.attributes && Array.isArray(doc.attributes)) { |
| 669 | for (const attr of doc.attributes) { |
| 670 | if (attr.className === "DocumentAttributeFilename" && attr.fileName) { |
| 671 | return attr.fileName; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | // 如果没有文件名,返回 MIME 类型 |
| 676 | if (doc.mimeType) { |
| 677 | return `[${doc.mimeType}]`; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // MessageMediaPhoto(图片) |
| 682 | if (message.media.className === "MessageMediaPhoto") { |
| 683 | return "[图片]"; |
| 684 | } |
| 685 | |
| 686 | // MessageMediaWebPage(网页预览) |
| 687 | if (message.media.className === "MessageMediaWebPage") { |
| 688 | return null; // 网页预览不作为文件处理 |
| 689 | } |
| 690 | |
| 691 | return null; |
| 692 | } |
| 693 | |
| 694 | // 获取群消息(按数量) |
| 695 | async function getGroupMessages( |
| 696 | chatId: string, |
| 697 | count: number, |
| 698 | ): Promise<MessageData[]> { |
| 699 | const client = await getGlobalClient(); |
| 700 | if (!client) throw new Error("Telegram 客户端未初始化"); |
| 701 | |
| 702 | const messages = await safeGetMessages(client, chatId, { limit: count }); |
| 703 | |
| 704 | // 获取群组 username(如果有) |
| 705 | let chatUsername: string | undefined; |
| 706 | try { |
| 707 | const entity = await client.getEntity(chatId); |
| 708 | chatUsername = (entity as any).username; |
| 709 | } catch (e) { |
| 710 | // 忽略错误,使用私有链接格式 |
| 711 | } |
| 712 | |
| 713 | const messageData: MessageData[] = []; |
| 714 | for (const msg of messages) { |
| 715 | const message = msg as any; |
| 716 | // 跳过完全没有内容的消息 |
| 717 | if (!message.message && !message.media) continue; |
| 718 | |
| 719 | const sender = |
| 720 | message.sender?.firstName || message.sender?.username || "未知用户"; |
| 721 | const time = formatDate(new Date(message.date * 1000)); |
| 722 | const link = buildMessageLink(chatId, message.id, chatUsername); |
| 723 | const urls = extractUrlsFromEntities(message); |
| 724 | |
| 725 | // 构建消息文本,包含文件信息 |
no test coverage detected