(msg: Api.Message, targetEntity: any, num: number, client: any)
| 193 | await msg.edit({ text: "🔍 正在查询消息历史...", parseMode: "html" }); |
| 194 | |
| 195 | // 格式化目标实体显示 |
| 196 | let targetDisplay = ""; |
| 197 | try { |
| 198 | const entity = await client.getEntity(targetEntity); |
| 199 | if (entity) { |
| 200 | const parts: string[] = []; |
| 201 | if (entity.title) parts.push(entity.title); |
| 202 | if (entity.firstName) parts.push(entity.firstName); |
| 203 | if (entity.lastName) parts.push(entity.lastName); |
| 204 | if (entity.username) parts.push(`@${entity.username}`); |
| 205 | targetDisplay = parts.join(" ") || targetEntity.toString(); |
| 206 | } else { |
| 207 | targetDisplay = targetEntity.toString(); |
| 208 | } |
| 209 | } catch (error) { |
| 210 | targetDisplay = targetEntity.toString(); |
| 211 | } |
| 212 | |
| 213 | // 获取聊天链接基础URL |
| 214 | let baseLinkUrl = ""; |
| 215 | try { |
| 216 | const chat = await client.getEntity(chatId); |
| 217 | if (chat.username) { |
| 218 | baseLinkUrl = `https://t.me/${chat.username}/`; |
| 219 | } else if (chat.megagroup) { |
| 220 | const chatIdStr = String(chatId).replace("-100", ""); |
| 221 | baseLinkUrl = `https://t.me/c/${chatIdStr}/`; |
| 222 | } |
| 223 | } catch (error) { |
| 224 | console.error("[HIS] Could not get chat entity for linking:", error); |
| 225 | } |
| 226 | |
| 227 | let count = 0; |
| 228 | const messages: string[] = []; |
| 229 | |
| 230 | try { |
| 231 | // 迭代消息 |
| 232 | const messageIterator = client.iterMessages(chatId, { |
| 233 | limit: num, |
| 234 | fromUser: targetEntity |
| 235 | }); |
| 236 | |
| 237 | for await (const message of messageIterator) { |
| 238 | count++; |
| 239 | let messageText = message.text || ""; |
| 240 | |
| 241 | // 处理媒体消息 |
| 242 | if (message.media) { |
| 243 | messageText = await this.processMediaMessage(message, messageText); |
| 244 | } |
| 245 | |
| 246 | // 处理服务消息 |
| 247 | if (message.className === "MessageService") { |
| 248 | const action = message.action; |
| 249 | if (action.className === "MessageActionPinMessage") { |
| 250 | const pinnedMessage = (action as any).message; |
| 251 | messageText = "[置顶消息] " + pinnedMessage; |
| 252 | } else if (action.className === "MessageActionChatEditTitle") { |
no test coverage detected