From 3f0b3bd5c05f5750349bcfb553ef4320fc8a7033 Mon Sep 17 00:00:00 2001 From: Shigma Date: Wed, 30 Oct 2024 17:30:39 +0800 Subject: [PATCH] fix(lark): fix response body and post content --- adapters/lark/src/http.ts | 2 +- adapters/lark/src/message.ts | 16 ++++++++++------ adapters/lark/src/types/message/content.ts | 19 ++++++++++++++++--- adapters/lark/src/types/message/index.ts | 21 ++------------------- adapters/lark/src/utils.ts | 6 +++--- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/adapters/lark/src/http.ts b/adapters/lark/src/http.ts index a2e5918a..fa9e2c6b 100644 --- a/adapters/lark/src/http.ts +++ b/adapters/lark/src/http.ts @@ -72,10 +72,10 @@ export class HttpServer extends Adapter extends MessageEncoder> { 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 { @@ -67,7 +67,11 @@ export class LarkMessageEncoder extends MessageEnco } else { await this.post({ msg_type: 'post', - content: JSON.stringify({ zh_cn: this.richContent }), + content: JSON.stringify({ + zh_cn: { + content: this.richContent, + }, + }), }) } @@ -161,7 +165,7 @@ export class LarkMessageEncoder 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', diff --git a/adapters/lark/src/types/message/content.ts b/adapters/lark/src/types/message/content.ts index 024065e2..5f4d4fa7 100644 --- a/adapters/lark/src/types/message/content.ts +++ b/adapters/lark/src/types/message/content.ts @@ -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 } @@ -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 @@ -132,7 +145,7 @@ export namespace MessageComponent { | ImageElement | MediaElement | CodeBlockElement - | HorizontalRuleElement + | HRElement export type Paragraph = | InlineElement[] diff --git a/adapters/lark/src/types/message/index.ts b/adapters/lark/src/types/message/index.ts index 6bbc3cb2..4cedc97f 100644 --- a/adapters/lark/src/types/message/index.ts +++ b/adapters/lark/src/types/message/index.ts @@ -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 keyof MessageContentMap ? MessageContentMap[T] : any - declare module '../event' { export interface Events { /** @@ -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 diff --git a/adapters/lark/src/utils.ts b/adapters/lark/src/utils.ts index b7f89e41..c22a6917 100644 --- a/adapters/lark/src/utils.ts +++ b/adapters/lark/src/utils.ts @@ -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 = | { @@ -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 { - const json = JSON.parse(data.message.content) as MessageContentType + 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) { @@ -119,7 +119,7 @@ export async function adaptSession(bot: LarkBot, 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 { - const json = JSON.parse(body.body.content) as MessageContentType + 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) {