diff --git a/adapters/lark/src/message.ts b/adapters/lark/src/message.ts index c8c46b54..d7b54cb6 100644 --- a/adapters/lark/src/message.ts +++ b/adapters/lark/src/message.ts @@ -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 extends MessageEncoder> { + declare referrer?: EventPayload + private quote: Dict | undefined private textContent = '' private richContent: MessageContent.RichText.Paragraph[] = [] @@ -17,19 +19,18 @@ export class LarkMessageEncoder 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, } } diff --git a/adapters/lark/src/types/event.ts b/adapters/lark/src/types/event.ts index c1ea1cbb..401d903e 100644 --- a/adapters/lark/src/types/event.ts +++ b/adapters/lark/src/types/event.ts @@ -8,7 +8,6 @@ export interface EventHeader { } 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. diff --git a/adapters/lark/src/utils.ts b/adapters/lark/src/utils.ts index 09b1e7b7..55d07e4a 100644 --- a/adapters/lark/src/utils.ts +++ b/adapters/lark/src/utils.ts @@ -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' @@ -75,9 +75,14 @@ export async function adaptMessage(bot: LarkBot, data: Events['im.message.receiv export async function adaptSession(bot: LarkBot, 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' @@ -95,6 +100,7 @@ export async function adaptSession(bot: LarkBot, 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