@alemonjs/bubble 2.1.0-alpha.2 → 2.1.0-alpha.4
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.
- package/lib/hook.d.ts +2 -2
- package/lib/index.js +7 -6
- package/lib/sdk/api.d.ts +3 -1
- package/lib/sdk/api.js +0 -3
- package/lib/send.d.ts +7 -3
- package/lib/send.js +40 -61
- package/package.json +1 -1
package/lib/hook.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ type MAP = {
|
|
|
9
9
|
'message.delete': undefined;
|
|
10
10
|
'message.reaction.add': undefined;
|
|
11
11
|
'message.reaction.remove': undefined;
|
|
12
|
-
'
|
|
13
|
-
'
|
|
12
|
+
'channel.create': undefined;
|
|
13
|
+
'channel.delete': undefined;
|
|
14
14
|
'guild.join': undefined;
|
|
15
15
|
'guild.exit': undefined;
|
|
16
16
|
'member.add': undefined;
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
|
3
3
|
import { getMaster, platform } from './config.js';
|
|
4
4
|
import { CDN_URL } from './sdk/api.js';
|
|
5
5
|
export { BubbleAPI as API, API_URL, GATEWAY_URL } from './sdk/api.js';
|
|
6
|
-
import {
|
|
6
|
+
import { sendToRoom, sendToUser } from './send.js';
|
|
7
7
|
export { OpCode } from './sdk/wss.types.js';
|
|
8
8
|
export { useClient, useValue } from './hook.js';
|
|
9
9
|
|
|
@@ -80,11 +80,11 @@ const main = () => {
|
|
|
80
80
|
active: {
|
|
81
81
|
send: {
|
|
82
82
|
channel: async (UserId, val) => {
|
|
83
|
-
const res = await
|
|
83
|
+
const res = await sendToRoom(client, { channel_id: UserId }, val);
|
|
84
84
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
85
85
|
},
|
|
86
86
|
user: async (OpenId, val) => {
|
|
87
|
-
const res = await
|
|
87
|
+
const res = await sendToUser(client, { author_id: OpenId }, val);
|
|
88
88
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
89
89
|
}
|
|
90
90
|
}
|
|
@@ -97,15 +97,16 @@ const main = () => {
|
|
|
97
97
|
const tag = event.tag;
|
|
98
98
|
if (tag === 'message.create') {
|
|
99
99
|
const ChannelId = String(event.value.channelId || '');
|
|
100
|
-
const res = await
|
|
100
|
+
const res = await sendToRoom(client, { channel_id: ChannelId, message_id: String(event.value.id || '') }, val);
|
|
101
101
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
102
102
|
}
|
|
103
103
|
else if (tag === 'private.message.create') {
|
|
104
104
|
const UserId = String(event.value.authorId || '');
|
|
105
105
|
const ChannelId = String(event.value.channelId || '');
|
|
106
|
-
const res = await
|
|
106
|
+
const res = await sendToUser(client, {
|
|
107
107
|
channel_id: ChannelId,
|
|
108
|
-
author_id: UserId
|
|
108
|
+
author_id: UserId,
|
|
109
|
+
message_id: String(event.value.id || '')
|
|
109
110
|
}, val);
|
|
110
111
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
111
112
|
}
|
package/lib/sdk/api.d.ts
CHANGED
package/lib/sdk/api.js
CHANGED
|
@@ -71,9 +71,6 @@ class BubbleAPI {
|
|
|
71
71
|
if (extra.threadId) {
|
|
72
72
|
form.append('threadId', String(extra.threadId));
|
|
73
73
|
}
|
|
74
|
-
if (extra.messageId) {
|
|
75
|
-
form.append('messageId', String(extra.messageId));
|
|
76
|
-
}
|
|
77
74
|
const value = getBubbleConfig();
|
|
78
75
|
const token = value.token;
|
|
79
76
|
const requestConfig = value.request_config ?? {};
|
package/lib/send.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { DataEnums } from 'alemonjs';
|
|
2
2
|
import { BubbleClient } from './sdk/wss';
|
|
3
3
|
type Client = typeof BubbleClient.prototype;
|
|
4
|
-
export declare const
|
|
5
|
-
channel_id: string | number;
|
|
4
|
+
export declare const sendToRoom: (client: Client, param: {
|
|
5
|
+
channel_id: string | number | null;
|
|
6
|
+
thread_id?: string | number | null;
|
|
7
|
+
message_id?: string | number | null;
|
|
6
8
|
}, val: DataEnums[]) => Promise<import("alemonjs").Result<any>[]>;
|
|
7
|
-
export declare const
|
|
9
|
+
export declare const sendToUser: (client: Client, param: {
|
|
8
10
|
author_id?: string | number;
|
|
9
11
|
channel_id?: string | number;
|
|
12
|
+
thread_id?: string | number;
|
|
13
|
+
message_id?: string | number;
|
|
10
14
|
}, val: DataEnums[]) => Promise<import("alemonjs").Result<any>[]>;
|
|
11
15
|
declare const _default: {};
|
|
12
16
|
export default _default;
|
package/lib/send.js
CHANGED
|
@@ -5,38 +5,15 @@ const ImageURLToBuffer = async (url) => {
|
|
|
5
5
|
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
6
6
|
return Buffer.from(arrayBuffer);
|
|
7
7
|
};
|
|
8
|
-
const
|
|
9
|
-
return rows.map(row => {
|
|
10
|
-
const val = row.value;
|
|
11
|
-
return {
|
|
12
|
-
type: 1,
|
|
13
|
-
components: val.map(button => {
|
|
14
|
-
const value = button.value;
|
|
15
|
-
let text = '';
|
|
16
|
-
if (typeof button.options?.data === 'object') {
|
|
17
|
-
text = button.options?.data.click;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
text = button.options.data;
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
type: 2,
|
|
24
|
-
custom_id: text,
|
|
25
|
-
style: 1,
|
|
26
|
-
label: typeof value === 'object' ? value.title : value
|
|
27
|
-
};
|
|
28
|
-
})
|
|
29
|
-
};
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
const sendchannel = async (client, param, val) => {
|
|
8
|
+
const sendToRoom = async (client, param, val) => {
|
|
33
9
|
try {
|
|
34
10
|
if (!val || val.length <= 0) {
|
|
35
11
|
return [];
|
|
36
12
|
}
|
|
37
13
|
const channelId = String(param?.channel_id ?? '');
|
|
14
|
+
const threadId = String(param?.thread_id ?? '');
|
|
15
|
+
const messageId = param?.message_id ? String(param?.message_id) : undefined;
|
|
38
16
|
const images = val.filter(item => item.type === 'Image' || item.type === 'ImageURL' || item.type === 'ImageFile');
|
|
39
|
-
const buttons = val.filter(item => item.type === 'BT.group');
|
|
40
17
|
const mds = val.filter(item => item.type === 'Markdown');
|
|
41
18
|
const content = val
|
|
42
19
|
.filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
|
|
@@ -92,19 +69,28 @@ const sendchannel = async (client, param, val) => {
|
|
|
92
69
|
bufferData = readFileSync(item.value);
|
|
93
70
|
}
|
|
94
71
|
}
|
|
95
|
-
const uploadRes = await client.uploadFile(bufferData, undefined, { channelId });
|
|
96
|
-
const fileAttachment = uploadRes?.file;
|
|
72
|
+
const uploadRes = await client.uploadFile(bufferData, undefined, { channelId, threadId, messageId: messageId });
|
|
73
|
+
const fileAttachment = uploadRes?.data?.file;
|
|
97
74
|
if (!fileAttachment) {
|
|
98
75
|
throw new Error('文件上传失败:未返回文件信息');
|
|
99
76
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
77
|
+
if (channelId) {
|
|
78
|
+
const res = await client.sendMessage(channelId, {
|
|
79
|
+
content: content,
|
|
80
|
+
type: 'text',
|
|
81
|
+
attachments: [fileAttachment]
|
|
82
|
+
});
|
|
83
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
84
|
+
}
|
|
85
|
+
if (threadId) {
|
|
86
|
+
const res = await client.sendDm(threadId, {
|
|
87
|
+
content: content,
|
|
88
|
+
type: 'text',
|
|
89
|
+
attachments: [fileAttachment]
|
|
90
|
+
});
|
|
91
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
92
|
+
}
|
|
93
|
+
return [createResult(ResultCode.Ok, '完成', null)];
|
|
108
94
|
}
|
|
109
95
|
let contentMd = '';
|
|
110
96
|
if (mds && mds.length > 0) {
|
|
@@ -157,24 +143,16 @@ const sendchannel = async (client, param, val) => {
|
|
|
157
143
|
}
|
|
158
144
|
});
|
|
159
145
|
}
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
content: contentMd || content,
|
|
171
|
-
components
|
|
172
|
-
});
|
|
173
|
-
return [createResult(ResultCode.Ok, '完成', res)];
|
|
174
|
-
}
|
|
175
|
-
if (content && content.length > 0) {
|
|
176
|
-
const res = await client.sendMessage(channelId, { content: contentMd || content });
|
|
177
|
-
return [createResult(ResultCode.Ok, '完成', res)];
|
|
146
|
+
if ((content && content.length > 0) || (contentMd && contentMd.length > 0)) {
|
|
147
|
+
if (channelId) {
|
|
148
|
+
const res = await client.sendMessage(channelId, { content: content ?? contentMd, type: 'text' });
|
|
149
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
150
|
+
}
|
|
151
|
+
if (threadId) {
|
|
152
|
+
const res = await client.sendDm(threadId, { content: content ?? contentMd, type: 'text' });
|
|
153
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
154
|
+
}
|
|
155
|
+
return [createResult(ResultCode.Ok, '完成', null)];
|
|
178
156
|
}
|
|
179
157
|
return [];
|
|
180
158
|
}
|
|
@@ -182,20 +160,21 @@ const sendchannel = async (client, param, val) => {
|
|
|
182
160
|
return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
|
|
183
161
|
}
|
|
184
162
|
};
|
|
185
|
-
const
|
|
163
|
+
const sendToUser = async (client, param, val) => {
|
|
186
164
|
if (!val || val.length <= 0) {
|
|
187
165
|
return [];
|
|
188
166
|
}
|
|
189
|
-
let
|
|
190
|
-
|
|
167
|
+
let threadId = param?.channel_id || param?.thread_id;
|
|
168
|
+
const messageId = param?.message_id;
|
|
169
|
+
if (!threadId && param.author_id) {
|
|
191
170
|
const dm = await client.getOrCreateDm(param.author_id);
|
|
192
|
-
|
|
171
|
+
threadId = dm?.id;
|
|
193
172
|
}
|
|
194
|
-
if (!
|
|
173
|
+
if (!threadId) {
|
|
195
174
|
return [];
|
|
196
175
|
}
|
|
197
|
-
return
|
|
176
|
+
return sendToRoom(client, { channel_id: null, thread_id: threadId, message_id: messageId }, val);
|
|
198
177
|
};
|
|
199
178
|
var send = {};
|
|
200
179
|
|
|
201
|
-
export { send as default,
|
|
180
|
+
export { send as default, sendToRoom, sendToUser };
|