From c62fb73eb00c4ac86b3730dcd736eb252b8ad796 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sun, 8 Dec 2024 23:10:13 +0800 Subject: [PATCH] feat(lark): support new internal url spec --- adapters/lark/src/bot.ts | 23 +++++++++++++++++------ adapters/lark/src/message.ts | 12 +++++++++++- adapters/lark/src/types/api.ts | 4 ++-- adapters/lark/src/utils.ts | 16 ++++++++-------- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/adapters/lark/src/bot.ts b/adapters/lark/src/bot.ts index 127ad9ea..11f97b44 100644 --- a/adapters/lark/src/bot.ts +++ b/adapters/lark/src/bot.ts @@ -26,15 +26,15 @@ export class LarkBot extends Bot ctx.plugin(HttpServer, this) - this.defineVirtualRoute('/:type/:message_id/:file_key', async ({ params }) => { - const type = params.type === 'image' ? 'image' : 'file' - const key = params.file_key - const messageId = params.message_id - const data = await this.internal.getImMessageResource(messageId, key, { type }) - return { data, status: 200 } + this.defineInternalRoute('/*path', async ({ params, method, headers, body }) => { + return this.http(params.path, { method, data: body, headers }) }) } + getResourceUrl(type: string, message_id: string, file_key: string) { + return this.getInternalUrl(`/im/v1/messages/${message_id}/resources/${file_key}`, { type }) + } + async initialize() { await this.refreshToken() const { bot } = await this.http.get<{ @@ -129,6 +129,17 @@ export class LarkBot extends Bot const data = members.items.map(v => ({ user: { id: v.member_id, name: v.name }, name: v.name })) return { data, next: members.page_token } } + + async createUpload(...uploads: Universal.Upload[]): Promise { + return await Promise.all(uploads.map(async (upload) => { + const response = await this.internal.createImFile({ + file_name: upload.filename, + file_type: upload.type, + file: new Blob([upload.data]), + }) + return this.getInternalUrl(`/im/v1/files/${response.file_key}`) + })) + } } export namespace LarkBot { diff --git a/adapters/lark/src/message.ts b/adapters/lark/src/message.ts index 8ed5d3a1..06dd7447 100644 --- a/adapters/lark/src/message.ts +++ b/adapters/lark/src/message.ts @@ -101,7 +101,17 @@ export class LarkMessageEncoder extends MessageEnco } async sendFile(_type: 'video' | 'audio' | 'file', attrs: any) { - const url = attrs.src || attrs.url + const url: string = attrs.src || attrs.url + const prefix = this.bot.getInternalUrl('/im/v1/files/') + if (url.startsWith(prefix)) { + const file_key = url.slice(prefix.length) + await this.post({ + msg_type: _type === 'video' ? 'media' : _type, + content: JSON.stringify({ file_key }), + }) + return + } + const { filename, type, data } = await this.bot.assetsQuester.file(url) let file_type: CreateImFileForm['file_type'] diff --git a/adapters/lark/src/types/api.ts b/adapters/lark/src/types/api.ts index df36e776..c692be4f 100644 --- a/adapters/lark/src/types/api.ts +++ b/adapters/lark/src/types/api.ts @@ -25654,11 +25654,11 @@ export interface GetProgressImBatchMessageResponse { } export interface CreateImImageResponse { /** 图片的key */ - image_key?: string + image_key: string } export interface CreateImFileResponse { /** 文件的key */ - file_key?: string + file_key: string } export interface CreateImMessageReactionResponse extends Lark.MessageReaction {} export interface DeleteImMessageReactionResponse extends Lark.MessageReaction {} diff --git a/adapters/lark/src/utils.ts b/adapters/lark/src/utils.ts index b3ecaf09..09b1e7b7 100644 --- a/adapters/lark/src/utils.ts +++ b/adapters/lark/src/utils.ts @@ -45,18 +45,18 @@ export async function adaptMessage(bot: LarkBot, data: Events['im.message.receiv break } case 'image': - content.push(h.image(bot.getVirtualUrl(`/image/${data.message.message_id}/${json.image_key}`))) + content.push(h.image(bot.getResourceUrl('image', data.message.message_id, json.image_key))) break case 'audio': - content.push(h.audio(bot.getVirtualUrl(`/file/${data.message.message_id}/${json.file_key}`))) + content.push(h.audio(bot.getResourceUrl('file', data.message.message_id, json.file_key))) break case 'media': - content.push(h.video(bot.getVirtualUrl(`/file/${data.message.message_id}/${json.file_key}`), { + content.push(h.video(bot.getResourceUrl('file', data.message.message_id, json.file_key), { poster: json.image_key, })) break case 'file': - content.push(h.file(bot.getVirtualUrl(`/file/${data.message.message_id}/${json.file_key}`))) + content.push(h.file(bot.getResourceUrl('file', data.message.message_id, json.file_key))) break } @@ -157,18 +157,18 @@ export async function decodeMessage(bot: LarkBot, body: Lark.Message, details = break } case 'image': - content.push(h.image(bot.getVirtualUrl(`/image/${body.message_id}/${json.image_key}`))) + content.push(h.image(bot.getResourceUrl('image', body.message_id, json.image_key))) break case 'audio': - content.push(h.audio(bot.getVirtualUrl(`/file/${body.message_id}/${json.file_key}`))) + content.push(h.audio(bot.getResourceUrl('file', body.message_id, json.file_key))) break case 'media': - content.push(h.video(bot.getVirtualUrl(`/file/${body.message_id}/${json.file_key}`), { + content.push(h.video(bot.getResourceUrl('file', body.message_id, json.file_key), { poster: json.image_key, })) break case 'file': - content.push(h.file(bot.getVirtualUrl(`/file/${body.message_id}/${json.file_key}`))) + content.push(h.file(bot.getResourceUrl('file', body.message_id, json.file_key))) break }