@asabot/core 0.1.0 → 0.2.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.
- package/CHANGELOG.md +17 -0
- package/README.md +78 -13
- package/asabot.config.example.yaml +47 -29
- package/dist/adapters/feishu/adapter.d.ts +19 -0
- package/dist/adapters/feishu/adapter.js +621 -0
- package/dist/adapters/feishu/api.d.ts +142 -0
- package/dist/adapters/feishu/api.js +306 -0
- package/dist/adapters/feishu/bot.d.ts +95 -0
- package/dist/adapters/feishu/bot.js +539 -0
- package/dist/adapters/feishu/cards.d.ts +93 -0
- package/dist/adapters/feishu/cards.js +106 -0
- package/dist/adapters/feishu/config.d.ts +13 -0
- package/dist/adapters/feishu/config.js +36 -0
- package/dist/adapters/feishu/events.d.ts +33 -0
- package/dist/adapters/feishu/events.js +14 -0
- package/dist/adapters/feishu/index.d.ts +8 -0
- package/dist/adapters/feishu/index.js +5 -0
- package/dist/adapters/feishu/message.d.ts +64 -0
- package/dist/adapters/feishu/message.js +197 -0
- package/dist/adapters/feishu/transport.d.ts +34 -0
- package/dist/adapters/feishu/transport.js +110 -0
- package/dist/adapters/index.d.ts +1 -7
- package/dist/adapters/index.js +75 -6
- package/dist/adapters/loader.d.ts +6 -0
- package/dist/adapters/loader.js +73 -0
- package/dist/adapters/media-source.d.ts +7 -0
- package/dist/adapters/media-source.js +137 -0
- package/dist/adapters/onebot11/adapter.d.ts +5 -0
- package/dist/adapters/onebot11/adapter.js +23 -11
- package/dist/adapters/onebot11/api.d.ts +4 -1
- package/dist/adapters/onebot11/api.js +21 -8
- package/dist/adapters/onebot11/errors.d.ts +3 -0
- package/dist/adapters/onebot11/errors.js +6 -0
- package/dist/adapters/virtual/index.js +7 -1
- package/dist/adapters/wecom/adapter.d.ts +14 -0
- package/dist/adapters/wecom/adapter.js +220 -0
- package/dist/adapters/wecom/bot.d.ts +71 -0
- package/dist/adapters/wecom/bot.js +312 -0
- package/dist/adapters/wecom/cards.d.ts +37 -0
- package/dist/adapters/wecom/cards.js +30 -0
- package/dist/adapters/wecom/config.d.ts +8 -0
- package/dist/adapters/wecom/config.js +10 -0
- package/dist/adapters/wecom/events.d.ts +93 -0
- package/dist/adapters/wecom/events.js +46 -0
- package/dist/adapters/wecom/index.d.ts +7 -0
- package/dist/adapters/wecom/index.js +21 -0
- package/dist/adapters/wecom/media.d.ts +50 -0
- package/dist/adapters/wecom/media.js +149 -0
- package/dist/adapters/wecom/message.d.ts +151 -0
- package/dist/adapters/wecom/message.js +116 -0
- package/dist/adapters/wecom/transport.d.ts +39 -0
- package/dist/adapters/wecom/transport.js +116 -0
- package/dist/adapters/weixin-clawbot/adapter.d.ts +16 -0
- package/dist/adapters/weixin-clawbot/adapter.js +173 -0
- package/dist/adapters/weixin-clawbot/bot.d.ts +41 -0
- package/dist/adapters/weixin-clawbot/bot.js +254 -0
- package/dist/adapters/weixin-clawbot/index.d.ts +2 -0
- package/dist/adapters/weixin-clawbot/index.js +19 -0
- package/dist/adapters/weixin-clawbot/media.d.ts +57 -0
- package/dist/adapters/weixin-clawbot/media.js +196 -0
- package/dist/adapters/weixin-clawbot/message.d.ts +12 -0
- package/dist/adapters/weixin-clawbot/message.js +79 -0
- package/dist/adapters/weixin-clawbot/protocol.d.ts +145 -0
- package/dist/adapters/weixin-clawbot/protocol.js +178 -0
- package/dist/adapters/weixin-clawbot/setup.d.ts +2 -0
- package/dist/adapters/weixin-clawbot/setup.js +121 -0
- package/dist/bootstrap.d.ts +5 -0
- package/dist/bootstrap.js +5 -20
- package/dist/cli.js +9 -3
- package/dist/core/adapter.d.ts +69 -3
- package/dist/core/adapter.js +11 -1
- package/dist/core/app.d.ts +12 -9
- package/dist/core/app.js +50 -51
- package/dist/core/bot-accounts.d.ts +28 -0
- package/dist/core/bot-accounts.js +78 -0
- package/dist/core/bot-policies.d.ts +30 -0
- package/dist/core/bot-policies.js +112 -0
- package/dist/core/bot.d.ts +19 -1
- package/dist/core/bot.js +13 -0
- package/dist/core/capabilities.d.ts +54 -0
- package/dist/core/capabilities.js +25 -0
- package/dist/core/config/schema.d.ts +61 -8
- package/dist/core/config/schema.js +15 -3
- package/dist/core/config/write.js +5 -1
- package/dist/core/connections.d.ts +84 -0
- package/dist/core/connections.js +326 -0
- package/dist/core/delivery.d.ts +53 -0
- package/dist/core/delivery.js +115 -0
- package/dist/core/dispatch/api.d.ts +3 -1
- package/dist/core/dispatch/api.js +12 -5
- package/dist/core/dispatch/dispatcher.d.ts +1 -0
- package/dist/core/dispatch/dispatcher.js +35 -15
- package/dist/core/dispatch/outgoing.d.ts +1 -0
- package/dist/core/dispatch/outgoing.js +5 -3
- package/dist/core/dispatch/trace.d.ts +2 -0
- package/dist/core/dispatch/types.d.ts +6 -0
- package/dist/core/dispatch/types.js +1 -2
- package/dist/core/errors.d.ts +4 -1
- package/dist/core/errors.js +7 -1
- package/dist/core/event/session.d.ts +3 -1
- package/dist/core/event/session.js +7 -4
- package/dist/core/event/types.d.ts +5 -1
- package/dist/core/identity.d.ts +18 -0
- package/dist/core/identity.js +31 -0
- package/dist/core/logger.js +5 -1
- package/dist/core/message/types.d.ts +2 -0
- package/dist/core/permission.d.ts +2 -1
- package/dist/core/permission.js +7 -2
- package/dist/core/plugin/context.d.ts +10 -10
- package/dist/core/plugin/context.js +20 -18
- package/dist/core/plugin/definition.d.ts +5 -0
- package/dist/core/plugin/execution.d.ts +3 -0
- package/dist/core/plugin/execution.js +3 -0
- package/dist/core/plugin/manager.d.ts +18 -4
- package/dist/core/plugin/manager.js +137 -45
- package/dist/core/plugin/tasks.d.ts +131 -24
- package/dist/core/plugin/tasks.js +517 -117
- package/dist/core/plugin/types.d.ts +3 -0
- package/dist/core/ratelimit.js +6 -1
- package/dist/core/storage/index.d.ts +3 -1
- package/dist/core/storage/index.js +52 -2
- package/dist/core/target.d.ts +24 -6
- package/dist/core/target.js +26 -6
- package/dist/core/utils/merge.d.ts +3 -0
- package/dist/core/utils/merge.js +18 -0
- package/dist/core/utils/redact.d.ts +6 -0
- package/dist/core/utils/redact.js +76 -0
- package/dist/index.d.ts +13 -5
- package/dist/index.js +7 -2
- package/dist/plugins/admin.js +30 -3
- package/dist/plugins/help.js +1 -0
- package/dist/plugins/history.d.ts +4 -0
- package/dist/plugins/history.js +13 -3
- package/dist/plugins/http.js +1 -0
- package/dist/plugins/long-message.js +20 -12
- package/dist/plugins/status.js +1 -0
- package/dist/server/auth.d.ts +5 -1
- package/dist/server/auth.js +11 -7
- package/dist/server/connection-setups.d.ts +126 -0
- package/dist/server/connection-setups.js +188 -0
- package/dist/server/groups.d.ts +1 -0
- package/dist/server/groups.js +8 -8
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +229 -29
- package/dist/server/schemas.d.ts +6 -2
- package/dist/server/settings.d.ts +66 -20
- package/dist/server/settings.js +158 -61
- package/dist/server/views.d.ts +95 -17
- package/dist/server/views.js +48 -13
- package/dist/testing/create-test-app.js +2 -2
- package/dist/testing/dispatch-fixture.js +2 -1
- package/dist/testing/fake-bot.d.ts +2 -1
- package/dist/testing/fake-bot.js +4 -1
- package/docs/DEPLOY.md +190 -0
- package/docs/FEISHU.md +170 -0
- package/docs/WECOM.md +107 -0
- package/docs/WEIXIN-CLAWBOT.md +56 -0
- package/package.json +29 -3
- package/web/dist/assets/index-BAbj8e7E.css +2 -0
- package/web/dist/assets/index-CMuBy6y5.js +51 -0
- package/web/dist/assets/{markdown-CDlgpI7K.js → markdown-MEN1AImu.js} +3 -3
- package/web/dist/index.html +2 -2
- package/web/dist/plugin-pages/_sdk.js +26 -1
- package/web/dist/assets/index-BI5GCpiI.css +0 -2
- package/web/dist/assets/index-C-rshHwD.js +0 -51
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { SendResult } from '../../core/bot.ts';
|
|
3
|
+
import type { AnyEvent, PlatformNoticeEvent } from '../../core/event/index.ts';
|
|
4
|
+
import type { Target } from '../../core/target.ts';
|
|
5
|
+
import type { WecomBot } from './bot.ts';
|
|
6
|
+
import type { WecomTemplateCard } from './cards.ts';
|
|
7
|
+
import { platform } from './config.ts';
|
|
8
|
+
export declare const eventFrame: z.ZodObject<{
|
|
9
|
+
headers: z.ZodObject<{
|
|
10
|
+
req_id: z.ZodString;
|
|
11
|
+
}, z.core.$strip>;
|
|
12
|
+
body: z.ZodIntersection<z.ZodObject<{
|
|
13
|
+
msgid: z.ZodString;
|
|
14
|
+
aibotid: z.ZodString;
|
|
15
|
+
from: z.ZodObject<{
|
|
16
|
+
userid: z.ZodString;
|
|
17
|
+
corpid: z.ZodOptional<z.ZodString>;
|
|
18
|
+
}, z.core.$strip>;
|
|
19
|
+
create_time: z.ZodOptional<z.ZodNumber>;
|
|
20
|
+
compat_info: z.ZodOptional<z.ZodUnknown>;
|
|
21
|
+
msgtype: z.ZodLiteral<"event">;
|
|
22
|
+
event: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
23
|
+
eventtype: z.ZodLiteral<"enter_chat">;
|
|
24
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
25
|
+
eventtype: z.ZodLiteral<"template_card_event">;
|
|
26
|
+
template_card_event: z.ZodObject<{
|
|
27
|
+
card_type: z.ZodString;
|
|
28
|
+
event_key: z.ZodString;
|
|
29
|
+
task_id: z.ZodString;
|
|
30
|
+
selected_items: z.ZodOptional<z.ZodObject<{
|
|
31
|
+
selected_item: z.ZodArray<z.ZodObject<{
|
|
32
|
+
question_key: z.ZodString;
|
|
33
|
+
option_ids: z.ZodObject<{
|
|
34
|
+
option_id: z.ZodArray<z.ZodString>;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
}, z.core.$strip>>;
|
|
37
|
+
}, z.core.$strip>>;
|
|
38
|
+
}, z.core.$strip>;
|
|
39
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
40
|
+
eventtype: z.ZodLiteral<"feedback_event">;
|
|
41
|
+
feedback_event: z.ZodObject<{
|
|
42
|
+
id: z.ZodString;
|
|
43
|
+
type: z.ZodLiteral<1 | 2 | 3>;
|
|
44
|
+
content: z.ZodOptional<z.ZodString>;
|
|
45
|
+
inaccurate_reason_list: z.ZodDefault<z.ZodArray<z.ZodNumber>>;
|
|
46
|
+
}, z.core.$strip>;
|
|
47
|
+
}, z.core.$strip>], "eventtype">;
|
|
48
|
+
}, z.core.$strip>, z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
49
|
+
chattype: z.ZodLiteral<"group">;
|
|
50
|
+
chatid: z.ZodString;
|
|
51
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
52
|
+
chattype: z.ZodOptional<z.ZodLiteral<"single">>;
|
|
53
|
+
}, z.core.$strip>], "chattype">>;
|
|
54
|
+
}, z.core.$strip>;
|
|
55
|
+
interface WecomNotice extends PlatformNoticeEvent {
|
|
56
|
+
readonly platform: typeof platform;
|
|
57
|
+
readonly bot: WecomBot;
|
|
58
|
+
readonly userId: string;
|
|
59
|
+
readonly corpId?: string;
|
|
60
|
+
readonly messageId: string;
|
|
61
|
+
readonly target: Target & {
|
|
62
|
+
selfId: string;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export interface WecomEnterChatEvent extends WecomNotice {
|
|
66
|
+
readonly name: 'notice.wecom.enter_chat';
|
|
67
|
+
readonly platformType: 'enter_chat';
|
|
68
|
+
replyWelcome(content: string | WecomTemplateCard): Promise<SendResult>;
|
|
69
|
+
}
|
|
70
|
+
export interface WecomCardActionEvent extends WecomNotice {
|
|
71
|
+
readonly name: 'notice.wecom.card.action';
|
|
72
|
+
readonly platformType: 'template_card_event';
|
|
73
|
+
readonly cardType: string;
|
|
74
|
+
readonly eventKey: string;
|
|
75
|
+
readonly taskId: string;
|
|
76
|
+
readonly selectedItems: readonly {
|
|
77
|
+
questionKey: string;
|
|
78
|
+
optionIds: readonly string[];
|
|
79
|
+
}[];
|
|
80
|
+
updateCard(card: WecomTemplateCard, userIds?: string[]): Promise<SendResult>;
|
|
81
|
+
}
|
|
82
|
+
export interface WecomFeedbackEvent extends WecomNotice {
|
|
83
|
+
readonly name: 'notice.wecom.feedback';
|
|
84
|
+
readonly platformType: 'feedback_event';
|
|
85
|
+
readonly feedbackId: string;
|
|
86
|
+
readonly feedbackType: 1 | 2 | 3;
|
|
87
|
+
readonly content?: string;
|
|
88
|
+
readonly inaccurateReasons: readonly number[];
|
|
89
|
+
}
|
|
90
|
+
export declare function isWecomEnterChat(event: AnyEvent): event is WecomEnterChatEvent;
|
|
91
|
+
export declare function isWecomCardAction(event: AnyEvent): event is WecomCardActionEvent;
|
|
92
|
+
export declare function isWecomFeedback(event: AnyEvent): event is WecomFeedbackEvent;
|
|
93
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { platform } from './config.js';
|
|
3
|
+
import { frameHeader, groupChat, inboundFields } from './message.js';
|
|
4
|
+
const selectedItems = z.object({
|
|
5
|
+
selected_item: z.array(z.object({
|
|
6
|
+
question_key: z.string(),
|
|
7
|
+
option_ids: z.object({ option_id: z.array(z.string()) }),
|
|
8
|
+
})),
|
|
9
|
+
});
|
|
10
|
+
export const eventFrame = frameHeader.extend({
|
|
11
|
+
body: inboundFields
|
|
12
|
+
.extend({
|
|
13
|
+
msgtype: z.literal('event'),
|
|
14
|
+
event: z.discriminatedUnion('eventtype', [
|
|
15
|
+
z.object({ eventtype: z.literal('enter_chat') }),
|
|
16
|
+
z.object({
|
|
17
|
+
eventtype: z.literal('template_card_event'),
|
|
18
|
+
template_card_event: z.object({
|
|
19
|
+
card_type: z.string().min(1),
|
|
20
|
+
event_key: z.string(),
|
|
21
|
+
task_id: z.string().min(1),
|
|
22
|
+
selected_items: selectedItems.optional(),
|
|
23
|
+
}),
|
|
24
|
+
}),
|
|
25
|
+
z.object({
|
|
26
|
+
eventtype: z.literal('feedback_event'),
|
|
27
|
+
feedback_event: z.object({
|
|
28
|
+
id: z.string().min(1),
|
|
29
|
+
type: z.literal([1, 2, 3]),
|
|
30
|
+
content: z.string().optional(),
|
|
31
|
+
inaccurate_reason_list: z.array(z.number().int()).default([]),
|
|
32
|
+
}),
|
|
33
|
+
}),
|
|
34
|
+
]),
|
|
35
|
+
})
|
|
36
|
+
.and(z.discriminatedUnion('chattype', [groupChat, z.object({ chattype: z.literal('single').optional() })])),
|
|
37
|
+
});
|
|
38
|
+
export function isWecomEnterChat(event) {
|
|
39
|
+
return event.platform === platform && event.name === 'notice.wecom.enter_chat' && 'replyWelcome' in event;
|
|
40
|
+
}
|
|
41
|
+
export function isWecomCardAction(event) {
|
|
42
|
+
return event.platform === platform && event.name === 'notice.wecom.card.action' && 'updateCard' in event;
|
|
43
|
+
}
|
|
44
|
+
export function isWecomFeedback(event) {
|
|
45
|
+
return event.platform === platform && event.name === 'notice.wecom.feedback' && 'feedbackId' in event;
|
|
46
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { WecomAdapter, type WecomAdapterOptions } from './adapter.ts';
|
|
2
|
+
export { isWecomBot, isWecomSession, type WecomBot, type WecomSession, type WecomStream } from './bot.ts';
|
|
3
|
+
export { type WecomCardSegment, type WecomTemplateCard, wecom } from './cards.ts';
|
|
4
|
+
export { type WecomConfig, wecomSchema } from './config.ts';
|
|
5
|
+
export { isWecomCardAction, isWecomEnterChat, isWecomFeedback, type WecomCardActionEvent, type WecomEnterChatEvent, type WecomFeedbackEvent, } from './events.ts';
|
|
6
|
+
export type { WecomHandlers, WecomTransport } from './transport.ts';
|
|
7
|
+
export declare const wecomDefinition: import("../../core/adapter.ts").AdapterDefinition;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineAdapter } from '../../core/adapter.js';
|
|
2
|
+
import { WecomAdapter } from './adapter.js';
|
|
3
|
+
import { wecomCapabilities } from './bot.js';
|
|
4
|
+
import { platform, wecomSchema } from './config.js';
|
|
5
|
+
export { WecomAdapter } from './adapter.js';
|
|
6
|
+
export { isWecomBot, isWecomSession } from './bot.js';
|
|
7
|
+
export { wecom } from './cards.js';
|
|
8
|
+
export { wecomSchema } from './config.js';
|
|
9
|
+
export { isWecomCardAction, isWecomEnterChat, isWecomFeedback, } from './events.js';
|
|
10
|
+
export const wecomDefinition = defineAdapter({
|
|
11
|
+
id: platform,
|
|
12
|
+
platform,
|
|
13
|
+
name: '企业微信',
|
|
14
|
+
version: '0.1.0',
|
|
15
|
+
description: '企业微信智能机器人,使用 BotID 和 Secret 建立长连接。',
|
|
16
|
+
connectionTypes: ['long-connection'],
|
|
17
|
+
capabilities: wecomCapabilities,
|
|
18
|
+
configSchema: wecomSchema,
|
|
19
|
+
accountId: (config) => config.botId,
|
|
20
|
+
create: (config, host) => new WecomAdapter(config, host),
|
|
21
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type SendMediaMsgBody } from '@wecom/aibot-node-sdk';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { type KnownSegment } from '../../core/message/index.ts';
|
|
4
|
+
import type { WecomTransport } from './transport.ts';
|
|
5
|
+
type MediaKind = 'image' | 'file' | 'video' | 'audio';
|
|
6
|
+
export type MediaSegment = Extract<KnownSegment, {
|
|
7
|
+
type: MediaKind;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const attachmentSchema: z.ZodObject<{
|
|
10
|
+
url: z.ZodString;
|
|
11
|
+
aeskey: z.ZodString;
|
|
12
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
13
|
+
}, z.core.$strip>;
|
|
14
|
+
type Attachment = z.output<typeof attachmentSchema>;
|
|
15
|
+
export declare class WecomMedia {
|
|
16
|
+
#private;
|
|
17
|
+
constructor(shutdown: AbortSignal);
|
|
18
|
+
remember(kind: Exclude<MediaKind, 'audio'>, attachment: Attachment, now: number): MediaSegment;
|
|
19
|
+
download(handle: string, limit?: number): Promise<Buffer>;
|
|
20
|
+
upload(segment: MediaSegment, transport: WecomTransport): Promise<{
|
|
21
|
+
body: SendMediaMsgBody;
|
|
22
|
+
message: ({
|
|
23
|
+
type: 'image';
|
|
24
|
+
data: {
|
|
25
|
+
file: string;
|
|
26
|
+
url?: string;
|
|
27
|
+
summary?: string;
|
|
28
|
+
};
|
|
29
|
+
} | {
|
|
30
|
+
type: 'audio';
|
|
31
|
+
data: {
|
|
32
|
+
file: string;
|
|
33
|
+
url?: string;
|
|
34
|
+
};
|
|
35
|
+
} | {
|
|
36
|
+
type: 'video';
|
|
37
|
+
data: {
|
|
38
|
+
file: string;
|
|
39
|
+
url?: string;
|
|
40
|
+
};
|
|
41
|
+
} | {
|
|
42
|
+
type: 'file';
|
|
43
|
+
data: {
|
|
44
|
+
file: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
};
|
|
47
|
+
})[];
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { decryptFile } from '@wecom/aibot-node-sdk';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { seg } from '../../core/message/index.js';
|
|
5
|
+
import { base64Media, localMedia, mediaName, remoteMedia } from '../media-source.js';
|
|
6
|
+
export const attachmentSchema = z.object({
|
|
7
|
+
url: z.string().min(1).max(8192),
|
|
8
|
+
aeskey: z.string().regex(/^[A-Za-z0-9+/]{43}=?$/),
|
|
9
|
+
filename: z.string().max(256).optional(),
|
|
10
|
+
});
|
|
11
|
+
const prefix = 'wecom-media:';
|
|
12
|
+
const lifetime = 5 * 60_000;
|
|
13
|
+
const timeout = 30_000;
|
|
14
|
+
const maxReferences = 2048;
|
|
15
|
+
const mib = 1024 * 1024;
|
|
16
|
+
const maxBytes = {
|
|
17
|
+
image: 10 * mib,
|
|
18
|
+
video: 10 * mib,
|
|
19
|
+
audio: 2 * mib,
|
|
20
|
+
file: 20 * mib,
|
|
21
|
+
};
|
|
22
|
+
function safeName(value, limit) {
|
|
23
|
+
let result = '';
|
|
24
|
+
let size = 0;
|
|
25
|
+
for (const character of mediaName(value)
|
|
26
|
+
?.replace(/[\r\n]/g, '')
|
|
27
|
+
.replaceAll('\0', '') ?? '') {
|
|
28
|
+
size += Buffer.byteLength(character);
|
|
29
|
+
if (size > limit)
|
|
30
|
+
break;
|
|
31
|
+
result += character;
|
|
32
|
+
}
|
|
33
|
+
return result || undefined;
|
|
34
|
+
}
|
|
35
|
+
function extension(kind, bytes) {
|
|
36
|
+
switch (kind) {
|
|
37
|
+
case 'image':
|
|
38
|
+
if (bytes.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex')))
|
|
39
|
+
return 'png';
|
|
40
|
+
if (bytes.subarray(0, 3).equals(Buffer.from('ffd8ff', 'hex')))
|
|
41
|
+
return 'jpg';
|
|
42
|
+
if (/^GIF8[79]a$/.test(bytes.toString('ascii', 0, 6)))
|
|
43
|
+
return 'gif';
|
|
44
|
+
throw new Error('企业微信图片仅支持 PNG、JPEG 或 GIF');
|
|
45
|
+
case 'audio':
|
|
46
|
+
if (/^#!AMR(?:-WB)?\n/.test(bytes.toString('ascii', 0, 9)))
|
|
47
|
+
return 'amr';
|
|
48
|
+
throw new Error('企业微信语音仅支持 AMR');
|
|
49
|
+
case 'video':
|
|
50
|
+
if (bytes.length >= 16 && bytes.toString('ascii', 4, 8) === 'ftyp')
|
|
51
|
+
return 'mp4';
|
|
52
|
+
throw new Error('企业微信视频仅支持 MP4');
|
|
53
|
+
case 'file':
|
|
54
|
+
return 'bin';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function fileName(kind, bytes, sourceName) {
|
|
58
|
+
const format = extension(kind, bytes);
|
|
59
|
+
if (kind === 'file')
|
|
60
|
+
return safeName(sourceName, 256) ?? `file.${format}`;
|
|
61
|
+
const stem = safeName(sourceName?.replace(/\.[^./\\]+$/, ''), 255 - format.length) ?? kind;
|
|
62
|
+
return `${stem}.${format}`;
|
|
63
|
+
}
|
|
64
|
+
export class WecomMedia {
|
|
65
|
+
#references = new Map();
|
|
66
|
+
#shutdown;
|
|
67
|
+
constructor(shutdown) {
|
|
68
|
+
this.#shutdown = shutdown;
|
|
69
|
+
shutdown.addEventListener('abort', () => this.#references.clear(), { once: true });
|
|
70
|
+
}
|
|
71
|
+
remember(kind, attachment, now) {
|
|
72
|
+
for (const [handle, reference] of this.#references) {
|
|
73
|
+
if (reference.expiresAt > now)
|
|
74
|
+
break;
|
|
75
|
+
this.#references.delete(handle);
|
|
76
|
+
}
|
|
77
|
+
const handle = `${prefix}${randomUUID()}`;
|
|
78
|
+
this.#references.set(handle, { attachment, expiresAt: now + lifetime });
|
|
79
|
+
const oldest = this.#references.keys().next();
|
|
80
|
+
if (this.#references.size > maxReferences && !oldest.done)
|
|
81
|
+
this.#references.delete(oldest.value);
|
|
82
|
+
return kind === 'file'
|
|
83
|
+
? seg.file(handle, { name: safeName(attachment.filename, 256) })
|
|
84
|
+
: seg[kind](handle);
|
|
85
|
+
}
|
|
86
|
+
async download(handle, limit = maxBytes.file) {
|
|
87
|
+
const reference = this.#references.get(handle);
|
|
88
|
+
if (!reference || reference.expiresAt <= Date.now()) {
|
|
89
|
+
this.#references.delete(handle);
|
|
90
|
+
throw new Error('企业微信媒体已过期或不属于此机器人');
|
|
91
|
+
}
|
|
92
|
+
const signal = AbortSignal.any([this.#shutdown, AbortSignal.timeout(timeout)]);
|
|
93
|
+
try {
|
|
94
|
+
const url = new URL(reference.attachment.url);
|
|
95
|
+
if (url.protocol !== 'https:' || url.port || url.hash)
|
|
96
|
+
throw new Error('媒体地址不受信任');
|
|
97
|
+
const encrypted = await remoteMedia(url, signal, limit + 32);
|
|
98
|
+
if (encrypted.length % 32 !== 0)
|
|
99
|
+
throw new Error('媒体密文长度错误');
|
|
100
|
+
const bytes = decryptFile(encrypted, reference.attachment.aeskey);
|
|
101
|
+
if (!bytes.length || bytes.length > limit)
|
|
102
|
+
throw new Error('媒体超过大小限制或内容为空');
|
|
103
|
+
return bytes;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
throw new Error(signal.aborted
|
|
107
|
+
? '企业微信媒体下载已取消或超时'
|
|
108
|
+
: '企业微信媒体下载或解密失败,请检查地址、有效期和大小');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async upload(segment, transport) {
|
|
112
|
+
const limit = maxBytes[segment.type];
|
|
113
|
+
const signal = AbortSignal.any([this.#shutdown, AbortSignal.timeout(timeout)]);
|
|
114
|
+
const source = segment.data.file;
|
|
115
|
+
let bytes;
|
|
116
|
+
let sourceName;
|
|
117
|
+
if (source.startsWith(prefix))
|
|
118
|
+
bytes = await this.download(source, limit);
|
|
119
|
+
else {
|
|
120
|
+
try {
|
|
121
|
+
if (source.startsWith('base64://'))
|
|
122
|
+
bytes = base64Media(source.slice(9), limit);
|
|
123
|
+
else {
|
|
124
|
+
const url = new URL(source);
|
|
125
|
+
sourceName = decodeURIComponent(url.pathname);
|
|
126
|
+
bytes =
|
|
127
|
+
url.protocol === 'file:'
|
|
128
|
+
? await localMedia(url, signal, limit)
|
|
129
|
+
: await remoteMedia(url, signal, limit);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
throw new Error(signal.aborted ? '企业微信媒体读取已取消或超时' : '无法读取企业微信待发送媒体,请检查来源及大小');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (bytes.length < 5 || bytes.length > limit)
|
|
137
|
+
throw new Error(`企业微信${segment.type}素材大小须在 5 字节至 ${limit / mib} MiB 之间`);
|
|
138
|
+
const filename = fileName(segment.type, bytes, segment.type === 'file' ? (segment.data.name ?? sourceName) : sourceName);
|
|
139
|
+
const kind = segment.type === 'audio' ? 'voice' : segment.type;
|
|
140
|
+
this.#shutdown.throwIfAborted();
|
|
141
|
+
const mediaId = await transport.upload(bytes, { type: kind, filename });
|
|
142
|
+
const body = { msgtype: kind, [kind]: { media_id: mediaId } };
|
|
143
|
+
const handle = source.startsWith(prefix) ? source : `wecom-sent:${randomUUID()}`;
|
|
144
|
+
const message = [
|
|
145
|
+
segment.type === 'file' ? seg.file(handle, { name: filename }) : seg[segment.type](handle),
|
|
146
|
+
];
|
|
147
|
+
return { body, message };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { SendMsgBody } from '@wecom/aibot-node-sdk';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { type Message } from '../../core/message/index.ts';
|
|
4
|
+
import { type MediaSegment, type WecomMedia } from './media.ts';
|
|
5
|
+
export declare const frameHeader: z.ZodObject<{
|
|
6
|
+
headers: z.ZodObject<{
|
|
7
|
+
req_id: z.ZodString;
|
|
8
|
+
}, z.core.$strip>;
|
|
9
|
+
}, z.core.$strip>;
|
|
10
|
+
export declare const inboundFields: z.ZodObject<{
|
|
11
|
+
msgid: z.ZodString;
|
|
12
|
+
aibotid: z.ZodString;
|
|
13
|
+
from: z.ZodObject<{
|
|
14
|
+
userid: z.ZodString;
|
|
15
|
+
corpid: z.ZodOptional<z.ZodString>;
|
|
16
|
+
}, z.core.$strip>;
|
|
17
|
+
create_time: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
compat_info: z.ZodOptional<z.ZodUnknown>;
|
|
19
|
+
}, z.core.$strip>;
|
|
20
|
+
export declare const groupChat: z.ZodObject<{
|
|
21
|
+
chattype: z.ZodLiteral<"group">;
|
|
22
|
+
chatid: z.ZodString;
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
export declare const messageFrame: z.ZodObject<{
|
|
25
|
+
headers: z.ZodObject<{
|
|
26
|
+
req_id: z.ZodString;
|
|
27
|
+
}, z.core.$strip>;
|
|
28
|
+
body: z.ZodIntersection<z.ZodIntersection<z.ZodObject<{
|
|
29
|
+
msgid: z.ZodString;
|
|
30
|
+
aibotid: z.ZodString;
|
|
31
|
+
from: z.ZodObject<{
|
|
32
|
+
userid: z.ZodString;
|
|
33
|
+
corpid: z.ZodOptional<z.ZodString>;
|
|
34
|
+
}, z.core.$strip>;
|
|
35
|
+
create_time: z.ZodOptional<z.ZodNumber>;
|
|
36
|
+
compat_info: z.ZodOptional<z.ZodUnknown>;
|
|
37
|
+
quote: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
38
|
+
msgtype: z.ZodLiteral<"text">;
|
|
39
|
+
text: z.ZodObject<{
|
|
40
|
+
content: z.ZodString;
|
|
41
|
+
}, z.core.$strip>;
|
|
42
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
43
|
+
msgtype: z.ZodLiteral<"image">;
|
|
44
|
+
image: z.ZodObject<{
|
|
45
|
+
url: z.ZodString;
|
|
46
|
+
aeskey: z.ZodString;
|
|
47
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
48
|
+
}, z.core.$strip>;
|
|
49
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
50
|
+
msgtype: z.ZodLiteral<"file">;
|
|
51
|
+
file: z.ZodObject<{
|
|
52
|
+
url: z.ZodString;
|
|
53
|
+
aeskey: z.ZodString;
|
|
54
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
55
|
+
}, z.core.$strip>;
|
|
56
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
57
|
+
msgtype: z.ZodLiteral<"video">;
|
|
58
|
+
video: z.ZodObject<{
|
|
59
|
+
url: z.ZodString;
|
|
60
|
+
aeskey: z.ZodString;
|
|
61
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
62
|
+
}, z.core.$strip>;
|
|
63
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
64
|
+
msgtype: z.ZodLiteral<"voice">;
|
|
65
|
+
voice: z.ZodObject<{
|
|
66
|
+
content: z.ZodString;
|
|
67
|
+
}, z.core.$strip>;
|
|
68
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
69
|
+
msgtype: z.ZodLiteral<"mixed">;
|
|
70
|
+
mixed: z.ZodObject<{
|
|
71
|
+
msg_item: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
72
|
+
msgtype: z.ZodLiteral<"text">;
|
|
73
|
+
text: z.ZodObject<{
|
|
74
|
+
content: z.ZodString;
|
|
75
|
+
}, z.core.$strip>;
|
|
76
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
77
|
+
msgtype: z.ZodLiteral<"image">;
|
|
78
|
+
image: z.ZodObject<{
|
|
79
|
+
url: z.ZodString;
|
|
80
|
+
aeskey: z.ZodString;
|
|
81
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
82
|
+
}, z.core.$strip>;
|
|
83
|
+
}, z.core.$strip>], "msgtype">>;
|
|
84
|
+
}, z.core.$strip>;
|
|
85
|
+
}, z.core.$strip>], "msgtype">>;
|
|
86
|
+
}, z.core.$strip>, z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
87
|
+
chattype: z.ZodLiteral<"single">;
|
|
88
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
89
|
+
chattype: z.ZodLiteral<"group">;
|
|
90
|
+
chatid: z.ZodString;
|
|
91
|
+
}, z.core.$strip>], "chattype">>, z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
92
|
+
msgtype: z.ZodLiteral<"text">;
|
|
93
|
+
text: z.ZodObject<{
|
|
94
|
+
content: z.ZodString;
|
|
95
|
+
}, z.core.$strip>;
|
|
96
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
97
|
+
msgtype: z.ZodLiteral<"image">;
|
|
98
|
+
image: z.ZodObject<{
|
|
99
|
+
url: z.ZodString;
|
|
100
|
+
aeskey: z.ZodString;
|
|
101
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
102
|
+
}, z.core.$strip>;
|
|
103
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
104
|
+
msgtype: z.ZodLiteral<"file">;
|
|
105
|
+
file: z.ZodObject<{
|
|
106
|
+
url: z.ZodString;
|
|
107
|
+
aeskey: z.ZodString;
|
|
108
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
109
|
+
}, z.core.$strip>;
|
|
110
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
111
|
+
msgtype: z.ZodLiteral<"video">;
|
|
112
|
+
video: z.ZodObject<{
|
|
113
|
+
url: z.ZodString;
|
|
114
|
+
aeskey: z.ZodString;
|
|
115
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
116
|
+
}, z.core.$strip>;
|
|
117
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
118
|
+
msgtype: z.ZodLiteral<"voice">;
|
|
119
|
+
voice: z.ZodObject<{
|
|
120
|
+
content: z.ZodString;
|
|
121
|
+
}, z.core.$strip>;
|
|
122
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
123
|
+
msgtype: z.ZodLiteral<"mixed">;
|
|
124
|
+
mixed: z.ZodObject<{
|
|
125
|
+
msg_item: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
126
|
+
msgtype: z.ZodLiteral<"text">;
|
|
127
|
+
text: z.ZodObject<{
|
|
128
|
+
content: z.ZodString;
|
|
129
|
+
}, z.core.$strip>;
|
|
130
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
131
|
+
msgtype: z.ZodLiteral<"image">;
|
|
132
|
+
image: z.ZodObject<{
|
|
133
|
+
url: z.ZodString;
|
|
134
|
+
aeskey: z.ZodString;
|
|
135
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
136
|
+
}, z.core.$strip>;
|
|
137
|
+
}, z.core.$strip>], "msgtype">>;
|
|
138
|
+
}, z.core.$strip>;
|
|
139
|
+
}, z.core.$strip>], "msgtype">>;
|
|
140
|
+
}, z.core.$strip>;
|
|
141
|
+
type Inbound = z.output<typeof messageFrame>['body'];
|
|
142
|
+
export declare function incoming(body: Inbound, media: WecomMedia, now: number, botName?: string): Message;
|
|
143
|
+
export declare const contentText: z.ZodString;
|
|
144
|
+
type OutgoingPart = {
|
|
145
|
+
body: SendMsgBody;
|
|
146
|
+
message: Message;
|
|
147
|
+
} | {
|
|
148
|
+
media: MediaSegment;
|
|
149
|
+
};
|
|
150
|
+
export declare function outgoing(message: Message): Generator<OutgoingPart>;
|
|
151
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isSegment, seg } from '../../core/message/index.js';
|
|
3
|
+
import { cardForSend, cardSchema } from './cards.js';
|
|
4
|
+
import { attachmentSchema } from './media.js';
|
|
5
|
+
const id = z.string().min(1).max(1024);
|
|
6
|
+
export const frameHeader = z.object({ headers: z.object({ req_id: z.string().min(1).max(256) }) });
|
|
7
|
+
export const inboundFields = z.object({
|
|
8
|
+
msgid: id,
|
|
9
|
+
aibotid: id,
|
|
10
|
+
from: z.object({ userid: id, corpid: id.optional() }),
|
|
11
|
+
create_time: z.number().int().positive().optional(),
|
|
12
|
+
compat_info: z.unknown().optional(),
|
|
13
|
+
});
|
|
14
|
+
export const groupChat = z.object({ chattype: z.literal('group'), chatid: id });
|
|
15
|
+
const privateChat = z.object({ chattype: z.literal('single') });
|
|
16
|
+
const text = z.object({ msgtype: z.literal('text'), text: z.object({ content: z.string() }) });
|
|
17
|
+
const image = z.object({ msgtype: z.literal('image'), image: attachmentSchema });
|
|
18
|
+
const contentSchema = z.discriminatedUnion('msgtype', [
|
|
19
|
+
text,
|
|
20
|
+
image,
|
|
21
|
+
z.object({ msgtype: z.literal('file'), file: attachmentSchema }),
|
|
22
|
+
z.object({ msgtype: z.literal('video'), video: attachmentSchema }),
|
|
23
|
+
z.object({ msgtype: z.literal('voice'), voice: z.object({ content: z.string() }) }),
|
|
24
|
+
z.object({
|
|
25
|
+
msgtype: z.literal('mixed'),
|
|
26
|
+
mixed: z.object({ msg_item: z.array(z.discriminatedUnion('msgtype', [text, image])) }),
|
|
27
|
+
}),
|
|
28
|
+
]);
|
|
29
|
+
export const messageFrame = frameHeader.extend({
|
|
30
|
+
body: inboundFields
|
|
31
|
+
.extend({ quote: contentSchema.optional() })
|
|
32
|
+
.and(z.discriminatedUnion('chattype', [privateChat, groupChat]))
|
|
33
|
+
.and(contentSchema),
|
|
34
|
+
});
|
|
35
|
+
function segments(content, media, now) {
|
|
36
|
+
switch (content.msgtype) {
|
|
37
|
+
case 'text':
|
|
38
|
+
return [seg.text(content.text.content)];
|
|
39
|
+
case 'voice':
|
|
40
|
+
return [seg.text(content.voice.content)];
|
|
41
|
+
case 'image':
|
|
42
|
+
return [media.remember('image', content.image, now)];
|
|
43
|
+
case 'file':
|
|
44
|
+
return [media.remember('file', content.file, now)];
|
|
45
|
+
case 'video':
|
|
46
|
+
return [media.remember('video', content.video, now)];
|
|
47
|
+
case 'mixed':
|
|
48
|
+
return content.mixed.msg_item.flatMap((item) => segments(item, media, now));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function incoming(body, media, now, botName) {
|
|
52
|
+
const message = segments(body, media, now);
|
|
53
|
+
if (body.chattype === 'group') {
|
|
54
|
+
const first = message[0];
|
|
55
|
+
if (botName && first && isSegment(first, 'text') && body.msgtype !== 'voice') {
|
|
56
|
+
const prefix = `@${botName}`;
|
|
57
|
+
const content = first.data.text.trimStart();
|
|
58
|
+
const after = content.slice(prefix.length);
|
|
59
|
+
if (content.startsWith(prefix) && (after === '' || /^\s/.test(after)))
|
|
60
|
+
message[0] = seg.text(after.trimStart());
|
|
61
|
+
}
|
|
62
|
+
message.unshift(seg.at(body.aibotid));
|
|
63
|
+
}
|
|
64
|
+
if (body.quote) {
|
|
65
|
+
message.unshift({
|
|
66
|
+
type: 'reply',
|
|
67
|
+
data: { id: `wecom-quote:${body.msgid}`, message: segments(body.quote, media, now) },
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return message;
|
|
71
|
+
}
|
|
72
|
+
export const contentText = z
|
|
73
|
+
.string()
|
|
74
|
+
.min(1)
|
|
75
|
+
.refine((value) => Buffer.byteLength(value) <= 20_480, '企业微信单条内容不能超过 20480 字节');
|
|
76
|
+
function textToMarkdown(text) {
|
|
77
|
+
return text
|
|
78
|
+
.replace(/[\\`*_{}[\]()#+\-.!|>~<]/g, '\\$&')
|
|
79
|
+
.replace(/\r\n?/g, '\n')
|
|
80
|
+
.replace(/(?<!\n)\n(?!\n)/g, ' \n');
|
|
81
|
+
}
|
|
82
|
+
function markdownPart(content, message) {
|
|
83
|
+
return { body: { msgtype: 'markdown', markdown: { content: contentText.parse(content) } }, message };
|
|
84
|
+
}
|
|
85
|
+
export function* outgoing(message) {
|
|
86
|
+
let content = '';
|
|
87
|
+
let parts = [];
|
|
88
|
+
for (const segment of message) {
|
|
89
|
+
if (isSegment(segment, 'text') || isSegment(segment, 'markdown')) {
|
|
90
|
+
content += segment.type === 'markdown' ? segment.data.content : textToMarkdown(segment.data.text);
|
|
91
|
+
parts.push(segment);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (content)
|
|
95
|
+
yield markdownPart(content, parts);
|
|
96
|
+
content = '';
|
|
97
|
+
parts = [];
|
|
98
|
+
if (isSegment(segment, 'image') ||
|
|
99
|
+
isSegment(segment, 'file') ||
|
|
100
|
+
isSegment(segment, 'video') ||
|
|
101
|
+
isSegment(segment, 'audio')) {
|
|
102
|
+
yield { media: segment };
|
|
103
|
+
}
|
|
104
|
+
else if (segment.type === 'wecom:card') {
|
|
105
|
+
const card = cardForSend(cardSchema.parse(segment.data.card));
|
|
106
|
+
yield {
|
|
107
|
+
body: { msgtype: 'template_card', template_card: card },
|
|
108
|
+
message: [{ type: 'wecom:card', data: { card } }],
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
else
|
|
112
|
+
throw new Error(`未知的企业微信消息段:${segment.type}`);
|
|
113
|
+
}
|
|
114
|
+
if (content)
|
|
115
|
+
yield markdownPart(content, parts);
|
|
116
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type SendMsgBody, type StreamReplyBody, type UpdateTemplateCardBody, type UploadMediaOptions, type WelcomeReplyBody, WsCmd } from '@wecom/aibot-node-sdk';
|
|
2
|
+
import type { MessageTarget } from '../../core/bot.ts';
|
|
3
|
+
import type { WecomConfig } from './config.ts';
|
|
4
|
+
export type ReplyBody = SendMsgBody | StreamReplyBody | WelcomeReplyBody | UpdateTemplateCardBody;
|
|
5
|
+
type ReplyCommand = typeof WsCmd.RESPONSE | typeof WsCmd.RESPONSE_WELCOME | typeof WsCmd.RESPONSE_UPDATE;
|
|
6
|
+
export interface WecomHandlers {
|
|
7
|
+
connecting(): void;
|
|
8
|
+
ready(): void;
|
|
9
|
+
disconnected(): void;
|
|
10
|
+
retrying(delay: number): void;
|
|
11
|
+
failed(reason: string): void;
|
|
12
|
+
message(frame: unknown): void;
|
|
13
|
+
event(frame: unknown): void;
|
|
14
|
+
}
|
|
15
|
+
export interface WecomTransport {
|
|
16
|
+
start(handlers: WecomHandlers): void;
|
|
17
|
+
close(): void;
|
|
18
|
+
send(target: MessageTarget, body: SendMsgBody): Promise<string | undefined>;
|
|
19
|
+
reply(reqId: string, body: ReplyBody, command?: ReplyCommand): Promise<string | undefined>;
|
|
20
|
+
upload(bytes: Buffer, options: UploadMediaOptions): Promise<string>;
|
|
21
|
+
}
|
|
22
|
+
export declare function classifyWecomSend(error: unknown): {
|
|
23
|
+
code: "CONVERSATION_UNAVAILABLE" | "DELIVERY_BLOCKED" | "DELIVERY_PREPARATION_FAILED" | "DELIVERY_REJECTED" | "DELIVERY_TIMEOUT" | "DELIVERY_UNCONFIRMED" | "DELIVERY_UNSUPPORTED" | "NO_BOT_ONLINE" | "RATE_LIMITED";
|
|
24
|
+
platformCode: number | undefined;
|
|
25
|
+
} | undefined;
|
|
26
|
+
export declare class SDKTransport implements WecomTransport {
|
|
27
|
+
#private;
|
|
28
|
+
constructor(config: WecomConfig, { wsUrl, subscribeTimeout, reconnectInterval, }?: {
|
|
29
|
+
wsUrl?: string;
|
|
30
|
+
subscribeTimeout?: number;
|
|
31
|
+
reconnectInterval?: number;
|
|
32
|
+
});
|
|
33
|
+
start(handlers: WecomHandlers): void;
|
|
34
|
+
close(): void;
|
|
35
|
+
send(target: MessageTarget, body: SendMsgBody): Promise<string | undefined>;
|
|
36
|
+
reply(reqId: string, body: ReplyBody, command?: ReplyCommand): Promise<string | undefined>;
|
|
37
|
+
upload(bytes: Buffer, options: UploadMediaOptions): Promise<string>;
|
|
38
|
+
}
|
|
39
|
+
export {};
|