Skip to content

Commit

Permalink
feat(lark): support referrer api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 3, 2025
1 parent 5a987b3 commit 719d666
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
17 changes: 9 additions & 8 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Context, Dict, h, MessageEncoder } from '@satorijs/core'
import { LarkBot } from './bot'
import { CreateImFileForm, Lark, MessageContent } from './types'
import { CreateImFileForm, EventPayload, Lark, MessageContent } from './types'
import { extractIdType } from './utils'

export class LarkMessageEncoder<C extends Context = Context> extends MessageEncoder<C, LarkBot<C>> {
declare referrer?: EventPayload

private quote: Dict | undefined
private textContent = ''
private richContent: MessageContent.RichText.Paragraph[] = []
Expand All @@ -17,19 +19,18 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
try {
let resp: Lark.Message
let quote = this.quote
if (!quote) {
const payload = this.options?.session?.lark
if (payload?.type === 'im.message.receive_v1' && payload.event.message.thread_id) {
if (!quote && this.referrer) {
if (this.referrer.type === 'im.message.receive_v1' && this.referrer.event.message.thread_id) {
quote = {
id: payload.event.message.message_id,
id: this.referrer.event.message.message_id,
replyInThread: true,
}
} else if (payload?.type === 'card.action.trigger') {
} else if (this.referrer.type === 'card.action.trigger') {
// cannot determine whether the card is in thread or not
const { items: [message] } = await this.bot.internal.getImMessage(payload.event.context.open_message_id)
const { items: [message] } = await this.bot.internal.getImMessage(this.referrer.event.context.open_message_id)
if (message?.thread_id) {
quote = {
id: payload.event.context.open_message_id,
id: this.referrer.event.context.open_message_id,
replyInThread: true,
}
}
Expand Down
1 change: 0 additions & 1 deletion adapters/lark/src/types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface EventHeader<K extends keyof Events> {
}

export interface Events {}
export type EventName = keyof Events

// In fact, this is the 2.0 version of the event sent by Lark.
// And only the 2.0 version has the `schema` field.
Expand Down
8 changes: 7 additions & 1 deletion adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'crypto'
import { Context, h, Session, Universal } from '@satorijs/core'
import { Context, h, pick, Session, Universal } from '@satorijs/core'
import { LarkBot } from './bot'
import { EventPayload, Events, GetImChatResponse, Lark } from './types'

Expand Down Expand Up @@ -75,9 +75,14 @@ export async function adaptMessage(bot: LarkBot, data: Events['im.message.receiv
export async function adaptSession<C extends Context>(bot: LarkBot<C>, body: EventPayload) {
const session = bot.session()
session.setInternal('lark', body)
session.event.referrer = {
type: body.type,
event: {},
}

switch (body.type) {
case 'im.message.receive_v1':
session.event.referrer.event.message = pick(body.event.message, ['message_id', 'thread_id'])
session.type = 'message'
session.subtype = body.event.message.chat_type
if (session.subtype === 'p2p') session.subtype = 'private'
Expand All @@ -95,6 +100,7 @@ export async function adaptSession<C extends Context>(bot: LarkBot<C>, body: Eve
}
break
case 'card.action.trigger':
session.event.referrer.event.context = pick(body.event.context, ['open_message_id'])
if (body.event.action.value?._satori_type === 'command') {
session.type = 'interaction/command'
let content = body.event.action.value.content
Expand Down

0 comments on commit 719d666

Please sign in to comment.