@satorijs/adapter-lark 3.4.1 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,7 @@
1
1
  export * from './internal'
2
- export * from './auth'
3
2
  export * from './event'
4
- export * from './guild'
5
3
  export * from './message'
4
+ export * from './api'
6
5
 
7
6
  export namespace Lark {
8
7
  /**
@@ -1,4 +1,4 @@
1
- import { Dict, makeArray, Quester } from '@satorijs/satori'
1
+ import { Dict, HTTP, makeArray } from '@satorijs/core'
2
2
  import { LarkBot } from '../bot'
3
3
 
4
4
  export interface Internal {}
@@ -36,7 +36,7 @@ export class Internal {
36
36
  if (!args.length) throw new Error(`too few arguments for ${path}, received ${raw}`)
37
37
  return args.shift()
38
38
  })
39
- const config: Quester.RequestConfig = {}
39
+ const config: HTTP.RequestConfig = {}
40
40
  if (args.length === 1) {
41
41
  if (method === 'GET' || method === 'DELETE') {
42
42
  config.params = args[0]
@@ -1,10 +1,7 @@
1
- import { Internal, Lark } from '..'
2
- import { Paginated, Pagination } from '../utils'
3
-
1
+ import { Lark } from '..'
4
2
  import { MessageContent } from './content'
5
3
 
6
4
  export * from './content'
7
- export * from './asset'
8
5
 
9
6
  export type MessageType = 'text' | 'post' | 'image' | 'file' | 'audio' | 'media' | 'sticker' | 'interactive' | 'share_chat' | 'share_user'
10
7
 
@@ -22,16 +19,6 @@ export interface MessageContentMap {
22
19
 
23
20
  export type MessageContentType<T extends MessageType> = T extends keyof MessageContentMap ? MessageContentMap[T] : any
24
21
 
25
- export interface Sender extends Lark.UserIdentifiers {
26
- sender_type: string
27
- tenant_key: string
28
- }
29
- export interface Mention extends Lark.UserIdentifiers {
30
- key: string
31
- name: string
32
- tenant_key: string
33
- }
34
-
35
22
  declare module '../event' {
36
23
  export interface Events {
37
24
  /**
@@ -75,176 +62,3 @@ declare module '../event' {
75
62
  }>
76
63
  }
77
64
  }
78
-
79
- export interface MessagePayload {
80
- receive_id: string
81
- content: string
82
- msg_type: string
83
- }
84
-
85
- export interface Message {
86
- /**
87
- * The id of current message
88
- *
89
- * Should be started with `om_`
90
- */
91
- message_id: string
92
- /**
93
- * The id of the *root* message in reply chains
94
- * @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro#ac79c1c2
95
- */
96
- root_id: string
97
-
98
- /**
99
- * The id of the direct *parent* message in reply chains
100
- * @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro#ac79c1c2
101
- */
102
- parent_id: string
103
-
104
- /**
105
- * The message type.
106
- * @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/create_json
107
- */
108
- msg_type: MessageType
109
-
110
- /**
111
- * The timestamp when the message is created in milliseconds.
112
- */
113
- create_time: string
114
-
115
- /**
116
- * The timestamp when the message is last updated in milliseconds.
117
- */
118
- update_time: string
119
-
120
- /**
121
- * Whether the message is deleted.
122
- */
123
- deleted: boolean
124
-
125
- /**
126
- * Whether the message is updated.
127
- */
128
- updated: boolean
129
-
130
- /**
131
- * The id of the group / channel the message is sent to.
132
- */
133
- chat_id: string
134
-
135
- /**
136
- * The sender of the message.
137
- * Can be a user or an app.
138
- */
139
- sender: Sender
140
-
141
- /**
142
- * The body of the message.
143
- */
144
- body: {
145
- /**
146
- * The content of the message.
147
- * Should be a string that represents the JSON object contains the message content.
148
- */
149
- content: string
150
- }
151
-
152
- /**
153
- * Users mentioned in the message.
154
- */
155
- mentions: Mention[]
156
-
157
- /**
158
- * The id of the direct *parent* message in `merge and repost` chains.
159
- */
160
- upper_message_id: string
161
- }
162
-
163
- export interface ReadUser {
164
- user_id_type: Lark.UserIdType
165
- user_id: string
166
- timestamp: string
167
- tenant_key: string
168
- }
169
-
170
- export interface GetMessageListParams {
171
- /**
172
- * Currently there is only 'chat' available
173
- * @see https://open.larksuite.com/document/server-docs/im-v1/message/list
174
- */
175
- container_id_type: 'p2p' | 'chat'
176
- /**
177
- * Should be in the format like `oc_234jsi43d3ssi993d43545f`
178
- */
179
- container_id: string
180
- /** Timestamp in seconds */
181
- start_time?: string | number
182
- /** Timestamp in seconds */
183
- end_time?: string | number
184
- /** @default 'ByCreateTimeAsc' */
185
- sort_type?: 'ByCreateTimeAsc' | 'ByCreateTimeDesc'
186
- /** Range from 1 to 50 */
187
- page_size?: number
188
- /**
189
- * If the current page is the first page, this field should be omitted.
190
- * Otherwise you could use the `page_token` from the previous response to
191
- * get the next page.
192
- */
193
- page_token?: string
194
- }
195
-
196
- declare module '../internal' {
197
- export interface Internal {
198
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create */
199
- sendMessage(receive_id_type: Lark.ReceiveIdType, message: MessagePayload): Promise<BaseResponse & { data: Message }>
200
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/reply */
201
- replyMessage(message_id: string, message: MessagePayload): Promise<BaseResponse & { data: Message }>
202
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/update */
203
- updateMessage(message_id: string, message: Omit<MessagePayload, 'receive_id'>): Promise<BaseResponse & { data: Message }>
204
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/get */
205
- getMessage(message_id: string): Promise<BaseResponse & { data: Message }>
206
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/delete */
207
- deleteMessage(message_id: string): Promise<BaseResponse>
208
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/forward */
209
- forwardMessage(
210
- message_id: string,
211
- receive_id_type: Lark.ReceiveIdType,
212
- data: { receive_id: string },
213
- ): Promise<BaseResponse & { data: Message }>
214
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/merge_forward */
215
- mergeForwardMessage(
216
- receive_id_type: Lark.ReceiveIdType,
217
- data: { receive_id: string; message_id_list: string[] },
218
- ): Promise<BaseResponse & { data: { message: Message; invalid_message_id_list: string[] }}>
219
- /** @see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/read_users */
220
- getMessageReadUsers(message_id: string, params: Pagination<{ user_id_type: Lark.UserIdType }>): Promise<BaseResponse & { data: Paginated<ReadUser> }>
221
- /** @see https://open.larksuite.com/document/server-docs/im-v1/message/list */
222
- getMessageList(params: GetMessageListParams): Promise<BaseResponse & { data: Paginated<Message> }>
223
- }
224
- }
225
-
226
- Internal.define({
227
- '/im/v1/messages': {
228
- GET: 'getMessageList',
229
- },
230
- '/im/v1/messages?receive_id_type={receive_id_type}': {
231
- POST: 'sendMessage',
232
- },
233
- '/im/v1/messages/{message_id}/reply': {
234
- POST: 'replyMessage',
235
- },
236
- '/im/v1/messages/{message_id}': {
237
- GET: 'getMessage',
238
- PUT: 'updateMessage',
239
- DELETE: 'deleteMessage',
240
- },
241
- '/im/v1/messages/{message_id}/read_users': {
242
- GET: 'getMessageReadUsers',
243
- },
244
- '/im/v1/messages/{message_id}/forward?receive_id_type={receive_id_type}': {
245
- POST: 'forwardMessage',
246
- },
247
- '/im/v1/messages/merge_forward?receive_id_type={receive_id_type}': {
248
- POST: 'mergeForwardMessage',
249
- },
250
- })
package/src/utils.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import crypto from 'crypto'
2
- import { Context, h, Session, trimSlash, Universal } from '@satorijs/satori'
2
+ import { Context, h, Session, trimSlash, Universal } from '@satorijs/core'
3
3
  import { FeishuBot, LarkBot } from './bot'
