@meet-im/meet-bot-jssdk 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -231,6 +231,12 @@ function buildUrl(baseUrl, path, pathPrefix, params) {
231
231
  }
232
232
  return url.toString();
233
233
  }
234
+ function sanitizeHeaders(headers) {
235
+ return {
236
+ ...headers,
237
+ Authorization: headers.Authorization ? "Bot ***" : ""
238
+ };
239
+ }
234
240
  async function request(options) {
235
241
  const {
236
242
  token,
@@ -241,6 +247,7 @@ async function request(options) {
241
247
  params,
242
248
  body,
243
249
  timeout = HTTP.DEFAULT_TIMEOUT,
250
+ userAgent,
244
251
  raw = false
245
252
  } = options;
246
253
  validateToken(token);
@@ -252,7 +259,11 @@ async function request(options) {
252
259
  Accept: "application/json",
253
260
  Authorization: `Bot ${extractBotToken(token)}`
254
261
  };
262
+ if (userAgent) {
263
+ headers["User-Agent"] = userAgent;
264
+ }
255
265
  logger.info("[Request]", method, url);
266
+ logger.info("[Headers]", JSON.stringify(sanitizeHeaders(headers)));
256
267
  if (body) {
257
268
  logger.info("[Body]", JSON.stringify(body));
258
269
  }
@@ -350,10 +361,11 @@ function mapStatusCodeToErrorCode(status) {
350
361
 
351
362
  // src/api/file.ts
352
363
  async function getUploadURL(params) {
353
- const { token, baseUrl, ...body } = params;
364
+ const { token, baseUrl, userAgent, ...body } = params;
354
365
  return request({
355
366
  token,
356
367
  baseUrl,
368
+ userAgent,
357
369
  method: "POST",
358
370
  path: "getUploadURL",
359
371
  body: {
@@ -371,10 +383,11 @@ async function getUploadURL(params) {
371
383
  });
372
384
  }
373
385
  async function getMultiPartUploadURL(params) {
374
- const { token, baseUrl, ...body } = params;
386
+ const { token, baseUrl, userAgent, ...body } = params;
375
387
  return request({
376
388
  token,
377
389
  baseUrl,
390
+ userAgent,
378
391
  method: "POST",
379
392
  path: "getMultiPartUploadURL",
380
393
  body: {
@@ -390,10 +403,11 @@ async function getMultiPartUploadURL(params) {
390
403
  });
391
404
  }
392
405
  async function completeMultipartUpload(params) {
393
- const { token, baseUrl, ...body } = params;
406
+ const { token, baseUrl, userAgent, ...body } = params;
394
407
  return request({
395
408
  token,
396
409
  baseUrl,
410
+ userAgent,
397
411
  method: "POST",
398
412
  path: "completeMultipartUpload",
399
413
  body: {
@@ -405,10 +419,11 @@ async function completeMultipartUpload(params) {
405
419
  });
406
420
  }
407
421
  async function getAccessURL(params) {
408
- const { token, baseUrl, ...queryParams } = params;
422
+ const { token, baseUrl, userAgent, ...queryParams } = params;
409
423
  return request({
410
424
  token,
411
425
  baseUrl,
426
+ userAgent,
412
427
  method: "GET",
413
428
  path: "getAccessURL",
414
429
  params: {
@@ -486,21 +501,22 @@ function formatSpeed(bytesPerSecond) {
486
501
  return `${(bytesPerSecond / 1024 / 1024).toFixed(2)}MB/s`;
487
502
  }
488
503
  }
489
- async function uploadFile(token, buffer, options, baseUrl) {
504
+ async function uploadFile(token, buffer, options, baseUrl, userAgent) {
490
505
  const { fileName, contentType, onProgress } = options;
491
506
  const size = buffer.length;
492
507
  const md5 = await computeMD5(buffer);
493
508
  logger.info(`[uploadFile] Starting upload: ${fileName}, size: ${size}, md5: ${md5}`);
494
509
  if (size < UPLOAD.MULTIPART_THRESHOLD) {
495
- return uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress);
510
+ return uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress);
496
511
  } else {
497
- return uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress);
512
+ return uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress);
498
513
  }
499
514
  }
500
- async function uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress) {
515
+ async function uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress) {
501
516
  const result = await getUploadURL({
502
517
  token,
503
518
  baseUrl,
519
+ userAgent,
504
520
  originFileName: fileName,
505
521
  contentType,
506
522
  md5,
@@ -513,13 +529,14 @@ async function uploadSingleFile(token, buffer, fileName, contentType, md5, baseU
513
529
  const ossResult = await uploadToOSS(result.signedUrl, buffer, contentType, result.callback || "", onProgress);
514
530
  return { fileID: ossResult.id, path: ossResult.path, size: ossResult.size };
515
531
  }
516
- async function uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress) {
532
+ async function uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress) {
517
533
  const size = buffer.length;
518
534
  const chunkNum = getChunkNum(size);
519
535
  logger.info(`[uploadMultipartFile] Starting multipart upload: ${chunkNum} chunks`);
520
536
  const result = await getMultiPartUploadURL({
521
537
  token,
522
538
  baseUrl,
539
+ userAgent,
523
540
  originFileName: fileName,
524
541
  contentType,
525
542
  md5,
@@ -567,6 +584,7 @@ async function uploadMultipartFile(token, buffer, fileName, contentType, md5, ba
567
584
  const complete = await completeMultipartUpload({
568
585
  token,
569
586
  baseUrl,
587
+ userAgent,
570
588
  originFileName: fileName,
571
589
  md5,
572
590
  UploadParts: uploadParts
@@ -574,13 +592,14 @@ async function uploadMultipartFile(token, buffer, fileName, contentType, md5, ba
574
592
  logger.info(`[uploadMultipartFile] Upload complete: ${complete.ID}`);
575
593
  return { fileID: complete.ID, path: complete.path, size };
576
594
  }
577
- async function sendMediaMessage(token, sessionInfo, options, baseUrl) {
595
+ async function sendMediaMessage(token, sessionInfo, options, baseUrl, userAgent) {
578
596
  const { buffer, fileName, contentType, content, onProgress } = options;
579
- const { fileID, path } = await uploadFile(token, buffer, { fileName, contentType, onProgress }, baseUrl);
597
+ const { fileID, path } = await uploadFile(token, buffer, { fileName, contentType, onProgress }, baseUrl, userAgent);
580
598
  logger.info(`[sendMediaMessage] fileID=${fileID}, path=${path}`);
581
599
  return sendMessage({
582
600
  token,
583
601
  baseUrl,
602
+ userAgent,
584
603
  sessionInfo,
585
604
  msgContent: {
586
605
  content: content || "",
@@ -599,10 +618,11 @@ async function sendMediaMessage(token, sessionInfo, options, baseUrl) {
599
618
 
600
619
  // src/api/user.ts
601
620
  async function getUsers(params) {
602
- const { token, baseUrl } = params;
621
+ const { token, baseUrl, userAgent } = params;
603
622
  const result = await request({
604
623
  token,
605
624
  baseUrl,
625
+ userAgent,
606
626
  method: "POST",
607
627
  path: "general/SyncCompanyUser",
608
628
  pathPrefix: "/",
@@ -618,10 +638,11 @@ async function getUsers(params) {
618
638
 
619
639
  // src/api/index.ts
620
640
  async function getUpdates(params) {
621
- const { token, baseUrl, timeout = API.DEFAULT_TIMEOUT, offset, limit = API.DEFAULT_LIMIT } = params;
641
+ const { token, baseUrl, userAgent, timeout = API.DEFAULT_TIMEOUT, offset, limit = API.DEFAULT_LIMIT } = params;
622
642
  return request({
623
643
  token,
624
644
  baseUrl,
645
+ userAgent,
625
646
  method: "POST",
626
647
  path: "getUpdates",
627
648
  body: {
@@ -633,10 +654,11 @@ async function getUpdates(params) {
633
654
  });
634
655
  }
635
656
  async function getUpdatesV2(params) {
636
- const { token, baseUrl, timeout = API.DEFAULT_TIMEOUT, limit = API.DEFAULT_LIMIT } = params;
657
+ const { token, baseUrl, userAgent, timeout = API.DEFAULT_TIMEOUT, limit = API.DEFAULT_LIMIT } = params;
637
658
  return request({
638
659
  token,
639
660
  baseUrl,
661
+ userAgent,
640
662
  method: "POST",
641
663
  path: "getUpdatesV2",
642
664
  body: {
@@ -647,7 +669,7 @@ async function getUpdatesV2(params) {
647
669
  });
648
670
  }
649
671
  async function sendMessage(params) {
650
- const { token, baseUrl, sessionInfo, msgContent } = params;
672
+ const { token, baseUrl, userAgent, sessionInfo, msgContent } = params;
651
673
  const hasContent = msgContent.content && msgContent.content.trim() !== "";
652
674
  const hasAttachment = msgContent.extraInfo?.attechmentInfo || msgContent.extraInfo?.attechmentInfos && msgContent.extraInfo.attechmentInfos.length > 0;
653
675
  if (!hasContent && !hasAttachment) {
@@ -657,6 +679,7 @@ async function sendMessage(params) {
657
679
  return request({
658
680
  token,
659
681
  baseUrl,
682
+ userAgent,
660
683
  method: "POST",
661
684
  path: "sendMessage",
662
685
  body: {
@@ -783,6 +806,7 @@ var MeetBot = class {
783
806
  pollingLimit;
784
807
  longPollingTimeout;
785
808
  useV2;
809
+ userAgent;
786
810
  eventHandlers = /* @__PURE__ */ new Map();
787
811
  polling = false;
788
812
  offset = 0;
@@ -803,6 +827,7 @@ var MeetBot = class {
803
827
  this.pollingLimit = config.pollingLimit ?? POLLING.DEFAULT_LIMIT;
804
828
  this.longPollingTimeout = config.longPollingTimeout ?? POLLING.DEFAULT_TIMEOUT;
805
829
  this.useV2 = config.useV2 ?? false;
830
+ this.userAgent = config.userAgent;
806
831
  this.userCache = new UserCache(config.userCacheOptions);
807
832
  logger.setLevel(config.logLevel ?? "silent");
808
833
  }
@@ -855,6 +880,7 @@ var MeetBot = class {
855
880
  const result = await getUpdatesV2({
856
881
  token: this.token,
857
882
  baseUrl: this.baseUrl,
883
+ userAgent: this.userAgent,
858
884
  timeout,
859
885
  limit
860
886
  });
@@ -868,6 +894,7 @@ var MeetBot = class {
868
894
  const updates = await getUpdates({
869
895
  token: this.token,
870
896
  baseUrl: this.baseUrl,
897
+ userAgent: this.userAgent,
871
898
  timeout,
872
899
  offset: this.offset,
873
900
  limit
@@ -907,6 +934,7 @@ var MeetBot = class {
907
934
  return getUpdates({
908
935
  token: this.token,
909
936
  baseUrl: this.baseUrl,
937
+ userAgent: this.userAgent,
910
938
  ...options
911
939
  });
912
940
  }
@@ -914,6 +942,7 @@ var MeetBot = class {
914
942
  return getUpdatesV2({
915
943
  token: this.token,
916
944
  baseUrl: this.baseUrl,
945
+ userAgent: this.userAgent,
917
946
  ...options
918
947
  });
919
948
  }
@@ -921,6 +950,7 @@ var MeetBot = class {
921
950
  return sendMessage({
922
951
  token: this.token,
923
952
  baseUrl: this.baseUrl,
953
+ userAgent: this.userAgent,
924
954
  sessionInfo,
925
955
  msgContent
926
956
  });
@@ -932,6 +962,7 @@ var MeetBot = class {
932
962
  return getUploadURL({
933
963
  token: this.token,
934
964
  baseUrl: this.baseUrl,
965
+ userAgent: this.userAgent,
935
966
  ...params
936
967
  });
937
968
  }
@@ -942,6 +973,7 @@ var MeetBot = class {
942
973
  return getMultiPartUploadURL({
943
974
  token: this.token,
944
975
  baseUrl: this.baseUrl,
976
+ userAgent: this.userAgent,
945
977
  ...params
946
978
  });
947
979
  }
@@ -952,6 +984,7 @@ var MeetBot = class {
952
984
  return completeMultipartUpload({
953
985
  token: this.token,
954
986
  baseUrl: this.baseUrl,
987
+ userAgent: this.userAgent,
955
988
  ...params
956
989
  });
957
990
  }
@@ -970,6 +1003,7 @@ var MeetBot = class {
970
1003
  return getAccessURL({
971
1004
  token: this.token,
972
1005
  baseUrl: this.baseUrl,
1006
+ userAgent: this.userAgent,
973
1007
  ...apiParams
974
1008
  });
975
1009
  }
@@ -977,20 +1011,20 @@ var MeetBot = class {
977
1011
  * 上传文件(自动选择单文件或分片上传)
978
1012
  */
979
1013
  async uploadFile(buffer, options) {
980
- return uploadFile(this.token, buffer, options, this.baseUrl);
1014
+ return uploadFile(this.token, buffer, options, this.baseUrl, this.userAgent);
981
1015
  }
982
1016
  /**
983
1017
  * 发送媒体消息(上传并发送)
984
1018
  */
985
1019
  async sendMedia(sessionInfo, options) {
986
- return sendMediaMessage(this.token, sessionInfo, options, this.baseUrl);
1020
+ return sendMediaMessage(this.token, sessionInfo, options, this.baseUrl, this.userAgent);
987
1021
  }
988
1022
  /**
989
1023
  * 刷新用户缓存
990
1024
  */
991
1025
  async refreshUserCache() {
992
1026
  await this.userCache.load(async () => {
993
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1027
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
994
1028
  });
995
1029
  }
996
1030
  /**
@@ -998,7 +1032,7 @@ var MeetBot = class {
998
1032
  */
999
1033
  async getUserById(userId) {
1000
1034
  await this.userCache.ensureLoaded(async () => {
1001
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1035
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1002
1036
  });
1003
1037
  return this.userCache.getById(userId);
1004
1038
  }
@@ -1007,7 +1041,7 @@ var MeetBot = class {
1007
1041
  */
1008
1042
  async getUserByIds(userIds) {
1009
1043
  await this.userCache.ensureLoaded(async () => {
1010
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1044
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1011
1045
  });
1012
1046
  return this.userCache.getByIds(userIds);
1013
1047
  }
@@ -1016,7 +1050,7 @@ var MeetBot = class {
1016
1050
  */
1017
1051
  async getUserByName(name) {
1018
1052
  await this.userCache.ensureLoaded(async () => {
1019
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1053
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1020
1054
  });
1021
1055
  return this.userCache.getByName(name);
1022
1056
  }
@@ -1025,7 +1059,7 @@ var MeetBot = class {
1025
1059
  */
1026
1060
  async searchUserByName(query) {
1027
1061
  await this.userCache.ensureLoaded(async () => {
1028
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1062
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1029
1063
  });
1030
1064
  return this.userCache.searchByName(query);
1031
1065
  }
package/dist/index.d.cts CHANGED
@@ -91,6 +91,7 @@ interface MeetBotConfig {
91
91
  longPollingTimeout?: number;
92
92
  logLevel?: LogLevel;
93
93
  useV2?: boolean;
94
+ userAgent?: string;
94
95
  }
95
96
  interface GetUpdatesOptions {
96
97
  token: string;
@@ -310,6 +311,7 @@ declare class MeetBot {
310
311
  private readonly pollingLimit;
311
312
  private readonly longPollingTimeout;
312
313
  private readonly useV2;
314
+ private readonly userAgent?;
313
315
  private readonly eventHandlers;
314
316
  private polling;
315
317
  private offset;
@@ -352,36 +354,42 @@ declare class MeetBot {
352
354
  interface GetUploadURLApiParams extends GetUploadURLParams {
353
355
  token: string;
354
356
  baseUrl?: string;
357
+ userAgent?: string;
355
358
  }
356
359
  declare function getUploadURL(params: GetUploadURLApiParams): Promise<UploadURLResult>;
357
360
  interface GetMultiPartUploadURLApiParams extends GetMultiPartUploadURLParams {
358
361
  token: string;
359
362
  baseUrl?: string;
363
+ userAgent?: string;
360
364
  }
361
365
  declare function getMultiPartUploadURL(params: GetMultiPartUploadURLApiParams): Promise<MultiPartUploadURLResult>;
362
366
  interface CompleteMultipartUploadApiParams extends CompleteMultipartUploadParams {
363
367
  token: string;
364
368
  baseUrl?: string;
369
+ userAgent?: string;
365
370
  }
366
371
  declare function completeMultipartUpload(params: CompleteMultipartUploadApiParams): Promise<CompleteMultipartUploadResult>;
367
372
  interface GetAccessURLApiParams extends GetAccessURLParams {
368
373
  token: string;
369
374
  baseUrl?: string;
375
+ userAgent?: string;
370
376
  }
371
377
  declare function getAccessURL(params: GetAccessURLApiParams): Promise<AccessURLResult>;
372
378
 
373
379
  declare function computeMD5(buffer: Buffer): Promise<string>;
374
- declare function uploadFile(token: string, buffer: Buffer, options: UploadFileOptions, baseUrl?: string): Promise<UploadFileResult>;
375
- declare function sendMediaMessage(token: string, sessionInfo: SessionInfo, options: SendMediaOptions, baseUrl?: string): Promise<SendMessageResult>;
380
+ declare function uploadFile(token: string, buffer: Buffer, options: UploadFileOptions, baseUrl?: string, userAgent?: string): Promise<UploadFileResult>;
381
+ declare function sendMediaMessage(token: string, sessionInfo: SessionInfo, options: SendMediaOptions, baseUrl?: string, userAgent?: string): Promise<SendMessageResult>;
376
382
 
377
383
  declare function getUsers(params: {
378
384
  token: string;
379
385
  baseUrl?: string;
386
+ userAgent?: string;
380
387
  }): Promise<MeetUser[]>;
381
388
 
382
389
  interface GetUpdatesParams {
383
390
  token: string;
384
391
  baseUrl?: string;
392
+ userAgent?: string;
385
393
  timeout?: number;
386
394
  offset?: number;
387
395
  limit?: number;
@@ -390,6 +398,7 @@ declare function getUpdates(params: GetUpdatesParams): Promise<BotUpdate[]>;
390
398
  interface GetUpdatesV2Params {
391
399
  token: string;
392
400
  baseUrl?: string;
401
+ userAgent?: string;
393
402
  timeout?: number;
394
403
  limit?: number;
395
404
  }
@@ -397,6 +406,7 @@ declare function getUpdatesV2(params: GetUpdatesV2Params): Promise<GetUpdatesV2R
397
406
  interface SendMessageParams {
398
407
  token: string;
399
408
  baseUrl?: string;
409
+ userAgent?: string;
400
410
  sessionInfo: SessionInfo;
401
411
  msgContent: MsgContent;
402
412
  }
package/dist/index.d.ts CHANGED
@@ -91,6 +91,7 @@ interface MeetBotConfig {
91
91
  longPollingTimeout?: number;
92
92
  logLevel?: LogLevel;
93
93
  useV2?: boolean;
94
+ userAgent?: string;
94
95
  }
95
96
  interface GetUpdatesOptions {
96
97
  token: string;
@@ -310,6 +311,7 @@ declare class MeetBot {
310
311
  private readonly pollingLimit;
311
312
  private readonly longPollingTimeout;
312
313
  private readonly useV2;
314
+ private readonly userAgent?;
313
315
  private readonly eventHandlers;
314
316
  private polling;
315
317
  private offset;
@@ -352,36 +354,42 @@ declare class MeetBot {
352
354
  interface GetUploadURLApiParams extends GetUploadURLParams {
353
355
  token: string;
354
356
  baseUrl?: string;
357
+ userAgent?: string;
355
358
  }
356
359
  declare function getUploadURL(params: GetUploadURLApiParams): Promise<UploadURLResult>;
357
360
  interface GetMultiPartUploadURLApiParams extends GetMultiPartUploadURLParams {
358
361
  token: string;
359
362
  baseUrl?: string;
363
+ userAgent?: string;
360
364
  }
361
365
  declare function getMultiPartUploadURL(params: GetMultiPartUploadURLApiParams): Promise<MultiPartUploadURLResult>;
362
366
  interface CompleteMultipartUploadApiParams extends CompleteMultipartUploadParams {
363
367
  token: string;
364
368
  baseUrl?: string;
369
+ userAgent?: string;
365
370
  }
366
371
  declare function completeMultipartUpload(params: CompleteMultipartUploadApiParams): Promise<CompleteMultipartUploadResult>;
367
372
  interface GetAccessURLApiParams extends GetAccessURLParams {
368
373
  token: string;
369
374
  baseUrl?: string;
375
+ userAgent?: string;
370
376
  }
371
377
  declare function getAccessURL(params: GetAccessURLApiParams): Promise<AccessURLResult>;
372
378
 
373
379
  declare function computeMD5(buffer: Buffer): Promise<string>;
374
- declare function uploadFile(token: string, buffer: Buffer, options: UploadFileOptions, baseUrl?: string): Promise<UploadFileResult>;
375
- declare function sendMediaMessage(token: string, sessionInfo: SessionInfo, options: SendMediaOptions, baseUrl?: string): Promise<SendMessageResult>;
380
+ declare function uploadFile(token: string, buffer: Buffer, options: UploadFileOptions, baseUrl?: string, userAgent?: string): Promise<UploadFileResult>;
381
+ declare function sendMediaMessage(token: string, sessionInfo: SessionInfo, options: SendMediaOptions, baseUrl?: string, userAgent?: string): Promise<SendMessageResult>;
376
382
 
377
383
  declare function getUsers(params: {
378
384
  token: string;
379
385
  baseUrl?: string;
386
+ userAgent?: string;
380
387
  }): Promise<MeetUser[]>;
381
388
 
382
389
  interface GetUpdatesParams {
383
390
  token: string;
384
391
  baseUrl?: string;
392
+ userAgent?: string;
385
393
  timeout?: number;
386
394
  offset?: number;
387
395
  limit?: number;
@@ -390,6 +398,7 @@ declare function getUpdates(params: GetUpdatesParams): Promise<BotUpdate[]>;
390
398
  interface GetUpdatesV2Params {
391
399
  token: string;
392
400
  baseUrl?: string;
401
+ userAgent?: string;
393
402
  timeout?: number;
394
403
  limit?: number;
395
404
  }
@@ -397,6 +406,7 @@ declare function getUpdatesV2(params: GetUpdatesV2Params): Promise<GetUpdatesV2R
397
406
  interface SendMessageParams {
398
407
  token: string;
399
408
  baseUrl?: string;
409
+ userAgent?: string;
400
410
  sessionInfo: SessionInfo;
401
411
  msgContent: MsgContent;
402
412
  }
package/dist/index.js CHANGED
@@ -225,6 +225,12 @@ function buildUrl(baseUrl, path, pathPrefix, params) {
225
225
  }
226
226
  return url.toString();
227
227
  }
228
+ function sanitizeHeaders(headers) {
229
+ return {
230
+ ...headers,
231
+ Authorization: headers.Authorization ? "Bot ***" : ""
232
+ };
233
+ }
228
234
  async function request(options) {
229
235
  const {
230
236
  token,
@@ -235,6 +241,7 @@ async function request(options) {
235
241
  params,
236
242
  body,
237
243
  timeout = HTTP.DEFAULT_TIMEOUT,
244
+ userAgent,
238
245
  raw = false
239
246
  } = options;
240
247
  validateToken(token);
@@ -246,7 +253,11 @@ async function request(options) {
246
253
  Accept: "application/json",
247
254
  Authorization: `Bot ${extractBotToken(token)}`
248
255
  };
256
+ if (userAgent) {
257
+ headers["User-Agent"] = userAgent;
258
+ }
249
259
  logger.info("[Request]", method, url);
260
+ logger.info("[Headers]", JSON.stringify(sanitizeHeaders(headers)));
250
261
  if (body) {
251
262
  logger.info("[Body]", JSON.stringify(body));
252
263
  }
@@ -344,10 +355,11 @@ function mapStatusCodeToErrorCode(status) {
344
355
 
345
356
  // src/api/file.ts
346
357
  async function getUploadURL(params) {
347
- const { token, baseUrl, ...body } = params;
358
+ const { token, baseUrl, userAgent, ...body } = params;
348
359
  return request({
349
360
  token,
350
361
  baseUrl,
362
+ userAgent,
351
363
  method: "POST",
352
364
  path: "getUploadURL",
353
365
  body: {
@@ -365,10 +377,11 @@ async function getUploadURL(params) {
365
377
  });
366
378
  }
367
379
  async function getMultiPartUploadURL(params) {
368
- const { token, baseUrl, ...body } = params;
380
+ const { token, baseUrl, userAgent, ...body } = params;
369
381
  return request({
370
382
  token,
371
383
  baseUrl,
384
+ userAgent,
372
385
  method: "POST",
373
386
  path: "getMultiPartUploadURL",
374
387
  body: {
@@ -384,10 +397,11 @@ async function getMultiPartUploadURL(params) {
384
397
  });
385
398
  }
386
399
  async function completeMultipartUpload(params) {
387
- const { token, baseUrl, ...body } = params;
400
+ const { token, baseUrl, userAgent, ...body } = params;
388
401
  return request({
389
402
  token,
390
403
  baseUrl,
404
+ userAgent,
391
405
  method: "POST",
392
406
  path: "completeMultipartUpload",
393
407
  body: {
@@ -399,10 +413,11 @@ async function completeMultipartUpload(params) {
399
413
  });
400
414
  }
401
415
  async function getAccessURL(params) {
402
- const { token, baseUrl, ...queryParams } = params;
416
+ const { token, baseUrl, userAgent, ...queryParams } = params;
403
417
  return request({
404
418
  token,
405
419
  baseUrl,
420
+ userAgent,
406
421
  method: "GET",
407
422
  path: "getAccessURL",
408
423
  params: {
@@ -480,21 +495,22 @@ function formatSpeed(bytesPerSecond) {
480
495
  return `${(bytesPerSecond / 1024 / 1024).toFixed(2)}MB/s`;
481
496
  }
482
497
  }
483
- async function uploadFile(token, buffer, options, baseUrl) {
498
+ async function uploadFile(token, buffer, options, baseUrl, userAgent) {
484
499
  const { fileName, contentType, onProgress } = options;
485
500
  const size = buffer.length;
486
501
  const md5 = await computeMD5(buffer);
487
502
  logger.info(`[uploadFile] Starting upload: ${fileName}, size: ${size}, md5: ${md5}`);
488
503
  if (size < UPLOAD.MULTIPART_THRESHOLD) {
489
- return uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress);
504
+ return uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress);
490
505
  } else {
491
- return uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress);
506
+ return uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress);
492
507
  }
493
508
  }
494
- async function uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress) {
509
+ async function uploadSingleFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress) {
495
510
  const result = await getUploadURL({
496
511
  token,
497
512
  baseUrl,
513
+ userAgent,
498
514
  originFileName: fileName,
499
515
  contentType,
500
516
  md5,
@@ -507,13 +523,14 @@ async function uploadSingleFile(token, buffer, fileName, contentType, md5, baseU
507
523
  const ossResult = await uploadToOSS(result.signedUrl, buffer, contentType, result.callback || "", onProgress);
508
524
  return { fileID: ossResult.id, path: ossResult.path, size: ossResult.size };
509
525
  }
510
- async function uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, onProgress) {
526
+ async function uploadMultipartFile(token, buffer, fileName, contentType, md5, baseUrl, userAgent, onProgress) {
511
527
  const size = buffer.length;
512
528
  const chunkNum = getChunkNum(size);
513
529
  logger.info(`[uploadMultipartFile] Starting multipart upload: ${chunkNum} chunks`);
514
530
  const result = await getMultiPartUploadURL({
515
531
  token,
516
532
  baseUrl,
533
+ userAgent,
517
534
  originFileName: fileName,
518
535
  contentType,
519
536
  md5,
@@ -561,6 +578,7 @@ async function uploadMultipartFile(token, buffer, fileName, contentType, md5, ba
561
578
  const complete = await completeMultipartUpload({
562
579
  token,
563
580
  baseUrl,
581
+ userAgent,
564
582
  originFileName: fileName,
565
583
  md5,
566
584
  UploadParts: uploadParts
@@ -568,13 +586,14 @@ async function uploadMultipartFile(token, buffer, fileName, contentType, md5, ba
568
586
  logger.info(`[uploadMultipartFile] Upload complete: ${complete.ID}`);
569
587
  return { fileID: complete.ID, path: complete.path, size };
570
588
  }
571
- async function sendMediaMessage(token, sessionInfo, options, baseUrl) {
589
+ async function sendMediaMessage(token, sessionInfo, options, baseUrl, userAgent) {
572
590
  const { buffer, fileName, contentType, content, onProgress } = options;
573
- const { fileID, path } = await uploadFile(token, buffer, { fileName, contentType, onProgress }, baseUrl);
591
+ const { fileID, path } = await uploadFile(token, buffer, { fileName, contentType, onProgress }, baseUrl, userAgent);
574
592
  logger.info(`[sendMediaMessage] fileID=${fileID}, path=${path}`);
575
593
  return sendMessage({
576
594
  token,
577
595
  baseUrl,
596
+ userAgent,
578
597
  sessionInfo,
579
598
  msgContent: {
580
599
  content: content || "",
@@ -593,10 +612,11 @@ async function sendMediaMessage(token, sessionInfo, options, baseUrl) {
593
612
 
594
613
  // src/api/user.ts
595
614
  async function getUsers(params) {
596
- const { token, baseUrl } = params;
615
+ const { token, baseUrl, userAgent } = params;
597
616
  const result = await request({
598
617
  token,
599
618
  baseUrl,
619
+ userAgent,
600
620
  method: "POST",
601
621
  path: "general/SyncCompanyUser",
602
622
  pathPrefix: "/",
@@ -612,10 +632,11 @@ async function getUsers(params) {
612
632
 
613
633
  // src/api/index.ts
614
634
  async function getUpdates(params) {
615
- const { token, baseUrl, timeout = API.DEFAULT_TIMEOUT, offset, limit = API.DEFAULT_LIMIT } = params;
635
+ const { token, baseUrl, userAgent, timeout = API.DEFAULT_TIMEOUT, offset, limit = API.DEFAULT_LIMIT } = params;
616
636
  return request({
617
637
  token,
618
638
  baseUrl,
639
+ userAgent,
619
640
  method: "POST",
620
641
  path: "getUpdates",
621
642
  body: {
@@ -627,10 +648,11 @@ async function getUpdates(params) {
627
648
  });
628
649
  }
629
650
  async function getUpdatesV2(params) {
630
- const { token, baseUrl, timeout = API.DEFAULT_TIMEOUT, limit = API.DEFAULT_LIMIT } = params;
651
+ const { token, baseUrl, userAgent, timeout = API.DEFAULT_TIMEOUT, limit = API.DEFAULT_LIMIT } = params;
631
652
  return request({
632
653
  token,
633
654
  baseUrl,
655
+ userAgent,
634
656
  method: "POST",
635
657
  path: "getUpdatesV2",
636
658
  body: {
@@ -641,7 +663,7 @@ async function getUpdatesV2(params) {
641
663
  });
642
664
  }
643
665
  async function sendMessage(params) {
644
- const { token, baseUrl, sessionInfo, msgContent } = params;
666
+ const { token, baseUrl, userAgent, sessionInfo, msgContent } = params;
645
667
  const hasContent = msgContent.content && msgContent.content.trim() !== "";
646
668
  const hasAttachment = msgContent.extraInfo?.attechmentInfo || msgContent.extraInfo?.attechmentInfos && msgContent.extraInfo.attechmentInfos.length > 0;
647
669
  if (!hasContent && !hasAttachment) {
@@ -651,6 +673,7 @@ async function sendMessage(params) {
651
673
  return request({
652
674
  token,
653
675
  baseUrl,
676
+ userAgent,
654
677
  method: "POST",
655
678
  path: "sendMessage",
656
679
  body: {
@@ -777,6 +800,7 @@ var MeetBot = class {
777
800
  pollingLimit;
778
801
  longPollingTimeout;
779
802
  useV2;
803
+ userAgent;
780
804
  eventHandlers = /* @__PURE__ */ new Map();
781
805
  polling = false;
782
806
  offset = 0;
@@ -797,6 +821,7 @@ var MeetBot = class {
797
821
  this.pollingLimit = config.pollingLimit ?? POLLING.DEFAULT_LIMIT;
798
822
  this.longPollingTimeout = config.longPollingTimeout ?? POLLING.DEFAULT_TIMEOUT;
799
823
  this.useV2 = config.useV2 ?? false;
824
+ this.userAgent = config.userAgent;
800
825
  this.userCache = new UserCache(config.userCacheOptions);
801
826
  logger.setLevel(config.logLevel ?? "silent");
802
827
  }
@@ -849,6 +874,7 @@ var MeetBot = class {
849
874
  const result = await getUpdatesV2({
850
875
  token: this.token,
851
876
  baseUrl: this.baseUrl,
877
+ userAgent: this.userAgent,
852
878
  timeout,
853
879
  limit
854
880
  });
@@ -862,6 +888,7 @@ var MeetBot = class {
862
888
  const updates = await getUpdates({
863
889
  token: this.token,
864
890
  baseUrl: this.baseUrl,
891
+ userAgent: this.userAgent,
865
892
  timeout,
866
893
  offset: this.offset,
867
894
  limit
@@ -901,6 +928,7 @@ var MeetBot = class {
901
928
  return getUpdates({
902
929
  token: this.token,
903
930
  baseUrl: this.baseUrl,
931
+ userAgent: this.userAgent,
904
932
  ...options
905
933
  });
906
934
  }
@@ -908,6 +936,7 @@ var MeetBot = class {
908
936
  return getUpdatesV2({
909
937
  token: this.token,
910
938
  baseUrl: this.baseUrl,
939
+ userAgent: this.userAgent,
911
940
  ...options
912
941
  });
913
942
  }
@@ -915,6 +944,7 @@ var MeetBot = class {
915
944
  return sendMessage({
916
945
  token: this.token,
917
946
  baseUrl: this.baseUrl,
947
+ userAgent: this.userAgent,
918
948
  sessionInfo,
919
949
  msgContent
920
950
  });
@@ -926,6 +956,7 @@ var MeetBot = class {
926
956
  return getUploadURL({
927
957
  token: this.token,
928
958
  baseUrl: this.baseUrl,
959
+ userAgent: this.userAgent,
929
960
  ...params
930
961
  });
931
962
  }
@@ -936,6 +967,7 @@ var MeetBot = class {
936
967
  return getMultiPartUploadURL({
937
968
  token: this.token,
938
969
  baseUrl: this.baseUrl,
970
+ userAgent: this.userAgent,
939
971
  ...params
940
972
  });
941
973
  }
@@ -946,6 +978,7 @@ var MeetBot = class {
946
978
  return completeMultipartUpload({
947
979
  token: this.token,
948
980
  baseUrl: this.baseUrl,
981
+ userAgent: this.userAgent,
949
982
  ...params
950
983
  });
951
984
  }
@@ -964,6 +997,7 @@ var MeetBot = class {
964
997
  return getAccessURL({
965
998
  token: this.token,
966
999
  baseUrl: this.baseUrl,
1000
+ userAgent: this.userAgent,
967
1001
  ...apiParams
968
1002
  });
969
1003
  }
@@ -971,20 +1005,20 @@ var MeetBot = class {
971
1005
  * 上传文件(自动选择单文件或分片上传)
972
1006
  */
973
1007
  async uploadFile(buffer, options) {
974
- return uploadFile(this.token, buffer, options, this.baseUrl);
1008
+ return uploadFile(this.token, buffer, options, this.baseUrl, this.userAgent);
975
1009
  }
976
1010
  /**
977
1011
  * 发送媒体消息(上传并发送)
978
1012
  */
979
1013
  async sendMedia(sessionInfo, options) {
980
- return sendMediaMessage(this.token, sessionInfo, options, this.baseUrl);
1014
+ return sendMediaMessage(this.token, sessionInfo, options, this.baseUrl, this.userAgent);
981
1015
  }
982
1016
  /**
983
1017
  * 刷新用户缓存
984
1018
  */
985
1019
  async refreshUserCache() {
986
1020
  await this.userCache.load(async () => {
987
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1021
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
988
1022
  });
989
1023
  }
990
1024
  /**
@@ -992,7 +1026,7 @@ var MeetBot = class {
992
1026
  */
993
1027
  async getUserById(userId) {
994
1028
  await this.userCache.ensureLoaded(async () => {
995
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1029
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
996
1030
  });
997
1031
  return this.userCache.getById(userId);
998
1032
  }
@@ -1001,7 +1035,7 @@ var MeetBot = class {
1001
1035
  */
1002
1036
  async getUserByIds(userIds) {
1003
1037
  await this.userCache.ensureLoaded(async () => {
1004
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1038
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1005
1039
  });
1006
1040
  return this.userCache.getByIds(userIds);
1007
1041
  }
@@ -1010,7 +1044,7 @@ var MeetBot = class {
1010
1044
  */
1011
1045
  async getUserByName(name) {
1012
1046
  await this.userCache.ensureLoaded(async () => {
1013
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1047
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1014
1048
  });
1015
1049
  return this.userCache.getByName(name);
1016
1050
  }
@@ -1019,7 +1053,7 @@ var MeetBot = class {
1019
1053
  */
1020
1054
  async searchUserByName(query) {
1021
1055
  await this.userCache.ensureLoaded(async () => {
1022
- return getUsers({ token: this.token, baseUrl: this.baseUrl });
1056
+ return getUsers({ token: this.token, baseUrl: this.baseUrl, userAgent: this.userAgent });
1023
1057
  });
1024
1058
  return this.userCache.searchByName(query);
1025
1059
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meet-im/meet-bot-jssdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "MeetIM Chatbot JavaScript SDK - 支持 Long Polling 消息获取和消息发送",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",