Skip to content

Commit

Permalink
fix(lark): set isDirent on interaction events
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 31, 2024
1 parent 6f91d20 commit e0d1084
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
13 changes: 8 additions & 5 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseResponse, Lark, MessageContent } from './types'
import { extractIdType } from './utils'

export class LarkMessageEncoder<C extends Context = Context> extends MessageEncoder<C, LarkBot<C>> {
private quote: string | undefined
private quote: Dict | undefined
private textContent = ''
private richContent: MessageContent.RichText.Paragraph[] = []
private card: MessageContent.Card | undefined
Expand All @@ -14,11 +14,14 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
async post(data?: any) {
try {
let resp: BaseResponse & { data?: Lark.Message }
if (this.quote) {
resp = await this.bot.internal.replyImMessage(this.quote, data)
if (this.quote?.id) {
resp = await this.bot.internal.replyImMessage(this.quote.id, {
...data,
reply_in_thread: this.quote.replyInThread,
})
} else {
data.receive_id = this.channelId
resp = await this.bot.internal?.createImMessage(data, {
resp = await this.bot.internal.createImMessage(data, {
receive_id_type: extractIdType(this.channelId),
})
}
Expand Down Expand Up @@ -174,7 +177,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
// platform does not support sharp
} else if (type === 'quote') {
await this.flush()
this.quote = attrs.id
this.quote = attrs
} else if (type === 'img' || type === 'image') {
const image_key = await this.createImage(attrs.src || attrs.url)
this.textContent += `![${attrs.alt ?? '图片'}](${image_key})`
Expand Down
2 changes: 2 additions & 0 deletions adapters/lark/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16681,6 +16681,8 @@ export interface ReplyImMessageRequest {
content: string
/** 消息类型,包括:text、post、image、file、audio、media、sticker、interactive、share_card、share_user */
msg_type: string
/** 是否以话题形式回复。取值为 true 时将以话题形式回复。注意:如果要回复的消息已经是话题形式的消息,则默认以话题形式进行回复。 */
reply_in_thread?: boolean
/** 由开发者生成的唯一字符串序列,用于回复消息请求去重;持有相同uuid的请求1小时内至多成功执行一次 */
uuid?: string
}
Expand Down
16 changes: 16 additions & 0 deletions adapters/lark/src/types/message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,21 @@ declare module '../event' {
open_chat_id: string
}
}
/**
* 机器人自定义菜单事件
* @see https://open.feishu.cn/document/client-docs/bot-v3/events/menu
*/
'application.bot.menu_v6': {
operator: {
operator_name: string
operator_id: {
union_id: string
user_id: string
open_id: string
}
}
event_key: string
timestamp: number
}
}
}
12 changes: 12 additions & 0 deletions adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ export async function adaptSession<C extends Context>(bot: LarkBot<C>, body: Eve
adaptSender(body.event.sender, session)
await adaptMessage(bot, body.event, session)
break
case 'application.bot.menu_v6':
if (body.event.event_key.startsWith('command:')) {
session.type = 'interaction/command'
session.content = body.event.event_key.slice(8)
session.channelId = body.event.operator.operator_id.open_id
session.userId = body.event.operator.operator_id.open_id
session.isDirect = true
}
break
case 'card.action.trigger':
if (body.event.action.value?._satori_type === 'command') {
session.type = 'interaction/command'
Expand Down Expand Up @@ -114,6 +123,9 @@ export async function adaptSession<C extends Context>(bot: LarkBot<C>, body: Eve
session.channelId = body.event.context.open_chat_id
session.guildId = body.event.context.open_chat_id
session.userId = body.event.operator.open_id
const { data } = await bot.internal.getImChat(session.channelId)
// TODO: add channel data
session.isDirect = data.chat_mode === 'p2p'
}
break
}
Expand Down

0 comments on commit e0d1084

Please sign in to comment.