Skip to content

Commit

Permalink
feat(lark): export name updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 27, 2023
1 parent 12ddfef commit 6f50c11
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion adapters/lark/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@satorijs/adapter-lark",
"description": "Feishu Adapter for Satorijs",
"description": "Lark / Feishu Adapter for Satorijs",
"version": "2.0.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class HttpServer extends Adapter.Server<FeishuBot> {
logger.debug('received decryped event: %o', body)
this.dispatchSession(body)

// Feishu requires 200 OK response to make sure event is received
// Lark requires 200 OK response to make sure event is received
return ctx.status = 200
})

Expand Down
4 changes: 2 additions & 2 deletions adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export class LarkMessenger extends Messenger<FeishuBot> {
session.app.emit(session, 'send', session)
this.results.push(session)
} catch (e) {
// try to extract error message from Feishu API
// try to extract error message from Lark API
if (Quester.isAxiosError(e)) {
if (e.response?.data?.code) {
const generalErrorMsg = `Check error code at https://open.larksuite.com/document/ukTMukTMukTM/ugjM14COyUjL4ITN`
e.message += ` (Feishu error code ${e.response.data.code}: ${e.response.data.msg ?? generalErrorMsg})`
e.message += ` (Lark error code ${e.response.data.code}: ${e.response.data.msg ?? generalErrorMsg})`
}
}
this.errors.push(e)
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/types/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseResponse, Internal } from '.'

