( client: any, chatId: any, filePaths: string[], photoSetUrl?: string )
| 323 | } |
| 324 | |
| 325 | async function sendImageAlbum( |
| 326 | client: any, |
| 327 | chatId: any, |
| 328 | filePaths: string[], |
| 329 | photoSetUrl?: string |
| 330 | ): Promise<void> { |
| 331 | try { |
| 332 | const files = filePaths.map( |
| 333 | (filePath) => |
| 334 | new CustomFile(path.basename(filePath), fs.statSync(filePath).size, filePath) |
| 335 | ); |
| 336 | |
| 337 | const singles: Api.InputSingleMedia[] = []; |
| 338 | |
| 339 | const { getInputPhoto, getInputDocument } = await import("teleproto/Utils"); |
| 340 | |
| 341 | for (let i = 0; i < files.length; i++) { |
| 342 | const file = files[i]; |
| 343 | |
| 344 | const handle = await client.uploadFile({ file, workers: 1 }); |
| 345 | const uploaded = new Api.InputMediaUploadedPhoto({ file: handle }); |
| 346 | |
| 347 | const r = await client.invoke( |
| 348 | new Api.messages.UploadMedia({ peer: chatId, media: uploaded }) |
| 349 | ); |
| 350 | |
| 351 | let media: Api.TypeInputMedia; |
| 352 | if (r instanceof Api.MessageMediaPhoto) { |
| 353 | const id = getInputPhoto(r.photo); |
| 354 | media = new Api.InputMediaPhoto({ id, spoiler: true }); |
| 355 | } else if (r instanceof Api.MessageMediaDocument) { |
| 356 | const id = getInputDocument(r.document); |
| 357 | media = new Api.InputMediaDocument({ id, spoiler: true }); |
| 358 | } else { |
| 359 | console.warn("非预期的 UploadMedia 返回类型,已跳过"); |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | singles.push( |
| 364 | new Api.InputSingleMedia({ |
| 365 | media, |
| 366 | message: i === 0 && photoSetUrl ? `套图链接: ${photoSetUrl}` : "", |
| 367 | entities: undefined, |
| 368 | }) |
| 369 | ); |
| 370 | } |
| 371 | |
| 372 | if (!singles.length) { |
| 373 | throw new Error("无可发送的媒体"); |
| 374 | } |
| 375 | |
| 376 | await client.invoke( |
| 377 | new Api.messages.SendMultiMedia({ peer: chatId, multiMedia: singles }) |
| 378 | ); |
| 379 | } catch (err: any) { |
| 380 | console.warn("剧透相册发送失败,尝试逐条发送", err?.message || err); |
| 381 | for (const filePath of filePaths) { |
| 382 | await sendSingleImage(client, chatId, filePath, photoSetUrl); |
no test coverage detected