| 203 | } |
| 204 | |
| 205 | // 2. 处理私有邀请链接 https://t.me/+xxxxx 或 https://t.me/joinchat/xxxxx |
| 206 | // 这种格式需要特殊处理,保留完整链接 |
| 207 | const inviteLinkMatch = input.match(/(?:https?:\/\/)?t\.me\/(?:\+|joinchat\/)([a-zA-Z0-9_-]+)/); |
| 208 | if (inviteLinkMatch) { |
| 209 | return input; // 返回完整链接,让 formatEntity 特殊处理 |
| 210 | } |
| 211 | |
| 212 | // 3. 处理 t.me 公开群组/频道链接 |
| 213 | // https://t.me/groupname 或 t.me/groupname |
| 214 | const publicLinkMatch = input.match(/(?:https?:\/\/)?t\.me\/([a-zA-Z0-9_]+)/); |
| 215 | if (publicLinkMatch) { |
| 216 | return publicLinkMatch[1]; // 返回用户名 |
| 217 | } |
| 218 | |
| 219 | // 4. 处理私有群组链接 https://t.me/c/1234567890/xxx |
| 220 | const privateLinkMatch = input.match(/(?:https?:\/\/)?t\.me\/c\/(\d+)/); |
| 221 | if (privateLinkMatch) { |
| 222 | return `-100${privateLinkMatch[1]}`; // 转换为完整ID |
| 223 | } |
| 224 | |
| 225 | // 5. 处理 @username 格式 |
| 226 | if (input.startsWith('@')) { |
| 227 | return input.substring(1); // 移除 @ 符号 |
| 228 | } |
| 229 | |
| 230 | // 6. 其他情况直接返回原值,让 formatEntity 处理 |
| 231 | return input; |
| 232 | } |
| 233 | |
| 234 | async function formatEntity(target: any) { |
| 235 | const client = await getGlobalClient(); |
| 236 | if (!client) throw new Error("Telegram 客户端未初始化"); |
| 237 | |
| 238 | let id: any; |
| 239 | let entity: any; |
| 240 | |
| 241 | try { |