@satorijs/adapter-lark 3.6.1 → 3.7.0

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/lib/index.cjs CHANGED
@@ -116,6 +116,15 @@ async function adaptSession(bot, body) {
116
116
  adaptSender(body.event.sender, session);
117
117
  await adaptMessage(bot, body.event, session);
118
118
  break;
119
+ case "application.bot.menu_v6":
120
+ if (body.event.event_key.startsWith("command:")) {
121
+ session.type = "interaction/command";
122
+ session.content = body.event.event_key.slice(8);
123
+ session.channelId = body.event.operator.operator_id.open_id;
124
+ session.userId = body.event.operator.operator_id.open_id;
125
+ session.isDirect = true;
126
+ }
127
+ break;
119
128
  case "card.action.trigger":
120
129
  if (body.event.action.value?._satori_type === "command") {
121
130
  session.type = "interaction/command";
@@ -138,11 +147,16 @@ async function adaptSession(bot, body) {
138
147
  for (const [key, value] of Object.entries(options)) {
139
148
  content += ` --${key} ${value}`;
140
149
  }
150
+ if (body.event.action.input_value) {
151
+ content += ` ${body.event.action.input_value}`;
152
+ }
141
153
  session.content = content;
142
154
  session.messageId = body.event.context.open_message_id;
143
155
  session.channelId = body.event.context.open_chat_id;
144
156
  session.guildId = body.event.context.open_chat_id;
145
157
  session.userId = body.event.operator.open_id;
158
+ const chat = await bot.internal.getImChat(session.channelId);
159
+ session.isDirect = chat.chat_mode === "p2p";
146
160
  }
147
161
  break;
148
162
  }
@@ -388,23 +402,27 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
388
402
  quote;
389
403
  textContent = "";
390
404
  richContent = [];
391
- cardElements;
405
+ card;
406
+ noteElements;
392
407
  actionElements = [];
393
408
  async post(data) {
394
409
  try {
395
410
  let resp;
396
- if (this.quote) {
397
- resp = await this.bot.internal.replyImMessage(this.quote, data);
411
+ if (this.quote?.id) {
412
+ resp = await this.bot.internal.replyImMessage(this.quote.id, {
413
+ ...data,
414
+ reply_in_thread: this.quote.replyInThread
415
+ });
398
416
  } else {
399
417
  data.receive_id = this.channelId;
400
- resp = await this.bot.internal?.createImMessage(data, {
418
+ resp = await this.bot.internal.createImMessage(data, {
401
419
  receive_id_type: extractIdType(this.channelId)
402
420
  });
403
421
  }
404
422
  const session = this.bot.session();
405
- session.messageId = resp.data.message_id;
406
- session.timestamp = Number(resp.data.create_time) * 1e3;
407
- session.userId = resp.data.sender.id;
423
+ session.messageId = resp.message_id;
424
+ session.timestamp = Number(resp.create_time) * 1e3;
425
+ session.userId = resp.sender.id;
408
426
  session.channelId = this.channelId;
409
427
  session.guildId = this.guildId;
410
428
  session.app.emit(session, "send", session);
@@ -419,25 +437,30 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
419
437
  this.errors.push(e);
420
438
  }
421
439
  }
422
- flushText(flushAction = false) {
423
- if ((this.textContent || flushAction) && this.actionElements.length) {
424
- this.cardElements?.push({ tag: "action", actions: this.actionElements });
440
+ flushText(button = false) {
441
+ if ((this.textContent || !button) && this.actionElements.length) {
442
+ this.card.elements.push({ tag: "action", actions: this.actionElements, layout: "flow" });
425
443
  this.actionElements = [];
426
444
  }
427
445
  if (this.textContent) {
428
446
  this.richContent.push([{ tag: "md", text: this.textContent }]);
429
- this.cardElements?.push({ tag: "markdown", content: this.textContent });
447
+ if (this.noteElements) {
448
+ this.noteElements.push({ tag: "plain_text", content: this.textContent });
449
+ } else if (this.card) {
450
+ this.card.elements.push({ tag: "markdown", content: this.textContent });
451
+ }
430
452
  this.textContent = "";
431
453
  }
432
454
  }
433
455
  async flush() {
434
456
  this.flushText();
435
- if (!this.cardElements && !this.richContent.length) return;
436
- if (this.cardElements) {
457
+ if (!this.card && !this.richContent.length) return;
458
+ if (this.card) {
459
+ this.bot.logger.debug("card", JSON.stringify(this.card.elements));
437
460
  await this.post({
438
461
  msg_type: "interactive",
439
462
  content: JSON.stringify({
440
- elements: this.cardElements
463
+ elements: this.card.elements
441
464
  })
442
465
  });
443
466
  } else {
@@ -453,14 +476,14 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
453
476
  this.quote = void 0;
454
477
  this.textContent = "";
455
478
  this.richContent = [];
456
- this.cardElements = void 0;
479
+ this.card = void 0;
457
480
  }
458
481
  async createImage(url) {
459
482
  const { filename, type, data } = await this.bot.assetsQuester.file(url);
460
483
  const payload = new FormData();
461
484
  payload.append("image", new Blob([data], { type }), filename);
462
485
  payload.append("image_type", "message");
463
- const { data: { image_key } } = await this.bot.internal.createImImage(payload);
486
+ const { image_key } = await this.bot.internal.createImImage(payload);
464
487
  return image_key;
465
488
  }
466
489
  async sendFile(_type, attrs) {
@@ -484,12 +507,31 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
484
507
  payload.append("file_type", "stream");
485
508
  }
486
509
  }
487
- const { data: { file_key } } = await this.bot.internal.createImFile(payload);
510
+ const { file_key } = await this.bot.internal.createImFile(payload);
488
511
  await this.post({
489
512
  msg_type: _type === "video" ? "media" : _type,
490
513
  content: JSON.stringify({ file_key })
491
514
  });
492
515
  }
516
+ createBehavior(attrs) {
517
+ const behaviors = [];
518
+ if (attrs.type === "link") {
519
+ behaviors.push({
520
+ type: "open_url",
521
+ default_url: attrs.href
522
+ });
523
+ } else if (attrs.type === "input") {
524
+ behaviors.push({
525
+ type: "callback",
526
+ value: {
527
+ _satori_type: "command",
528
+ content: attrs.text
529
+ }
530
+ });
531
+ } else if (attrs.type === "action") {
532
+ }
533
+ return behaviors.length ? behaviors : void 0;
534
+ }
493
535
  async visit(element) {
494
536
  const { type, attrs, children } = element;
495
537
  if (type === "text") {
@@ -512,7 +554,7 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
512
554
  } else if (type === "sharp") {
513
555
  } else if (type === "quote") {
514
556
  await this.flush();
515
- this.quote = attrs.id;
557
+ this.quote = attrs;
516
558
  } else if (type === "img" || type === "image") {
517
559
  const image_key = await this.createImage(attrs.src || attrs.url);
518
560
  this.textContent += `![${attrs.alt ?? "图片"}](${image_key})`;
@@ -527,25 +569,41 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
527
569
  } else if (type === "hr") {
528
570
  this.flushText();
529
571
  this.richContent.push([{ tag: "hr" }]);
530
- this.cardElements?.push({ tag: "hr" });
531
- } else if (type === "button") {
572
+ this.card?.elements.push({ tag: "hr" });
573
+ } else if (type === "form") {
532
574
  this.flushText();
533
- const behaviors = [];
534
- if (attrs.type === "link") {
535
- behaviors.push({
536
- type: "open_url",
537
- default_url: attrs.href
538
- });
539
- } else if (attrs.type === "input") {
540
- behaviors.push({
541
- type: "callback",
542
- value: {
543
- _satori_type: "command",
544
- content: attrs.text
545
- }
575
+ const length = this.card?.elements.length;
576
+ await this.render(children);
577
+ if (this.card?.elements.length > length) {
578
+ const elements = this.card?.elements.slice(length);
579
+ this.card.elements.push({
580
+ tag: "form",
581
+ name: attrs.name || "Form",
582
+ elements
546
583
  });
547
- } else if (attrs.type === "action") {
548
584
  }
585
+ } else if (type === "input") {
586
+ this.flushText();
587
+ this.card?.elements.push({
588
+ tag: "action",
589
+ actions: [{
590
+ tag: "input",
591
+ name: attrs.name,
592
+ width: attrs.width,
593
+ label: attrs.label && {
594
+ tag: "plain_text",
595
+ content: attrs.label
596
+ },
597
+ placeholder: attrs.placeholder && {
598
+ tag: "plain_text",
599
+ content: attrs.placeholder
600
+ },
601
+ behaviors: this.createBehavior(attrs)
602
+ }]
603
+ });
604
+ } else if (type === "button") {
605
+ this.card ??= { elements: [] };
606
+ this.flushText(true);
549
607
  await this.render(children);
550
608
  this.actionElements.push({
551
609
  tag: "button",
@@ -553,13 +611,14 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
553
611
  tag: "plain_text",
554
612
  content: this.textContent
555
613
  },
556
- behaviors
614
+ disabled: attrs.disabled,
615
+ behaviors: this.createBehavior(attrs)
557
616
  });
558
617
  this.textContent = "";
559
618
  } else if (type === "button-group") {
560
- this.flushText(true);
619
+ this.flushText();
561
620
  await this.render(children);
562
- this.flushText(true);
621
+ this.flushText();
563
622
  } else if (type.startsWith("lark:") || type.startsWith("feishu:")) {
564
623
  const tag = type.slice(type.split(":", 1)[0].length + 1);
565
624
  if (tag === "share-chat") {
@@ -588,18 +647,51 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
588
647
  this.textContent = "";
589
648
  } else if (tag === "card") {
590
649
  await this.flush();
591
- this.cardElements = [];
650
+ this.card = {
651
+ elements: [],
652
+ header: attrs.title && {
653
+ template: attrs.color,
654
+ ud_icon: attrs.icon && {
655
+ tag: "standard_icon",
656
+ token: attrs.icon
657
+ },
658
+ title: {
659
+ tag: "plain_text",
660
+ content: attrs.title
661
+ },
662
+ subtitle: attrs.subtitle && {
663
+ tag: "plain_text",
664
+ content: attrs.subtitle
665
+ }
666
+ }
667
+ };
592
668
  await this.render(children, true);
593
669
  } else if (tag === "div") {
594
670
  this.flushText();
595
671
  await this.render(children);
596
- this.cardElements?.push({
672
+ this.card?.elements.push({
597
673
  tag: "markdown",
598
674
  text_align: attrs.align,
599
675
  text_size: attrs.size,
600
676
  content: this.textContent
601
677
  });
602
678
  this.textContent = "";
679
+ } else if (tag === "note") {
680
+ this.flushText();
681
+ this.noteElements = [];
682
+ await this.render(children);
683
+ this.flushText();
684
+ this.card?.elements.push({
685
+ tag: "note",
686
+ elements: this.noteElements
687
+ });
688
+ this.noteElements = void 0;
689
+ } else if (tag === "icon") {
690
+ this.flushText();
691
+ this.noteElements?.push({
692
+ tag: "standard_icon",
693
+ token: attrs.token
694
+ });
603
695
  }
604
696
  } else {
605
697
  await this.render(children);
@@ -622,16 +714,14 @@ var Internal = class _Internal {
622
714
  static {
623
715
  __name(this, "Internal");
624
716
  }
625
- processReponse(response) {
626
- const { code, msg } = response;
627
- if (code === 0) {
628
- return response;
629
- } else {
630
- this.bot.logger.debug("response: %o", response);
631
- throw new Error(`HTTP response with non-zero status (${code}) with message "${msg}"`);
632
- }
717
+ assertResponse(response) {
718
+ if (!response.data.code) return;
719
+ this.bot.logger.debug("response: %o", response.data);
720
+ const error = new import_core4.HTTP.Error(`request failed`);
721
+ error.response = response;
722
+ throw error;
633
723
  }
634
- static define(routes) {
724
+ static define(routes, extractData = true) {
635
725
  for (const path in routes) {
636
726
  for (const key in routes[path]) {
637
727
  const method = key;
@@ -655,7 +745,9 @@ var Internal = class _Internal {
655
745
  } else if (args.length > 1) {
656
746
  throw new Error(`too many arguments for ${path}, received ${raw}`);
657
747
  }
658
- return this.processReponse((await this.bot.http(method, url, config)).data);
748
+ const response = await this.bot.http(method, url, config);
749
+ this.assertResponse(response.data);
750
+ return extractData ? response.data.data : response.data;
659
751
  };
660
752
  }
661
753
  }
@@ -665,9 +757,6 @@ var Internal = class _Internal {
665
757
 
666
758
  // src/types/api.ts
667
759
  Internal.define({
668
- "/event/v1/outbound_ip": {
669
- GET: "listEventOutboundIp"
670
- },
671
760
  "/auth/v3/tenant_access_token/internal": {
672
761
  POST: "tenantAccessTokenInternalAuth"
673
762
  },
@@ -680,15 +769,20 @@ Internal.define({
680
769
  "/auth/v3/tenant_access_token": {
681
770
  POST: "tenantAccessTokenAuth"
682
771
  },
772
+ "/auth/v3/app_ticket/resend": {
773
+ POST: "appTicketResendAuth"
774
+ }
775
+ }, false);
776
+ Internal.define({
777
+ "/event/v1/outbound_ip": {
778
+ GET: "listEventOutboundIp"
779
+ },
683
780
  "/authen/v1/oidc/access_token": {
684
781
  POST: "createAuthenOidcAccessToken"
685
782
  },
686
783
  "/authen/v1/oidc/refresh_access_token": {
687
784
  POST: "createAuthenOidcRefreshAccessToken"
688
785
  },
689
- "/auth/v3/app_ticket/resend": {
690
- POST: "appTicketResendAuth"
691
- },
692
786
  "/authen/v1/user_info": {
693
787
  GET: "getAuthenUserInfo"
694
788
  },
@@ -3215,39 +3309,39 @@ var LarkBot = class extends import_core5.Bot {
3215
3309
  }
3216
3310
  async getMessage(channelId, messageId, recursive = true) {
3217
3311
  const data = await this.internal.getImMessage(messageId);
3218
- const message = await decodeMessage(this, data.data.items[0], recursive);
3312
+ const message = await decodeMessage(this, data.items[0], recursive);
3219
3313
  const im = await this.internal.getImChat(channelId);
3220
- message.channel.type = im.data.chat_mode === "p2p" ? import_core5.Universal.Channel.Type.DIRECT : import_core5.Universal.Channel.Type.TEXT;
3314
+ message.channel.type = im.chat_mode === "p2p" ? import_core5.Universal.Channel.Type.DIRECT : import_core5.Universal.Channel.Type.TEXT;
3221
3315
  return message;
3222
3316
  }
3223
3317
  async getMessageList(channelId, before) {
3224
- const { data: messages } = await this.internal.listImMessage({ container_id_type: "chat", container_id: channelId, page_token: before });
3318
+ const messages = await this.internal.listImMessage({ container_id_type: "chat", container_id: channelId, page_token: before });
3225
3319
  const data = await Promise.all(messages.items.reverse().map((data2) => decodeMessage(this, data2)));
3226
3320
  return { data, next: data[0]?.id };
3227
3321
  }
3228
3322
  async getUser(userId, guildId) {
3229
3323
  const data = await this.internal.getContactUser(userId);
3230
- return decodeUser(data.data.user);
3324
+ return decodeUser(data.user);
3231
3325
  }
3232
3326
  async getChannel(channelId) {
3233
- const { data } = await this.internal.getImChat(channelId);
3234
- return decodeChannel(channelId, data);
3327
+ const chat = await this.internal.getImChat(channelId);
3328
+ return decodeChannel(channelId, chat);
3235
3329
  }
3236
3330
  async getChannelList(guildId) {
3237
3331
  return { data: [await this.getChannel(guildId)] };
3238
3332
  }
3239
3333
  async getGuild(guildId) {
3240
- const { data } = await this.internal.getImChat(guildId);
3241
- return decodeGuild(data);
3334
+ const chat = await this.internal.getImChat(guildId);
3335
+ return decodeGuild(chat);
3242
3336
  }
3243
3337
  async getGuildList(after) {
3244
- const { data: guilds } = await this.internal.listImChat({ page_token: after });
3245
- return { data: guilds.items.map(decodeGuild), next: guilds.page_token };
3338
+ const chats = await this.internal.listImChat({ page_token: after });
3339
+ return { data: chats.items.map(decodeGuild), next: chats.page_token };
3246
3340
  }
3247
3341
  async getGuildMemberList(guildId, after) {
3248
- const { data: users } = await this.internal.getImChatMembers(guildId, { page_token: after });
3249
- const data = users.items.map((v) => ({ user: { id: v.member_id, name: v.name }, name: v.name }));
3250
- return { data, next: users.page_token };
3342
+ const members = await this.internal.getImChatMembers(guildId, { page_token: after });
3343
+ const data = members.items.map((v) => ({ user: { id: v.member_id, name: v.name }, name: v.name }));
3344
+ return { data, next: members.page_token };
3251
3345
  }
3252
3346
  };
3253
3347
  ((LarkBot2) => {