Skip to content

Commit

Permalink
feat(lark): support new internal url spec
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 8, 2024
1 parent 6cb72b2 commit c62fb73
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
23 changes: 17 additions & 6 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>

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<{
Expand Down Expand Up @@ -129,6 +129,17 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
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<string[]> {
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 {
Expand Down
12 changes: 11 additions & 1 deletion adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,17 @@ export class LarkMessageEncoder<C extends Context = Context> 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']
Expand Down
4 changes: 2 additions & 2 deletions adapters/lark/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
16 changes: 8 additions & 8 deletions adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit c62fb73

Please sign in to comment.