@zhin.js/adapter-milky 1.0.1 → 1.1.2
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/CHANGELOG.md +1046 -0
- package/{skills/milky/PERMITS.md → PERMITS.md} +2 -2
- package/README.md +60 -117
- package/adapters/milky/index.js +51 -0
- package/adapters/milky/index.ts +74 -0
- package/commands/milky/endpoint/add/[id]/index.js +3 -0
- package/commands/milky/endpoint/add/[id]/index.ts +3 -0
- package/commands/milky/endpoint/definition.js +19 -0
- package/commands/milky/endpoint/definition.ts +19 -0
- package/commands/milky/endpoint/list/index.js +3 -0
- package/commands/milky/endpoint/list/index.ts +3 -0
- package/commands/milky/endpoint/remove/[id]/index.js +3 -0
- package/commands/milky/endpoint/remove/[id]/index.ts +3 -0
- package/lib/client.d.ts +21 -0
- package/lib/client.js +33 -0
- package/lib/endpoint-management.d.ts +17 -0
- package/lib/endpoint-management.js +47 -0
- package/lib/endpoint.d.ts +7 -0
- package/lib/endpoint.js +6 -0
- package/lib/index.d.ts +6 -20
- package/lib/index.js +4 -35
- package/lib/milky-auth.d.ts +2 -0
- package/lib/milky-auth.js +16 -0
- package/lib/milky-runtime-state.d.ts +1 -0
- package/lib/milky-runtime-state.js +6 -0
- package/lib/protocol.d.ts +160 -0
- package/lib/protocol.js +491 -0
- package/lib/sse-client.d.ts +20 -0
- package/lib/sse-client.js +85 -0
- package/lib/sse-endpoint.d.ts +32 -0
- package/lib/sse-endpoint.js +207 -0
- package/lib/webhook-endpoint.d.ts +22 -0
- package/lib/webhook-endpoint.js +149 -0
- package/lib/ws-endpoint.d.ts +23 -0
- package/lib/ws-endpoint.js +233 -0
- package/lib/ws-types.d.ts +11 -0
- package/lib/ws-types.js +1 -0
- package/lib/wss-endpoint.d.ts +25 -0
- package/lib/wss-endpoint.js +199 -0
- package/package.json +78 -19
- package/plugin.js +14 -0
- package/schema.json +119 -0
- package/src/client.ts +80 -0
- package/src/endpoint-management.ts +74 -0
- package/src/endpoint.ts +26 -0
- package/src/index.ts +62 -47
- package/src/milky-auth.ts +14 -0
- package/src/milky-runtime-state.ts +7 -0
- package/src/protocol.ts +633 -0
- package/src/sse-client.ts +106 -0
- package/src/sse-endpoint.ts +258 -0
- package/src/webhook-endpoint.ts +196 -0
- package/src/ws-endpoint.ts +291 -0
- package/src/ws-types.ts +12 -0
- package/src/wss-endpoint.ts +241 -0
- package/lib/adapter.d.ts +0 -29
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -91
- package/lib/adapter.js.map +0 -1
- package/lib/api.d.ts +0 -10
- package/lib/api.d.ts.map +0 -1
- package/lib/api.js +0 -48
- package/lib/api.js.map +0 -1
- package/lib/endpoint-sse.d.ts +0 -31
- package/lib/endpoint-sse.d.ts.map +0 -1
- package/lib/endpoint-sse.js +0 -195
- package/lib/endpoint-sse.js.map +0 -1
- package/lib/endpoint-webhook.d.ts +0 -34
- package/lib/endpoint-webhook.d.ts.map +0 -1
- package/lib/endpoint-webhook.js +0 -187
- package/lib/endpoint-webhook.js.map +0 -1
- package/lib/endpoint-ws.d.ts +0 -35
- package/lib/endpoint-ws.d.ts.map +0 -1
- package/lib/endpoint-ws.js +0 -262
- package/lib/endpoint-ws.js.map +0 -1
- package/lib/endpoint-wss.d.ts +0 -34
- package/lib/endpoint-wss.d.ts.map +0 -1
- package/lib/endpoint-wss.js +0 -225
- package/lib/endpoint-wss.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types.d.ts +0 -75
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -5
- package/lib/types.js.map +0 -1
- package/lib/utils.d.ts +0 -35
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -165
- package/lib/utils.js.map +0 -1
- package/plugin.yml +0 -3
- package/skills/milky/SKILL.md +0 -65
- package/src/adapter.ts +0 -103
- package/src/api.ts +0 -63
- package/src/endpoint-sse.ts +0 -228
- package/src/endpoint-webhook.ts +0 -221
- package/src/endpoint-ws.ts +0 -296
- package/src/endpoint-wss.ts +0 -268
- package/src/types.ts +0 -73
- package/src/utils.ts +0 -181
package/src/endpoint-sse.ts
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Milky SSE Bot:应用 GET 协议端 /event,Accept: text/event-stream,解析 SSE 的 milky_event
|
|
3
|
-
*/
|
|
4
|
-
import EventSource from 'eventsource';
|
|
5
|
-
import { EventEmitter } from 'events';
|
|
6
|
-
import { formatCompact, Endpoint, Message, segment, SendOptions } from 'zhin.js';
|
|
7
|
-
import { callApi } from './api.js';
|
|
8
|
-
import type { MilkySseConfig, MilkyEvent } from './types.js';
|
|
9
|
-
import type { MilkyAdapter } from './adapter.js';
|
|
10
|
-
import {
|
|
11
|
-
formatMilkyMessagePayload,
|
|
12
|
-
parseMessageReceiveData,
|
|
13
|
-
toMilkyOutgoingSegments,
|
|
14
|
-
parseMilkyMessageId,
|
|
15
|
-
} from './utils.js';
|
|
16
|
-
|
|
17
|
-
export class MilkySseClient extends EventEmitter implements Endpoint<MilkySseConfig, MilkyEvent> {
|
|
18
|
-
$connected: boolean;
|
|
19
|
-
private es?: EventSource;
|
|
20
|
-
|
|
21
|
-
get logger() {
|
|
22
|
-
return this.adapter.plugin.logger;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
constructor(public adapter: MilkyAdapter, public $config: MilkySseConfig) {
|
|
26
|
-
super();
|
|
27
|
-
this.$connected = false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get $id() {
|
|
31
|
-
return this.$config.name;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private get eventUrl(): string {
|
|
35
|
-
const base = this.$config.baseUrl.replace(/\/$/, '');
|
|
36
|
-
const url = `${base}/event`;
|
|
37
|
-
const token = this.$config.access_token;
|
|
38
|
-
if (token) return `${url}${url.includes('?') ? '&' : '?'}access_token=${encodeURIComponent(token)}`;
|
|
39
|
-
return url;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private apiOptions() {
|
|
43
|
-
return { baseUrl: this.$config.baseUrl, access_token: this.$config.access_token };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async $connect(): Promise<void> {
|
|
47
|
-
const headers: Record<string, string> = { Accept: 'text/event-stream' };
|
|
48
|
-
if (this.$config.access_token) {
|
|
49
|
-
headers['Authorization'] = `Bearer ${this.$config.access_token}`;
|
|
50
|
-
}
|
|
51
|
-
this.es = new EventSource(this.eventUrl, { headers });
|
|
52
|
-
this.$connected = true;
|
|
53
|
-
if (!this.$config.access_token) {
|
|
54
|
-
this.logger.warn(formatCompact({ endpoint: this.$id, ok: false, error: 'missing access_token' }));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
this.es.addEventListener('milky_event', (e: MessageEvent) => {
|
|
58
|
-
try {
|
|
59
|
-
const event = JSON.parse(e.data) as MilkyEvent;
|
|
60
|
-
this.handleEvent(event);
|
|
61
|
-
} catch (err: unknown) {
|
|
62
|
-
this.emit('error', err instanceof Error ? err : new Error(String(err)));
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
this.es.onerror = (err: unknown) => {
|
|
67
|
-
this.emit('error', err instanceof Error ? err : new Error(String(err)));
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async $disconnect(): Promise<void> {
|
|
72
|
-
if (this.es) {
|
|
73
|
-
this.es.close();
|
|
74
|
-
this.es = undefined;
|
|
75
|
-
}
|
|
76
|
-
this.$connected = false;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
$formatMessage(event: MilkyEvent): Message<MilkyEvent> {
|
|
80
|
-
const data = parseMessageReceiveData(event);
|
|
81
|
-
if (!data) {
|
|
82
|
-
return Message.from(event, {
|
|
83
|
-
$id: '',
|
|
84
|
-
$adapter: 'milky',
|
|
85
|
-
$endpoint: this.$config.name,
|
|
86
|
-
$channel: { id: '', type: 'private' },
|
|
87
|
-
$sender: { id: '', name: '' },
|
|
88
|
-
$content: [],
|
|
89
|
-
$raw: '',
|
|
90
|
-
$timestamp: event.time ?? 0,
|
|
91
|
-
$recall: async () => {},
|
|
92
|
-
$reply: async () => '',
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
const payload = formatMilkyMessagePayload(
|
|
96
|
-
event,
|
|
97
|
-
data,
|
|
98
|
-
(id) => this.$recallMessage(id),
|
|
99
|
-
(channel, content) =>
|
|
100
|
-
this.adapter.sendMessage({
|
|
101
|
-
...channel,
|
|
102
|
-
context: 'milky',
|
|
103
|
-
endpoint: this.$config.name,
|
|
104
|
-
content: content as import('zhin.js').SendContent,
|
|
105
|
-
}),
|
|
106
|
-
'milky',
|
|
107
|
-
this.$config.name,
|
|
108
|
-
);
|
|
109
|
-
return Message.from(event, payload);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private handleEvent(event: MilkyEvent): void {
|
|
113
|
-
const data = parseMessageReceiveData(event);
|
|
114
|
-
if (data) {
|
|
115
|
-
const message = this.$formatMessage(event);
|
|
116
|
-
this.adapter.emit('message.receive', message);
|
|
117
|
-
this.logger.debug(
|
|
118
|
-
`${this.$config.name} recv ${message.$channel.type}(${message.$channel.id}):${segment.raw(message.$content)}`,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
async $sendMessage(options: SendOptions): Promise<string> {
|
|
124
|
-
const content = Array.isArray(options.content) ? options.content : [options.content];
|
|
125
|
-
const segments = content.map((c) =>
|
|
126
|
-
typeof c === 'string' ? { type: 'text' as const, data: { text: c } } : (c as { type: string; data?: Record<string, unknown> }),
|
|
127
|
-
);
|
|
128
|
-
const message = toMilkyOutgoingSegments(segments);
|
|
129
|
-
if (options.type === 'group') {
|
|
130
|
-
const result = await callApi(this.apiOptions(), 'send_group_message', {
|
|
131
|
-
group_id: parseInt(options.id, 10),
|
|
132
|
-
message,
|
|
133
|
-
});
|
|
134
|
-
const seq = (result as { message_seq?: number }).message_seq;
|
|
135
|
-
this.logger.debug(`${this.$config.name} send group(${options.id}):${segment.raw(options.content)}`);
|
|
136
|
-
return seq != null ? `group:${options.id}:${seq}` : '';
|
|
137
|
-
}
|
|
138
|
-
if (options.type === 'private') {
|
|
139
|
-
const result = await callApi(this.apiOptions(), 'send_private_message', {
|
|
140
|
-
user_id: parseInt(options.id, 10),
|
|
141
|
-
message,
|
|
142
|
-
});
|
|
143
|
-
const seq = (result as { message_seq?: number }).message_seq;
|
|
144
|
-
this.logger.debug(`${this.$config.name} send private(${options.id}):${segment.raw(options.content)}`);
|
|
145
|
-
return seq != null ? `friend:${options.id}:${seq}` : '';
|
|
146
|
-
}
|
|
147
|
-
throw new Error('Either group or private must be provided');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async $recallMessage(id: string): Promise<void> {
|
|
151
|
-
const parsed = parseMilkyMessageId(id);
|
|
152
|
-
if (!parsed) throw new Error(`Invalid message id: ${id}`);
|
|
153
|
-
if (parsed.message_scene === 'group') {
|
|
154
|
-
await callApi(this.apiOptions(), 'recall_group_message', {
|
|
155
|
-
group_id: parsed.peer_id,
|
|
156
|
-
message_seq: parsed.message_seq,
|
|
157
|
-
});
|
|
158
|
-
} else {
|
|
159
|
-
await callApi(this.apiOptions(), 'recall_private_message', {
|
|
160
|
-
user_id: parsed.peer_id,
|
|
161
|
-
message_seq: parsed.message_seq,
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async kickMember(groupId: number, userId: number, rejectAddRequest = false): Promise<boolean> {
|
|
167
|
-
await callApi(this.apiOptions(), 'kick_group_member', {
|
|
168
|
-
group_id: groupId,
|
|
169
|
-
user_id: userId,
|
|
170
|
-
reject_add_request: rejectAddRequest,
|
|
171
|
-
});
|
|
172
|
-
return true;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async muteMember(groupId: number, userId: number, duration = 600): Promise<boolean> {
|
|
176
|
-
await callApi(this.apiOptions(), 'set_group_member_mute', {
|
|
177
|
-
group_id: groupId,
|
|
178
|
-
user_id: userId,
|
|
179
|
-
duration,
|
|
180
|
-
});
|
|
181
|
-
return true;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
async muteAll(groupId: number, enable = true): Promise<boolean> {
|
|
185
|
-
await callApi(this.apiOptions(), 'set_group_whole_mute', { group_id: groupId, is_mute: enable });
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async setAdmin(groupId: number, userId: number, enable = true): Promise<boolean> {
|
|
190
|
-
await callApi(this.apiOptions(), 'set_group_member_admin', {
|
|
191
|
-
group_id: groupId,
|
|
192
|
-
user_id: userId,
|
|
193
|
-
is_set: enable,
|
|
194
|
-
});
|
|
195
|
-
return true;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
async setCard(groupId: number, userId: number, card: string): Promise<boolean> {
|
|
199
|
-
await callApi(this.apiOptions(), 'set_group_member_card', {
|
|
200
|
-
group_id: groupId,
|
|
201
|
-
user_id: userId,
|
|
202
|
-
card,
|
|
203
|
-
});
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
async setTitle(groupId: number, userId: number, title: string): Promise<boolean> {
|
|
208
|
-
await callApi(this.apiOptions(), 'set_group_member_special_title', {
|
|
209
|
-
group_id: groupId,
|
|
210
|
-
user_id: userId,
|
|
211
|
-
special_title: title,
|
|
212
|
-
});
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
async setGroupName(groupId: number, name: string): Promise<boolean> {
|
|
217
|
-
await callApi(this.apiOptions(), 'set_group_name', { group_id: groupId, new_group_name: name });
|
|
218
|
-
return true;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
async getMemberList(groupId: number): Promise<unknown[]> {
|
|
222
|
-
return callApi(this.apiOptions(), 'get_group_member_list', { group_id: groupId }) as Promise<unknown[]>;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async getGroupInfo(groupId: number): Promise<unknown> {
|
|
226
|
-
return callApi(this.apiOptions(), 'get_group_info', { group_id: groupId });
|
|
227
|
-
}
|
|
228
|
-
}
|
package/src/endpoint-webhook.ts
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Milky Webhook Bot:无长连接,在 router.post(path) 接收 POST 事件,鉴权后转 Message
|
|
3
|
-
*/
|
|
4
|
-
import { EventEmitter } from 'events';
|
|
5
|
-
import { formatCompact, Endpoint, Message, segment, SendOptions } from 'zhin.js';
|
|
6
|
-
import { firstHeader, firstQuery, registerFetchRoute, type Router, type RouterContext } from '@zhin.js/host-router/router';
|
|
7
|
-
import { callApi } from './api.js';
|
|
8
|
-
import type { MilkyWebhookConfig, MilkyEvent } from './types.js';
|
|
9
|
-
import type { MilkyAdapter } from './adapter.js';
|
|
10
|
-
import {
|
|
11
|
-
formatMilkyMessagePayload,
|
|
12
|
-
parseMessageReceiveData,
|
|
13
|
-
toMilkyOutgoingSegments,
|
|
14
|
-
parseMilkyMessageId,
|
|
15
|
-
} from './utils.js';
|
|
16
|
-
|
|
17
|
-
function getAccessTokenFromRequest(ctx: RouterContext): string | undefined {
|
|
18
|
-
const auth = firstHeader(ctx, 'authorization');
|
|
19
|
-
if (auth?.startsWith('Bearer ')) return auth.slice(7);
|
|
20
|
-
return firstQuery(ctx, 'access_token');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class MilkyWebhookEndpoint extends EventEmitter implements Endpoint<MilkyWebhookConfig, MilkyEvent> {
|
|
24
|
-
$connected: boolean = true;
|
|
25
|
-
|
|
26
|
-
get logger() {
|
|
27
|
-
return this.adapter.plugin.logger;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
constructor(
|
|
31
|
-
public adapter: MilkyAdapter,
|
|
32
|
-
public router: Router,
|
|
33
|
-
public $config: MilkyWebhookConfig,
|
|
34
|
-
) {
|
|
35
|
-
super();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get $id() {
|
|
39
|
-
return this.$config.name;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async $connect(): Promise<void> {
|
|
43
|
-
const path = this.$config.path.startsWith('/') ? this.$config.path : `/${this.$config.path}`;
|
|
44
|
-
const token = this.$config.access_token;
|
|
45
|
-
registerFetchRoute(this.router, 'POST', path, async (ctx: RouterContext) => {
|
|
46
|
-
const received = getAccessTokenFromRequest(ctx);
|
|
47
|
-
if (token && received !== token) {
|
|
48
|
-
ctx.status = 401;
|
|
49
|
-
ctx.body = { retcode: 401, message: 'Unauthorized' };
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const body = ctx.request.body;
|
|
53
|
-
if (!body || typeof body !== 'object') {
|
|
54
|
-
ctx.status = 400;
|
|
55
|
-
ctx.body = { retcode: 400, message: 'Invalid JSON' };
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const event = body as MilkyEvent;
|
|
59
|
-
this.handleEvent(event);
|
|
60
|
-
ctx.status = 200;
|
|
61
|
-
ctx.body = { retcode: 0 };
|
|
62
|
-
});
|
|
63
|
-
this.logger.info(formatCompact( { op: 'webhook', path }));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async $disconnect(): Promise<void> {
|
|
67
|
-
this.$connected = false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
$formatMessage(event: MilkyEvent): Message<MilkyEvent> {
|
|
71
|
-
const data = parseMessageReceiveData(event);
|
|
72
|
-
if (!data) {
|
|
73
|
-
return Message.from(event, {
|
|
74
|
-
$id: '',
|
|
75
|
-
$adapter: 'milky',
|
|
76
|
-
$endpoint: this.$config.name,
|
|
77
|
-
$channel: { id: '', type: 'private' },
|
|
78
|
-
$sender: { id: '', name: '' },
|
|
79
|
-
$content: [],
|
|
80
|
-
$raw: '',
|
|
81
|
-
$timestamp: event.time ?? 0,
|
|
82
|
-
$recall: async () => {},
|
|
83
|
-
$reply: async () => '',
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
const payload = formatMilkyMessagePayload(
|
|
87
|
-
event,
|
|
88
|
-
data,
|
|
89
|
-
(id) => this.$recallMessage(id),
|
|
90
|
-
(channel, content) =>
|
|
91
|
-
this.adapter.sendMessage({
|
|
92
|
-
...channel,
|
|
93
|
-
context: 'milky',
|
|
94
|
-
endpoint: this.$config.name,
|
|
95
|
-
content: content as import('zhin.js').SendContent,
|
|
96
|
-
}),
|
|
97
|
-
'milky',
|
|
98
|
-
this.$config.name,
|
|
99
|
-
);
|
|
100
|
-
return Message.from(event, payload);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
private handleEvent(event: MilkyEvent): void {
|
|
104
|
-
const data = parseMessageReceiveData(event);
|
|
105
|
-
if (data) {
|
|
106
|
-
const message = this.$formatMessage(event);
|
|
107
|
-
this.adapter.emit('message.receive', message);
|
|
108
|
-
this.logger.debug(
|
|
109
|
-
`${this.$config.name} recv ${message.$channel.type}(${message.$channel.id}):${segment.raw(message.$content)}`,
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private apiOptions() {
|
|
115
|
-
return { baseUrl: this.$config.baseUrl, access_token: this.$config.access_token };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async $sendMessage(options: SendOptions): Promise<string> {
|
|
119
|
-
const content = Array.isArray(options.content) ? options.content : [options.content];
|
|
120
|
-
const segments = content.map((c) =>
|
|
121
|
-
typeof c === 'string' ? { type: 'text' as const, data: { text: c } } : (c as { type: string; data?: Record<string, unknown> }),
|
|
122
|
-
);
|
|
123
|
-
const message = toMilkyOutgoingSegments(segments);
|
|
124
|
-
if (options.type === 'group') {
|
|
125
|
-
const result = await callApi(this.apiOptions(), 'send_group_message', {
|
|
126
|
-
group_id: parseInt(options.id, 10),
|
|
127
|
-
message,
|
|
128
|
-
});
|
|
129
|
-
const seq = (result as { message_seq?: number }).message_seq;
|
|
130
|
-
return seq != null ? `group:${options.id}:${seq}` : '';
|
|
131
|
-
}
|
|
132
|
-
if (options.type === 'private') {
|
|
133
|
-
const result = await callApi(this.apiOptions(), 'send_private_message', {
|
|
134
|
-
user_id: parseInt(options.id, 10),
|
|
135
|
-
message,
|
|
136
|
-
});
|
|
137
|
-
const seq = (result as { message_seq?: number }).message_seq;
|
|
138
|
-
return seq != null ? `friend:${options.id}:${seq}` : '';
|
|
139
|
-
}
|
|
140
|
-
throw new Error('Either group or private must be provided');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async $recallMessage(id: string): Promise<void> {
|
|
144
|
-
const parsed = parseMilkyMessageId(id);
|
|
145
|
-
if (!parsed) throw new Error(`Invalid message id: ${id}`);
|
|
146
|
-
if (parsed.message_scene === 'group') {
|
|
147
|
-
await callApi(this.apiOptions(), 'recall_group_message', {
|
|
148
|
-
group_id: parsed.peer_id,
|
|
149
|
-
message_seq: parsed.message_seq,
|
|
150
|
-
});
|
|
151
|
-
} else {
|
|
152
|
-
await callApi(this.apiOptions(), 'recall_private_message', {
|
|
153
|
-
user_id: parsed.peer_id,
|
|
154
|
-
message_seq: parsed.message_seq,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async kickMember(groupId: number, userId: number, rejectAddRequest = false): Promise<boolean> {
|
|
160
|
-
await callApi(this.apiOptions(), 'kick_group_member', {
|
|
161
|
-
group_id: groupId,
|
|
162
|
-
user_id: userId,
|
|
163
|
-
reject_add_request: rejectAddRequest,
|
|
164
|
-
});
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
async muteMember(groupId: number, userId: number, duration = 600): Promise<boolean> {
|
|
169
|
-
await callApi(this.apiOptions(), 'set_group_member_mute', {
|
|
170
|
-
group_id: groupId,
|
|
171
|
-
user_id: userId,
|
|
172
|
-
duration,
|
|
173
|
-
});
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async muteAll(groupId: number, enable = true): Promise<boolean> {
|
|
178
|
-
await callApi(this.apiOptions(), 'set_group_whole_mute', { group_id: groupId, is_mute: enable });
|
|
179
|
-
return true;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async setAdmin(groupId: number, userId: number, enable = true): Promise<boolean> {
|
|
183
|
-
await callApi(this.apiOptions(), 'set_group_member_admin', {
|
|
184
|
-
group_id: groupId,
|
|
185
|
-
user_id: userId,
|
|
186
|
-
is_set: enable,
|
|
187
|
-
});
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async setCard(groupId: number, userId: number, card: string): Promise<boolean> {
|
|
192
|
-
await callApi(this.apiOptions(), 'set_group_member_card', {
|
|
193
|
-
group_id: groupId,
|
|
194
|
-
user_id: userId,
|
|
195
|
-
card,
|
|
196
|
-
});
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async setTitle(groupId: number, userId: number, title: string): Promise<boolean> {
|
|
201
|
-
await callApi(this.apiOptions(), 'set_group_member_special_title', {
|
|
202
|
-
group_id: groupId,
|
|
203
|
-
user_id: userId,
|
|
204
|
-
special_title: title,
|
|
205
|
-
});
|
|
206
|
-
return true;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async setGroupName(groupId: number, name: string): Promise<boolean> {
|
|
210
|
-
await callApi(this.apiOptions(), 'set_group_name', { group_id: groupId, new_group_name: name });
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async getMemberList(groupId: number): Promise<unknown[]> {
|
|
215
|
-
return callApi(this.apiOptions(), 'get_group_member_list', { group_id: groupId }) as Promise<unknown[]>;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
async getGroupInfo(groupId: number): Promise<unknown> {
|
|
219
|
-
return callApi(this.apiOptions(), 'get_group_info', { group_id: groupId });
|
|
220
|
-
}
|
|
221
|
-
}
|