/**
* Feishu defines three types of token:
* Lark defines three types of token:
* - app_access_token: to access the API in an app (published on App Store).
* - tenant_access_token: to access the API as an enterprise or a team (tenant).
* *We commonly use this one*
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface EventHeader<T extends string> {
export interface Events {}
export type EventName = keyof Events

// In fact, this is the 2.0 version of the event sent by Feishu.
// In fact, this is the 2.0 version of the event sent by Lark.
// And only the 2.0 version has the `schema` field.
export type EventSkeleton<T extends EventName, Event, Header = EventHeader<T>> = {
schema: '2.0'
Expand Down
12 changes: 6 additions & 6 deletions adapters/lark/src/types/guild.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Dict } from '@satorijs/satori'

import { Feishu } from '.'
import { Lark } from '.'
import { Internal } from './internal'
import { Paginated, Pagination } from './utils'

declare module '.' {
export namespace Feishu {
export namespace Lark {
export interface Guild {
avatar: string
name: string
Expand Down Expand Up @@ -33,7 +33,7 @@ declare module '.' {
}

export interface GuildMember {
member_id_type: Feishu.UserIdType
member_id_type: Lark.UserIdType
member_id: string
name: string
tenant_key: string
Expand All @@ -42,11 +42,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: Feishu.UserIdType }>): Promise<{ data: Paginated<Feishu.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: Feishu.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: Feishu.UserIdType }>): Promise<{ data: Paginated<GuildMember> }>
getGuildMembers(chat_id: string, params: Pagination<{ member_id_type: Lark.UserIdType }>): Promise<{ data: Paginated<GuildMember> }>
}
}

Expand Down
11 changes: 6 additions & 5 deletions adapters/lark/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export * from './internal'

export * from './auth'
export * from './event'
export * from './guild'
export * from './message'

export namespace Feishu {
export namespace Lark {
/**
* A user in Feishu has several different IDs.
* A user in Lark has several different IDs.
* @see https://open.larksuite.com/document/home/user-identity-introduction/introduction
*/
export interface UserIds {
Expand All @@ -18,8 +17,8 @@ export namespace Feishu {
}

/**
* Identify a user in Feishu.
* This behaves like {@link Feishu.UserIds}, but it only contains *open_id*.
* Identify a user in Lark.
* This behaves like {@link Lark.UserIds}, but it only contains *open_id*.
* (i.e. the id_type is always `open_id`)
*/
export interface UserIdentifiers {
Expand All @@ -36,3 +35,5 @@ export namespace Feishu {
*/
export type ReceiveIdType = UserIdType | 'email' | 'chat_id'
}

export { Lark as Feishu }
18 changes: 9 additions & 9 deletions adapters/lark/src/types/message/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feishu, Internal } from '..'
import { Internal, Lark } from '..'
import { Paginated, Pagination } from '../utils'

import { MessageContent } from './content'
Expand All @@ -21,11 +21,11 @@ export interface MessageContentMap {
}
export type MessageContentType<T extends MessageType> = T extends keyof MessageContentMap ? MessageContentMap[T] : any

export interface Sender extends Feishu.UserIdentifiers {
export interface Sender extends Lark.UserIdentifiers {
sender_type: string
tenant_key: string
}
export interface Mention extends Feishu.UserIdentifiers {
export interface Mention extends Lark.UserIdentifiers {
key: string
name: string
tenant_key: string
Expand All @@ -39,7 +39,7 @@ declare module '../event' {
*/
'im.message.receive_v1': EventSkeleton<'im.message.receive_v1', {
sender: {
sender_id: Feishu.UserIds
sender_id: Lark.UserIds
sender_type?: string
tenant_key: string
}
Expand All @@ -54,7 +54,7 @@ declare module '../event' {
content: string
mentions: {
key: string
id: Feishu.UserIds
id: Lark.UserIds
name: string
tenant_key: string
}[]
Expand All @@ -66,7 +66,7 @@ declare module '../event' {
*/
'im.message.message_read_v1': EventSkeleton<'im.message.message_read_v1', {
reader: {
reader_id: Feishu.UserIds
reader_id: Lark.UserIds
read_time: string
tenant_key: string
}
Expand Down Expand Up @@ -160,7 +160,7 @@ export interface Message {
}

export interface ReadUser {
user_id_type: Feishu.UserIdType
user_id_type: Lark.UserIdType
user_id: string
timestamp: string
tenant_key: string
Expand All @@ -169,15 +169,15 @@ export interface ReadUser {
declare module '../internal' {
export interface Internal {
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create */
sendMessage(receive_id_type: Feishu.ReceiveIdType, message: MessagePayload): Promise<BaseResponse & { data: Message }>
sendMessage(receive_id_type: Lark.ReceiveIdType, message: MessagePayload): Promise<BaseResponse & { data: Message }>
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/reply */
replyMessage(message_id: string, message: MessagePayload): Promise<BaseResponse & { data: Message }>
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/get */
getMessage(message_id: string): Promise<BaseResponse & { data: Message }>
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/delete */
deleteMessage(message_id: string): Promise<BaseResponse>
/** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/read_users */
getMessageReadUsers(message_id: string, params: Pagination<{ user_id_type: Feishu.UserIdType }>): Promise<BaseResponse & { data: Paginated<ReadUser> }>
getMessageReadUsers(message_id: string, params: Pagination<{ user_id_type: Lark.UserIdType }>): Promise<BaseResponse & { data: Paginated<ReadUser> }>
}
}

Expand Down
10 changes: 5 additions & 5 deletions adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import crypto from 'crypto'
import { defineProperty, h, Session, trimSlash } from '@satorijs/satori'

import { FeishuBot } from './bot'
import { AllEvents, Events, Feishu, MessageContentType, MessageType } from './types'
import { AllEvents, Events, Lark, MessageContentType, MessageType } from './types'

export type Sender =
| {
sender_id: Feishu.UserIds
sender_id: Lark.UserIds
sender_type?: string
tenant_key: string
}
| (Feishu.UserIdentifiers & { sender_type?: string; tenant_key: string })
| (Lark.UserIdentifiers & { sender_type?: string; tenant_key: string })

export function adaptSender(sender: Sender, session: Session): Session {
let userId: string | undefined
Expand Down Expand Up @@ -39,7 +39,7 @@ export function adaptMessage(bot: FeishuBot, data: Events['im.message.receive_v1
break
}

// Feishu's `at` Element would be `@user_id` in text
// Lark's `at` Element would be `@user_id` in text
text.split(' ').forEach((word) => {
if (word.startsWith('@')) {
const mention = data.message.mentions.find((mention) => mention.key === word)
Expand Down Expand Up @@ -95,7 +95,7 @@ export function adaptSession(bot: FeishuBot, body: AllEvents): Session {
* Get ID type from id string
* @see https://open.larksuite.com/document/home/user-identity-introduction/introduction
*/
export function extractIdType(id: string): Feishu.ReceiveIdType {
export function extractIdType(id: string): Lark.ReceiveIdType {
if (id.startsWith('ou')) return 'open_id'
if (id.startsWith('on')) return 'union_id'
if (id.startsWith('oc')) return 'chat_id'
Expand Down

0 comments on commit 6f50c11

Please sign in to comment.