@satorijs/adapter-lark 3.11.0 → 3.11.2

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/src/message.ts CHANGED
@@ -11,8 +11,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
11
11
  private textContent = ''
12
12
  private richContent: MessageContent.RichText.Paragraph[] = []
13
13
  private card: MessageContent.Card | undefined
14
- private noteElements: MessageContent.Card.NoteElement.InnerElement[] | undefined
15
- private actionElements: MessageContent.Card.Element[] = []
14
+ private elements: MessageContent.Card.Element[] = []
16
15
  private isForm = false
17
16
 
18
17
  public editMessageIds: string[] | undefined
@@ -79,20 +78,11 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
79
78
  }
80
79
  }
81
80
 
82
- private flushText(button = false) {
83
- if ((this.textContent || !button) && this.actionElements.length) {
84
- this.card!.elements.push({ tag: 'action', actions: this.actionElements, layout: 'flow' })
85
- this.actionElements = []
86
- }
87
- if (this.textContent) {
88
- this.richContent.push([{ tag: 'md', text: this.textContent }])
89
- if (this.noteElements) {
90
- this.noteElements.push({ tag: 'plain_text', content: this.textContent })
91
- } else if (this.card) {
92
- this.card.elements.push({ tag: 'markdown', content: this.textContent })
93
- }
94
- this.textContent = ''
95
- }
81
+ private flushText() {
82
+ if (!this.textContent) return
83
+ this.richContent.push([{ tag: 'md', text: this.textContent }])
84
+ this.elements.push({ tag: 'markdown', content: this.textContent })
85
+ this.textContent = ''
96
86
  }
97
87
 
98
88
  async flush() {
@@ -100,13 +90,11 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
100
90
  if (!this.card && !this.richContent.length) return
101
91
 
102
92
  if (this.card) {
103
- this.bot.logger.debug('card', JSON.stringify(this.card.elements))
93
+ // strip undefined properties
94
+ this.bot.logger.debug('card %o', JSON.parse(JSON.stringify(this.card)))
104
95
  await this.post({
105
96
  msg_type: 'interactive',
106
- content: JSON.stringify({
107
- header: this.card.header,
108
- elements: this.card.elements,
109
- }),
97
+ content: JSON.stringify(this.card),
110
98
  })
111
99
  } else {
112
100
  await this.post({
@@ -181,14 +169,14 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
181
169
  })
182
170
  }
183
171
 
184
- private createBehavior(attrs: Dict) {
172
+ private createBehaviors(attrs: Dict) {
185
173
  const behaviors: MessageContent.Card.ActionBehavior[] = []
186
174
  if (attrs.type === 'link') {
187
175
  behaviors.push({
188
176
  type: 'open_url',
189
177
  default_url: attrs.href,
190
178
  })
191
- } else if (attrs.type === 'input') {
179
+ } else if (attrs.type === 'input' || attrs.type === 'submit') {
192
180
  behaviors.push({
193
181
  type: 'callback',
194
182
  value: {
@@ -248,26 +236,24 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
248
236
  } else if (type === 'hr') {
249
237
  this.flushText()
250
238
  this.richContent.push([{ tag: 'hr' }])
251
- this.card?.elements.push({ tag: 'hr' })
239
+ this.elements.push({ tag: 'hr' })
252
240
  } else if (type === 'form') {
253
241
  this.flushText()
254
- const length = this.card?.elements.length
242
+ const parent = this.elements
243
+ parent.push({
244
+ tag: 'form',
245
+ name: attrs.name || 'Form',
246
+ elements: this.elements = [],
247
+ })
255
248
  this.isForm = true
256
249
  await this.render(children)
257
250
  this.isForm = false
258
- if (this.card?.elements.length > length) {
259
- const elements = this.card?.elements.splice(length)
260
- this.card.elements.push({
261
- tag: 'form',
262
- name: attrs.name || 'Form',
263
- elements,
264
- })
265
- }
251
+ this.elements = parent
266
252
  } else if (type === 'input') {
267
253
  if (attrs.type === 'checkbox') {
268
254
  this.flushText()
269
255
  await this.render(children)
270
- this.card?.elements.push({
256
+ this.elements.push({
271
257
  tag: 'checker',
272
258
  name: (attrs.argument ? '@@' : attrs.option ? `@${attrs.option}=` : '') + attrs.name,
273
259
  checked: attrs.value,
@@ -278,20 +264,17 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
278
264
  })
279
265
  this.textContent = ''
280
266
  } else if (attrs.type === 'submit') {
281
- this.flushText(true)
267
+ this.flushText()
282
268
  await this.render(children)
283
- this.card?.elements.push({
269
+ this.elements.push({
284
270
  tag: 'button',
285
271
  name: attrs.name,
286
272
  text: {
287
273
  tag: 'plain_text',
288
274
  content: this.textContent,
289
275
  },
290
- action_type: 'form_submit',
291
- value: {
292
- _satori_type: 'command',
293
- content: attrs.text,
294
- },
276
+ form_action_type: 'submit',
277
+ behaviors: this.createBehaviors(attrs),
295
278
  })
296
279
  this.textContent = ''
297
280
  } else {
@@ -311,18 +294,9 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
311
294
  default_value: attrs.value,
312
295
  disabled: attrs.disabled,
313
296
  required: attrs.required,
297
+ behaviors: this.createBehaviors(attrs),
314
298
  }
315
- if (this.isForm) {
316
- this.card?.elements.push(input)
317
- } else {
318
- this.card?.elements.push({
319
- tag: 'action',
320
- actions: [{
321
- ...input,
322
- behaviors: this.createBehavior(attrs),
323
- }],
324
- })
325
- }
299
+ this.elements.push(input)
326
300
  }
327
301
  } else if (type === 'select') {
328
302
  this.flushText()
@@ -338,6 +312,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
338
312
  content: attrs.placeholder,
339
313
  },
340
314
  options: [],
315
+ behaviors: this.createBehaviors(attrs),
341
316
  }
342
317
  for (const child of children) {
343
318
  if (child.type !== 'option') continue
@@ -351,29 +326,17 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
351
326
  })
352
327
  this.textContent = ''
353
328
  }
354
- if (this.isForm) {
355
- this.card?.elements.push(select)
356
- } else {
357
- this.card?.elements.push({
358
- tag: 'action',
359
- actions: [{
360
- ...select,
361
- behaviors: this.createBehavior(attrs),
362
- }],
363
- })
364
- }
329
+ this.elements.push(select)
365
330
  } else if (type === 'button') {
366
- this.card ??= { elements: [] }
367
- this.flushText(true)
331
+ this.flushText()
368
332
  await this.render(children)
369
- this.actionElements.push({
333
+ this.elements.push({
370
334
  tag: 'button',
371
335
  text: {
372
336
  tag: 'plain_text',
373
337
  content: this.textContent,
374
338
  },
375
339
  disabled: attrs.disabled,
376
- behaviors: this.createBehavior(attrs),
377
340
  type: attrs['lark:type'],
378
341
  size: attrs['lark:size'],
379
342
  width: attrs['lark:width'],
@@ -389,12 +352,24 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
389
352
  tag: 'plain_text',
390
353
  content: attrs['lark:disabled-tips'],
391
354
  },
355
+ behaviors: this.createBehaviors(attrs),
392
356
  })
393
357
  this.textContent = ''
394
- } else if (type === 'button-group') {
358
+ } else if (type === 'div') {
395
359
  this.flushText()
396
360
  await this.render(children)
397
- this.flushText()
361
+ this.elements.push({
362
+ tag: 'markdown',
363
+ text_align: attrs.align,
364
+ text_size: attrs.size,
365
+ content: this.textContent,
366
+ margin: attrs.margin,
367
+ icon: attrs.icon && {
368
+ tag: 'standard_icon',
369
+ token: attrs.icon,
370
+ },
371
+ })
372
+ this.textContent = ''
398
373
  } else if (type.startsWith('lark:') || type.startsWith('feishu:')) {
399
374
  const tag = type.slice(type.split(':', 1)[0].length + 1)
400
375
  if (tag === 'share-chat') {
@@ -424,10 +399,10 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
424
399
  } else if (tag === 'card') {
425
400
  await this.flush()
426
401
  this.card = {
427
- elements: [],
402
+ schema: '2.0',
428
403
  header: attrs.title && {
429
404
  template: attrs.color,
430
- ud_icon: attrs.icon && {
405
+ icon: attrs.icon && {
431
406
  tag: 'standard_icon',
432
407
  token: attrs.icon,
433
408
  },
@@ -440,39 +415,15 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
440
415
  content: attrs.subtitle,
441
416
  },
442
417
  },
418
+ body: {
419
+ elements: this.elements = [],
420
+ },
443
421
  }
444
422
  await this.render(children, true)
445
- } else if (tag === 'div') {
446
- this.flushText()
447
- await this.render(children)
448
- this.card?.elements.push({
449
- tag: 'markdown',
450
- text_align: attrs.align,
451
- text_size: attrs.size,
452
- content: this.textContent,
453
- })
454
- this.textContent = ''
455
- } else if (tag === 'note') {
456
- this.flushText()
457
- this.noteElements = []
458
- await this.render(children)
459
- this.flushText()
460
- this.card?.elements.push({
461
- tag: 'note',
462
- elements: this.noteElements,
463
- })
464
- this.noteElements = undefined
465
- } else if (tag === 'icon') {
466
- this.flushText()
467
- this.noteElements?.push({
468
- tag: 'standard_icon',
469
- token: attrs.token,
470
- })
471
423
  } else if (tag === 'column-set') {
472
424
  this.flushText()
473
- const parent = this.card
474
425
  const columns: MessageContent.Card.ColumnElement[] = []
475
- parent?.elements.push({
426
+ this.elements.push({
476
427
  tag: 'column_set',
477
428
  margin: attrs.margin,
478
429
  flex_mode: attrs.flexMode,
@@ -481,12 +432,13 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
481
432
  background_style: attrs.backgroundStyle,
482
433
  columns,
483
434
  })
435
+ const parent = this.elements
484
436
  for (const child of children) {
485
437
  if (child.type !== 'lark:column' && child.type !== 'feishu:column') {
486
438
  // throw unexpected?
487
439
  continue
488
440
  }
489
- this.card = { elements: [] }
441
+ this.elements = []
490
442
  await this.render(child.children)
491
443
  this.flushText()
492
444
  columns.push({
@@ -497,11 +449,30 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
497
449
  vertical_align: child.attrs.verticalAlign ?? 'center',
498
450
  vertical_spacing: child.attrs.verticalSpacing ?? '0px',
499
451
  background_style: child.attrs.backgroundStyle,
500
- elements: this.card.elements,
452
+ elements: this.elements,
501
453
  })
502
454
  }
503
- this.card = parent
455
+ this.elements = parent
504
456
  }
457
+ } else if (type === 'button-group') {
458
+ this.flushText()
459
+ const parent = this.elements
460
+ this.elements = []
461
+ await this.render(children)
462
+ this.flushText()
463
+ parent.push({
464
+ tag: 'column_set',
465
+ margin: attrs.margin,
466
+ flex_mode: attrs.flexMode,
467
+ horizontal_align: attrs.horizontalAlign,
468
+ horizontal_spacing: attrs.horizontalSpacing,
469
+ background_style: attrs.backgroundStyle,
470
+ columns: this.elements.map((element) => ({
471
+ tag: 'column',
472
+ elements: [element],
473
+ })),
474
+ })
475
+ this.elements = parent
505
476
  } else {
506
477
  await this.render(children)
507
478
  }