4
- import { AllEvents, Events, Lark, Message as LarkMessage, MessageContentType, MessageType } from './types'
4
+ import { AllEvents, Events, GetImChatResponse, Lark, MessageContentType, MessageType } from './types'
5
5
 
6
6
  export type Sender =
7
7
  | {
8
- sender_id: Lark.UserIds
9
- sender_type?: string
10
- tenant_key: string
11
- }
8
+ sender_id: Lark.UserIds
9
+ sender_type?: string
10
+ tenant_key: string
11
+ }
12
12
  | (Lark.UserIdentifiers & { sender_type?: string; tenant_key: string })
13
13
 
14
14
  export function adaptSender(sender: Sender, session: Session): Session {
@@ -22,7 +22,7 @@ export function adaptSender(sender: Sender, session: Session): Session {
22
22
  return session
23
23
  }
24
24
 
25
- export function adaptMessage(bot: FeishuBot, data: Events['im.message.receive_v1']['event'], session: Session): Session {
25
+ export async function adaptMessage(bot: FeishuBot, data: Events['im.message.receive_v1']['event'], session: Session, details = true): Promise<Session> {
26
26
  const json = JSON.parse(data.message.content) as MessageContentType<MessageType>
27
27
  const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.server.config.selfUrl) + bot.config.path + '/assets'
28
28
  const content: (string | h)[] = []
@@ -62,30 +62,34 @@ export function adaptMessage(bot: FeishuBot, data: Events['im.message.receive_v1
62
62
  session.timestamp = +data.message.create_time
63
63
  session.messageId = data.message.message_id
64
64
  session.channelId = data.message.chat_id
65
+ session.guildId = data.message.chat_id
65
66
  session.content = content.map((c) => c.toString()).join(' ')
66
67
 
68
+ if (data.message.parent_id && details) {
69
+ session.quote = await bot.getMessage(session.channelId, data.message.parent_id, false)
70
+ }
67
71
  return session
68
72
  }
69
73
 
70
- export function adaptSession<C extends Context>(bot: FeishuBot<C>, body: AllEvents) {
74
+ export async function adaptSession<C extends Context>(bot: FeishuBot<C>, body: AllEvents) {
71
75
  const session = bot.session()
72
76
  session.setInternal('lark', body)
73
77
 
74
78
  switch (body.type) {
75
79
  case 'im.message.receive_v1':
76
80
  session.type = 'message'
77
- session.subtype = body.event.message.chat_type
81
+ session.subtype = body.event.message.chat_id
78
82
  if (session.subtype === 'p2p') session.subtype = 'private'
79
83
  session.isDirect = session.subtype === 'private'
80
84
  adaptSender(body.event.sender, session)
81
- adaptMessage(bot, body.event, session)
85
+ await adaptMessage(bot, body.event, session)
82
86
  break
83
87
  }
84
88
  return session
85
89
  }
86
90
 
87
91
  // TODO: This function has many duplicated code with `adaptMessage`, should refactor them
88
- export async function decodeMessage(bot: LarkBot, body: LarkMessage): Promise<Universal.Message> {
92
+ export async function decodeMessage(bot: LarkBot, body: Lark.Message, details = true): Promise<Universal.Message> {
89
93
  const json = JSON.parse(body.body.content) as MessageContentType<MessageType>
90
94
  const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.server.config.selfUrl) + bot.config.path + '/assets'
91
95
  const content: h[] = []
@@ -127,9 +131,17 @@ export async function decodeMessage(bot: LarkBot, body: LarkMessage): Promise<Un
127
131
  createdAt: +body.create_time,
128
132
  updatedAt: +body.update_time,
129
133
  id: body.message_id,
134
+ messageId: body.message_id,
135
+ user: {
136
+ id: body.sender.id,
137
+ },
138
+ channel: {
139
+ id: body.chat_id,
140
+ type: Universal.Channel.Type.TEXT,
141
+ },
130
142
  content: content.map((c) => c.toString()).join(' '),
131
143
  elements: content,
132
- quote: body.upper_message_id ? await bot.getMessage(body.chat_id, body.upper_message_id) : undefined,
144
+ quote: (body.upper_message_id && details) ? await bot.getMessage(body.chat_id, body.upper_message_id, false) : undefined,
133
145
  }
134
146
  }
135
147
 
@@ -145,16 +157,16 @@ export function extractIdType(id: string): Lark.ReceiveIdType {
145
157
  return 'user_id'
146
158
  }
147
159
 
148
- export function decodeChannel(guild: Lark.Guild): Universal.Channel {
160
+ export function decodeChannel(channelId: string, guild: GetImChatResponse['data']): Universal.Channel {
149
161
  return {
150
- id: guild.chat_id,
162
+ id: channelId,
151
163
  type: Universal.Channel.Type.TEXT,
152
164
  name: guild.name,
153
- parentId: guild.chat_id,
165
+ parentId: channelId,
154
166
  }
155
167
  }
156
168
 
157
- export function decodeGuild(guild: Lark.Guild): Universal.Guild {
169
+ export function decodeGuild(guild: Lark.ListChat): Universal.Guild {
158
170
  return {
159
171
  id: guild.chat_id,
160
172
  name: guild.name,