Skip to content

Commit

Permalink
feat(lark): add getGuild / getChannel / getGuildMemberList etc (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan authored Oct 13, 2023
1 parent 17b5788 commit bc27bbf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
25 changes: 25 additions & 0 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ export class LarkBot extends Bot<LarkBot.Config> {
const data = await this.internal.getUserInfo(userId)
return Utils.decodeUser(data.data)
}

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

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

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

async getGuildList(after?: string) {
const { data: guilds } = await this.internal.getCurrentUserGuilds({ 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.items.map(v => ({ user: { id: v.member_id, name: v.name }, name: v.name }))
return { data, next: users.page_token }
}
}

export namespace LarkBot {
Expand Down
7 changes: 4 additions & 3 deletions adapters/lark/src/types/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module '.' {
edit_permission: string
owner_id_type: string
owner_id: string
chat_id: string
chat_mode: string
chat_type: string
chat_tag: string
Expand All @@ -42,11 +43,11 @@ export interface GuildMember {
declare module './internal' {
export interface Internal {
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat/list */
getCurrentUserGuilds(params: Pagination<{ user_id_type: Lark.UserIdType }>): Promise<{ data: Paginated<Lark.Guild> }>
getCurrentUserGuilds(params?: Pagination<{ user_id_type?: Lark.UserIdType }>): Promise<{ data: Paginated<Lark.Guild> }>
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat/get */
getGuildInfo(chat_id: string, params: { user_id_type: string }): Promise<BaseResponse & { data: Lark.Guild }>
getGuildInfo(chat_id: string, params?: { user_id_type?: string }): Promise<BaseResponse & { data: Lark.Guild }>
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat-members/get */
getGuildMembers(chat_id: string, params: Pagination<{ member_id_type: Lark.UserIdType }>): Promise<{ data: Paginated<GuildMember> }>
getGuildMembers(chat_id: string, params?: Pagination<{ member_id_type?: Lark.UserIdType }>): Promise<{ data: Paginated<GuildMember> }>
}
}

Expand Down
19 changes: 18 additions & 1 deletion adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'crypto'
import { Message, User } from '@satorijs/protocol'
import { Channel, Guild, Message, User } from '@satorijs/protocol'
import { h, Session, trimSlash } from '@satorijs/satori'
import { FeishuBot, LarkBot } from './bot'
import { AllEvents, Events, Lark, Message as LarkMessage, MessageContentType, MessageType } from './types'
Expand Down Expand Up @@ -146,6 +146,23 @@ export function extractIdType(id: string): Lark.ReceiveIdType {
return 'user_id'
}

export function decodeChannel(guild: Lark.Guild): Channel {
return {
id: guild.chat_id,
type: Channel.Type.TEXT,
name: guild.name,
parentId: guild.chat_id,
}
}

export function decodeGuild(guild: Lark.Guild): Guild {
return {
id: guild.chat_id,
name: guild.name,
avatar: guild.avatar,
}
}

export function decodeUser(user: Lark.User): User {
return {
id: user.open_id,
Expand Down

0 comments on commit bc27bbf

Please sign in to comment.