Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lark): add internal api #249

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
}

private async refreshToken() {
const { tenant_access_token: token } = await this.internal.getTenantAccessToken({
const { tenant_access_token: token } = await this.internal.tenantAccessTokenInternalAuth({
app_id: this.config.appId,
app_secret: this.config.appSecret,
})
Expand All @@ -62,53 +62,53 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
}

async editMessage(channelId: string, messageId: string, content: h.Fragment) {
await this.internal.updateMessage(messageId, {
await this.internal.updateImMessage(messageId, {
content: h.normalize(content).join(''),
msg_type: 'text',
})
}

async deleteMessage(channelId: string, messageId: string) {
await this.internal.deleteMessage(messageId)
await this.internal.deleteImMessage(messageId)
}

async getMessage(channelId: string, messageId: string) {
const data = await this.internal.getMessage(messageId)
return await Utils.decodeMessage(this, data.data)
const data = await this.internal.getImMessage(messageId)
return await Utils.decodeMessage(this, data.data.items[0])
}

async getMessageList(channelId: string, before?: string) {
const { data: messages } = await this.internal.getMessageList({ container_id_type: 'chat', container_id: channelId, page_token: before })
const { data: messages } = await this.internal.listImMessage({ container_id_type: 'chat', container_id: channelId, page_token: before })
const data = await Promise.all(messages.items.reverse().map(data => Utils.decodeMessage(this, data)))
return { data, next: data[0]?.id }
}

async getUser(userId: string, guildId?: string) {
const data = await this.internal.getContactUser(userId)
return Utils.decodeUser(data.data)
return Utils.decodeUser(data.data.user)
}

async getChannel(channelId: string) {
const { data } = await this.internal.getGuildInfo(channelId)
return Utils.decodeChannel(data)
const { data } = await this.internal.getImChat(channelId)
return Utils.decodeChannel(channelId, data)
}

async getChannelList(guildId: string) {
return { data: [await this.getChannel(guildId)] }
}

async getGuild(guildId: string) {
const { data } = await this.internal.getGuildInfo(guildId)
const { data } = await this.internal.getImChat(guildId)
return Utils.decodeGuild(data)
}

async getGuildList(after?: string) {
const { data: guilds } = await this.internal.getCurrentUserGuilds({ page_token: after })
const { data: guilds } = await this.internal.listImChat({ page_token: after })
return { data: guilds.items.map(Utils.decodeGuild), next: guilds.page_token }
}

async getGuildMemberList(guildId: string, after?: string) {
const { data: users } = await this.internal.getGuildMembers(guildId, { page_token: after })
const { data: users } = await this.internal.getImChatMembers(guildId, { page_token: after })
const data = users.items.map(v => ({ user: { id: v.member_id, name: v.name }, name: v.name }))
return { data, next: users.page_token }
}
Expand Down
16 changes: 10 additions & 6 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, h, MessageEncoder, Quester } from '@satorijs/satori'
import { LarkBot } from './bot'
import { BaseResponse, Message, MessageContent, MessageType } from './types'
import { BaseResponse, Lark, MessageContent, MessageType } from './types'
import { extractIdType } from './utils'

export interface Addition {
Expand All @@ -17,18 +17,21 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco

async post(data?: any) {
try {
let resp: BaseResponse & { data: Message }
let resp: BaseResponse & { data?: Lark.Message }
if (this.quote) {
resp = await this.bot.internal?.replyMessage(this.quote, data)
resp = await this.bot.internal.replyImMessage(this.quote, data)
} else {
data.receive_id = this.channelId
resp = await this.bot.internal?.sendMessage(extractIdType(this.channelId), data)
resp = await this.bot.internal?.createImMessage(data, {
receive_id_type: extractIdType(this.channelId),
})
}
const session = this.bot.session()
session.messageId = resp.data.message_id
session.timestamp = Number(resp.data.create_time) * 1000
session.userId = resp.data.sender.id
session.app.emit(session, 'send', session)
console.log(session)
this.results.push(session.event.message)
} catch (e) {
// try to extract error message from Lark API
Expand Down Expand Up @@ -79,7 +82,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco

if (type === 'img' || type === 'image') {
payload.append('image_type', 'message')
const { data } = await this.bot.internal.uploadImage(payload)
const { data } = await this.bot.internal.createImImage(payload)
return {
type: 'image',
file: {
Expand All @@ -105,7 +108,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
}
}
payload.append('file_name', filename)
const { data } = await this.bot.internal.uploadFile(payload)
const { data } = await this.bot.internal.createImFile(payload)
return {
type: msgType,
file: {
Expand Down Expand Up @@ -157,6 +160,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
if (attrs.src || attrs.url) {
await this.flush()
this.addition = await this.sendFile(type, attrs.src || attrs.url)
await this.flush()
}
break
case 'figure': // FIXME: treat as message element for now
Expand Down
Loading
Loading