(target: any)
| 238 | let id: any; |
| 239 | let entity: any; |
| 240 | |
| 241 | try { |
| 242 | // 检查是否是邀请链接 |
| 243 | const inviteLinkMatch = typeof target === 'string' |
| 244 | ? target.match(/(?:https?:\/\/)?t\.me\/(?:\+|joinchat\/)([a-zA-Z0-9_-]+)/) |
| 245 | : null; |
| 246 | |
| 247 | if (inviteLinkMatch) { |
| 248 | // 处理邀请链接 |
| 249 | const hash = inviteLinkMatch[1]; |
| 250 | |
| 251 | try { |
| 252 | // 先检查邀请链接信息 |
| 253 | const inviteInfo = await client.invoke( |
| 254 | new Api.messages.CheckChatInvite({ hash }) |
| 255 | ); |
| 256 | |
| 257 | if (inviteInfo instanceof Api.ChatInviteAlready) { |
| 258 | // 已经在群组中,直接使用返回的 chat 对象 |
| 259 | entity = inviteInfo.chat; |
| 260 | id = entity?.id; |
| 261 | } else if (inviteInfo instanceof Api.ChatInvite) { |
| 262 | // 还未加入群组,需要先加入 |
| 263 | const importResult = await client.invoke( |
| 264 | new Api.messages.ImportChatInvite({ hash }) |
| 265 | ); |
| 266 | |
| 267 | // 从导入结果中获取 chat 对象 |
| 268 | // importResult 类型为 unknown,使用类型守卫安全访问 |
| 269 | if (importResult && typeof importResult === 'object' && 'chats' in importResult) { |
| 270 | const chats = (importResult as Record<string, unknown>).chats; |
| 271 | if (Array.isArray(chats) && chats.length > 0) { |
| 272 | entity = chats[0] as typeof entity; |
| 273 | id = entity?.id; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } catch (inviteError: any) { |
| 278 | console.error("处理邀请链接失败:", inviteError); |
| 279 | throw new Error(`无法处理邀请链接: ${inviteError.message || "未知错误"}`); |
| 280 | } |
| 281 | } else { |
| 282 | // 普通的 username 或 ID,直接获取 entity |
| 283 | entity = await client.getEntity(target); |
| 284 | id = entity?.id; |
| 285 | } |
| 286 | } catch (e: any) { |
| 287 | console.error(e); |
| 288 | throw new Error(`无法获取群组信息: ${e.message || "未知错误"}`); |
| 289 | } |
| 290 | |
| 291 | const displayParts: string[] = []; |
| 292 | if (entity?.title) displayParts.push(htmlEscape(entity.title)); |
| 293 | if (entity?.username) displayParts.push(htmlEscape(`@${entity.username}`)); |
| 294 | if (id) displayParts.push(codeTag(id)); |
| 295 | |
| 296 | return { |
| 297 | id, |
no test coverage detected