Skip to content

Commit

Permalink
fix(lark): fix response body and post content
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 30, 2024
1 parent 22e736b commit 3f0b3bd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion adapters/lark/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, LarkBot<

// dispatch message
bot.logger.debug('received decryped event: %o', body)
// @TODO: need await?
this.dispatchSession(body)

// Lark requires 200 OK response to make sure event is received
ctx.body = {}
return ctx.status = 200
})

Expand Down
16 changes: 10 additions & 6 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Context, h, MessageEncoder } from '@satorijs/core'
import { LarkBot } from './bot'
import { BaseResponse, Lark, MessageComponent } from './types'
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 textContent = ''
private richContent: MessageComponent.RichText.Paragraph[] = []
private cardElements: MessageComponent.Card.Element[] | undefined
private actionElements: MessageComponent.Card.ActionElement[] = []
private richContent: MessageContent.RichText.Paragraph[] = []
private cardElements: MessageContent.Card.Element[] | undefined
private actionElements: MessageContent.Card.ButtonElement[] = []

async post(data?: any) {
try {
Expand Down Expand Up @@ -67,7 +67,11 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
} else {
await this.post({
msg_type: 'post',
content: JSON.stringify({ zh_cn: this.richContent }),
content: JSON.stringify({
zh_cn: {
content: this.richContent,
},
}),
})
}

Expand Down Expand Up @@ -161,7 +165,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
this.cardElements?.push({ tag: 'hr' })
} else if (type === 'button') {
this.flushText()
const behaviors: MessageComponent.Card.ActionBehavior[] = []
const behaviors: MessageContent.Card.ActionBehavior[] = []
if (attrs.type === 'link') {
behaviors.push({
type: 'open_url',
Expand Down
19 changes: 16 additions & 3 deletions adapters/lark/src/types/message/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ declare global {
}
}

export namespace MessageComponent {
export interface MessageContent {
text: MessageContent.Text
post: MessageContent.RichText
image: MessageContent.Image
file: MessageContent.File
audio: MessageContent.Audio
media: MessageContent.Media
sticker: MessageContent.Sticker
share_chat: MessageContent.ShareChat
share_user: MessageContent.ShareUser
system: MessageContent.System
}

export namespace MessageContent {
export interface Text {
text: string
}
Expand Down Expand Up @@ -115,7 +128,7 @@ export namespace MessageComponent {
text: string
}

export interface HorizontalRuleElement extends BaseElement<'hr'> {}
export interface HRElement extends BaseElement<'hr'> {}

export interface MarkdownElement extends BaseElement<'md'> {
text: string
Expand All @@ -132,7 +145,7 @@ export namespace MessageComponent {
| ImageElement
| MediaElement
| CodeBlockElement
| HorizontalRuleElement
| HRElement

export type Paragraph =
| InlineElement[]
Expand Down
21 changes: 2 additions & 19 deletions adapters/lark/src/types/message/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { Lark } from '..'
import { MessageComponent } from './content'
import { Lark, MessageContent } from '..'

export * from './content'

export type MessageType = 'text' | 'post' | 'image' | 'file' | 'audio' | 'media' | 'sticker' | 'interactive' | 'share_chat' | 'share_user'

export interface MessageContentMap {
'text': MessageComponent.Text
'post': MessageComponent.RichText
'image': MessageComponent.Image
'file': MessageComponent.File
'audio': MessageComponent.Audio
'media': MessageComponent.Media
'sticker': MessageComponent.Sticker
'share_chat': MessageComponent.ShareChat
'share_user': MessageComponent.ShareUser
}

export type MessageContentType<T extends MessageType> = T extends keyof MessageContentMap ? MessageContentMap[T] : any

declare module '../event' {
export interface Events {
/**
Expand All @@ -38,7 +21,7 @@ declare module '../event' {
create_time: string
chat_id: string
chat_type: string
message_type: MessageType
message_type: keyof MessageContent
content: string
mentions: {
key: string
Expand Down
6 changes: 3 additions & 3 deletions adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'crypto'
import { Context, h, Session, trimSlash, Universal } from '@satorijs/core'
import { LarkBot } from './bot'
import { EventPayload, Events, GetImChatResponse, Lark, MessageContentType, MessageType } from './types'
import { EventPayload, Events, GetImChatResponse, Lark } from './types'

export type Sender =
| {
Expand All @@ -23,7 +23,7 @@ export function adaptSender(sender: Sender, session: Session): Session {
}

export async function adaptMessage(bot: LarkBot, data: Events['im.message.receive_v1'], session: Session, details = true): Promise<Session> {
const json = JSON.parse(data.message.content) as MessageContentType<MessageType>
const json = JSON.parse(data.message.content)
const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.server.config.selfUrl) + bot.config.path + '/assets'
const content: (string | h)[] = []
switch (data.message.message_type) {
Expand Down Expand Up @@ -119,7 +119,7 @@ export async function adaptSession<C extends Context>(bot: LarkBot<C>, body: Eve

// TODO: This function has many duplicated code with `adaptMessage`, should refactor them
export async function decodeMessage(bot: LarkBot, body: Lark.Message, details = true): Promise<Universal.Message> {
const json = JSON.parse(body.body.content) as MessageContentType<MessageType>
const json = JSON.parse(body.body.content)
const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.server.config.selfUrl) + bot.config.path + '/assets'
const content: h[] = []
switch (body.msg_type) {
Expand Down

0 comments on commit 3f0b3bd

Please sign in to comment.