@zhin.js/adapter-wechat-mp 5.0.12 → 5.0.13
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 +21 -0
- package/adapters/wechat-mp.js +2 -1
- package/adapters/wechat-mp.ts +2 -1
- package/lib/endpoint.d.ts +2 -1
- package/lib/endpoint.js +4 -0
- package/lib/side-event-dispatch.d.ts +4 -0
- package/lib/side-event-dispatch.js +38 -0
- package/package.json +11 -11
- package/src/endpoint.ts +11 -1
- package/src/side-event-dispatch.ts +45 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @zhin.js/adapter-wechat-mp
|
|
2
2
|
|
|
3
|
+
## 5.0.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 5969c5b: Add SideEventGateway so adapters forward notice/request/system into HandlerIndex. HandlerContext now exposes only generation-safe capabilities and prompt ports; live Endpoint escape hatches are removed.
|
|
8
|
+
- Updated dependencies [5969c5b]
|
|
9
|
+
- Updated dependencies [d336a3f]
|
|
10
|
+
- Updated dependencies [5969c5b]
|
|
11
|
+
- Updated dependencies [5969c5b]
|
|
12
|
+
- Updated dependencies [974772e]
|
|
13
|
+
- Updated dependencies [5969c5b]
|
|
14
|
+
- Updated dependencies [2f786bd]
|
|
15
|
+
- Updated dependencies [1312ca0]
|
|
16
|
+
- Updated dependencies [985fa22]
|
|
17
|
+
- @zhin.js/im-contract@1.0.4
|
|
18
|
+
- @zhin.js/core@1.5.12
|
|
19
|
+
- @zhin.js/adapter@1.1.11
|
|
20
|
+
- @zhin.js/host-http@1.0.11
|
|
21
|
+
- @zhin.js/command@1.0.15
|
|
22
|
+
- zhin.js@6.0.12
|
|
23
|
+
|
|
3
24
|
## 5.0.12
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
package/adapters/wechat-mp.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Convention entry: discover `adapters/wechat-mp.ts` → defineAdapter.
|
|
4
4
|
*/
|
|
5
5
|
import { defineAdapter } from 'zhin.js/adapter';
|
|
6
|
-
import { messageGatewayToken } from '@zhin.js/core/runtime';
|
|
6
|
+
import { messageGatewayToken, sideEventGatewayToken } from '@zhin.js/core/runtime';
|
|
7
7
|
import { httpHostToken } from '@zhin.js/host-http';
|
|
8
8
|
import { WeChatMpEndpoint } from "../lib/endpoint.js";
|
|
9
9
|
import { resolveWeChatMpConfig, } from "../lib/protocol.js";
|
|
@@ -27,6 +27,7 @@ export default defineAdapter({
|
|
|
27
27
|
return new WeChatMpEndpoint({
|
|
28
28
|
id: context.id,
|
|
29
29
|
gateway: context.use(messageGatewayToken),
|
|
30
|
+
sideEvents: context.use(sideEventGatewayToken),
|
|
30
31
|
http: context.use(httpHostToken),
|
|
31
32
|
config,
|
|
32
33
|
});
|
package/adapters/wechat-mp.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Convention entry: discover `adapters/wechat-mp.ts` → defineAdapter.
|
|
3
3
|
*/
|
|
4
4
|
import { defineAdapter } from 'zhin.js/adapter';
|
|
5
|
-
import { messageGatewayToken } from '@zhin.js/core/runtime';
|
|
5
|
+
import { messageGatewayToken, sideEventGatewayToken } from '@zhin.js/core/runtime';
|
|
6
6
|
import { httpHostToken } from '@zhin.js/host-http';
|
|
7
7
|
import { WeChatMpEndpoint } from '../src/endpoint.js';
|
|
8
8
|
import {
|
|
@@ -32,6 +32,7 @@ export default defineAdapter<WeChatMpAdapterConfig>({
|
|
|
32
32
|
return new WeChatMpEndpoint({
|
|
33
33
|
id: context.id,
|
|
34
34
|
gateway: context.use(messageGatewayToken),
|
|
35
|
+
sideEvents: context.use(sideEventGatewayToken),
|
|
35
36
|
http: context.use(httpHostToken),
|
|
36
37
|
config,
|
|
37
38
|
});
|
package/lib/endpoint.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EndpointFriend, EndpointInstance, EndpointManagement, EndpointSendRequest } from 'zhin.js/adapter';
|
|
2
|
-
import type { MessageGateway } from '@zhin.js/core/runtime';
|
|
2
|
+
import type { MessageGateway, SideEventGateway } from '@zhin.js/core/runtime';
|
|
3
3
|
import type { HttpHost } from '@zhin.js/host-http';
|
|
4
4
|
import type { CapabilityId } from 'zhin.js';
|
|
5
5
|
import { type ResolvedWeChatMpConfig, type WeChatMessage } from './protocol.js';
|
|
@@ -13,6 +13,7 @@ export type WeChatMpFetch = (url: string, init?: {
|
|
|
13
13
|
export interface WeChatMpEndpointOptions {
|
|
14
14
|
readonly id: CapabilityId;
|
|
15
15
|
readonly gateway: MessageGateway;
|
|
16
|
+
readonly sideEvents?: SideEventGateway;
|
|
16
17
|
readonly http: HttpHost;
|
|
17
18
|
readonly config: ResolvedWeChatMpConfig;
|
|
18
19
|
readonly fetch?: WeChatMpFetch;
|
package/lib/endpoint.js
CHANGED
|
@@ -7,6 +7,7 @@ import { extractOutboundText, formatCustomerServiceBody, formatInboundContent, f
|
|
|
7
7
|
import { buildMediaUploadForm, readOutboundMedia, resolveMediaBinary, } from './media-upload.js';
|
|
8
8
|
import { getPassiveReplyCapture, recordPassiveReplyText, } from './passive-reply.js';
|
|
9
9
|
import { registerWeChatMpWebhookRoutes } from './webhook.js';
|
|
10
|
+
import { receiveWeChatMpSideEvent } from './side-event-dispatch.js';
|
|
10
11
|
/** token 失效类错误码:40001/40014 invalid access_token、42001 access_token expired。 */
|
|
11
12
|
const TOKEN_INVALID_ERRCODES = new Set([40001, 40014, 42001]);
|
|
12
13
|
/**
|
|
@@ -132,6 +133,9 @@ export class WeChatMpEndpoint {
|
|
|
132
133
|
admit(msg) {
|
|
133
134
|
if (!this.#open)
|
|
134
135
|
return;
|
|
136
|
+
if (receiveWeChatMpSideEvent(this.#options.sideEvents, this.#options.config.id, msg, this.#logger)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
135
139
|
const conversation = wechatMpInboundConversation(String(this.#options.id), msg);
|
|
136
140
|
void this.#options.gateway.receive({
|
|
137
141
|
conversation,
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SideEventGateway } from '@zhin.js/core/runtime';
|
|
2
|
+
import { type getAdapterLogger } from '@zhin.js/logger';
|
|
3
|
+
import { type WeChatMessage } from './protocol.js';
|
|
4
|
+
export declare function receiveWeChatMpSideEvent(sideEvents: SideEventGateway | undefined, configId: string, msg: WeChatMessage, logger: ReturnType<typeof getAdapterLogger>): boolean;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { buildNotice, senderFromId } from '@zhin.js/core';
|
|
2
|
+
import { formatCompact } from '@zhin.js/logger';
|
|
3
|
+
import { formatInboundId } from './protocol.js';
|
|
4
|
+
function mapWeChatMpEventParts(eventName) {
|
|
5
|
+
switch (eventName) {
|
|
6
|
+
case 'subscribe':
|
|
7
|
+
return { scene_type: 'friend', sub_type: 'increase' };
|
|
8
|
+
case 'unsubscribe':
|
|
9
|
+
return { scene_type: 'friend', sub_type: 'decrease' };
|
|
10
|
+
default:
|
|
11
|
+
return { scene_type: 'wechat-mp', sub_type: eventName || 'unknown' };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function receiveWeChatMpSideEvent(sideEvents, configId, msg, logger) {
|
|
15
|
+
if (!sideEvents || msg.MsgType !== 'event')
|
|
16
|
+
return false;
|
|
17
|
+
const eventName = msg.Event ?? 'unknown';
|
|
18
|
+
const parts = mapWeChatMpEventParts(eventName);
|
|
19
|
+
void sideEvents.receiveNotice(buildNotice(msg, {
|
|
20
|
+
$id: `wechat-mp:${formatInboundId(msg)}`,
|
|
21
|
+
$adapter: 'wechat-mp',
|
|
22
|
+
$endpoint: configId,
|
|
23
|
+
$type: 'notice',
|
|
24
|
+
$scene_id: msg.FromUserName,
|
|
25
|
+
$scene_type: parts.scene_type,
|
|
26
|
+
$sub_type: parts.sub_type,
|
|
27
|
+
$actor: senderFromId(msg.FromUserName),
|
|
28
|
+
$timestamp: msg.CreateTime ?? Date.now(),
|
|
29
|
+
})).catch((err) => {
|
|
30
|
+
logger.warn(formatCompact({
|
|
31
|
+
op: 'wechat_mp_side_event_failed',
|
|
32
|
+
endpoint: configId,
|
|
33
|
+
event: eventName,
|
|
34
|
+
error: err instanceof Error ? err.message : String(err),
|
|
35
|
+
}));
|
|
36
|
+
});
|
|
37
|
+
return true;
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/adapter-wechat-mp",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Zhin.js WeChat Official Account adapter for Plugin Runtime (HTTP webhook)",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"axios": "^1.19.0",
|
|
10
10
|
"xml2js": "^0.6.2",
|
|
11
|
-
"@zhin.js/adapter": "1.1.
|
|
12
|
-
"@zhin.js/core": "1.5.
|
|
13
|
-
"@zhin.js/host-http": "1.0.
|
|
14
|
-
"@zhin.js/im-contract": "1.0.
|
|
11
|
+
"@zhin.js/adapter": "1.1.11",
|
|
12
|
+
"@zhin.js/core": "1.5.12",
|
|
13
|
+
"@zhin.js/host-http": "1.0.11",
|
|
14
|
+
"@zhin.js/im-contract": "1.0.4",
|
|
15
15
|
"@zhin.js/logger": "1.0.76"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@zhin.js/adapter": "1.1.
|
|
19
|
-
"@zhin.js/command": "1.0.
|
|
20
|
-
"@zhin.js/core": "1.5.
|
|
21
|
-
"@zhin.js/host-http": "1.0.
|
|
22
|
-
"zhin.js": "6.0.
|
|
18
|
+
"@zhin.js/adapter": "1.1.11",
|
|
19
|
+
"@zhin.js/command": "1.0.15",
|
|
20
|
+
"@zhin.js/core": "1.5.12",
|
|
21
|
+
"@zhin.js/host-http": "1.0.11",
|
|
22
|
+
"zhin.js": "6.0.12"
|
|
23
23
|
},
|
|
24
24
|
"peerDependenciesMeta": {
|
|
25
25
|
"@zhin.js/command": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"@types/xml2js": "^0.4.14",
|
|
35
35
|
"typescript": "^6.0.3",
|
|
36
36
|
"vitest": "^4.1.10",
|
|
37
|
-
"zhin.js": "6.0.
|
|
37
|
+
"zhin.js": "6.0.12"
|
|
38
38
|
},
|
|
39
39
|
"keywords": [
|
|
40
40
|
"zhin",
|
package/src/endpoint.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import axios from 'axios';
|
|
5
5
|
import type { EndpointFriend, EndpointInstance, EndpointManagement, EndpointSendRequest } from 'zhin.js/adapter';
|
|
6
|
-
import type { MessageGateway } from '@zhin.js/core/runtime';
|
|
6
|
+
import type { MessageGateway, SideEventGateway } from '@zhin.js/core/runtime';
|
|
7
7
|
import type { HttpHost, HttpRouteRegistration } from '@zhin.js/host-http';
|
|
8
8
|
import { formatCompact, getAdapterLogger } from '@zhin.js/logger';
|
|
9
9
|
import type { CapabilityId } from 'zhin.js';
|
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
recordPassiveReplyText,
|
|
30
30
|
} from './passive-reply.js';
|
|
31
31
|
import { registerWeChatMpWebhookRoutes } from './webhook.js';
|
|
32
|
+
import { receiveWeChatMpSideEvent } from './side-event-dispatch.js';
|
|
32
33
|
|
|
33
34
|
/** token 失效类错误码:40001/40014 invalid access_token、42001 access_token expired。 */
|
|
34
35
|
const TOKEN_INVALID_ERRCODES = new Set([40001, 40014, 42001]);
|
|
@@ -52,6 +53,7 @@ export type WeChatMpFetch = (
|
|
|
52
53
|
export interface WeChatMpEndpointOptions {
|
|
53
54
|
readonly id: CapabilityId;
|
|
54
55
|
readonly gateway: MessageGateway;
|
|
56
|
+
readonly sideEvents?: SideEventGateway;
|
|
55
57
|
readonly http: HttpHost;
|
|
56
58
|
readonly config: ResolvedWeChatMpConfig;
|
|
57
59
|
readonly fetch?: WeChatMpFetch;
|
|
@@ -184,6 +186,14 @@ export class WeChatMpEndpoint implements EndpointInstance {
|
|
|
184
186
|
/** Test / internal: admit a parsed message when open (non-webhook path). */
|
|
185
187
|
admit(msg: WeChatMessage): void {
|
|
186
188
|
if (!this.#open) return;
|
|
189
|
+
if (receiveWeChatMpSideEvent(
|
|
190
|
+
this.#options.sideEvents,
|
|
191
|
+
this.#options.config.id,
|
|
192
|
+
msg,
|
|
193
|
+
this.#logger,
|
|
194
|
+
)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
187
197
|
const conversation = wechatMpInboundConversation(String(this.#options.id), msg);
|
|
188
198
|
void this.#options.gateway.receive({
|
|
189
199
|
conversation,
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { buildNotice, senderFromId } from '@zhin.js/core';
|
|
2
|
+
import type { SideEventGateway } from '@zhin.js/core/runtime';
|
|
3
|
+
import { formatCompact, type getAdapterLogger } from '@zhin.js/logger';
|
|
4
|
+
import { formatInboundId, type WeChatMessage } from './protocol.js';
|
|
5
|
+
|
|
6
|
+
function mapWeChatMpEventParts(eventName: string): { scene_type: string; sub_type: string } {
|
|
7
|
+
switch (eventName) {
|
|
8
|
+
case 'subscribe':
|
|
9
|
+
return { scene_type: 'friend', sub_type: 'increase' };
|
|
10
|
+
case 'unsubscribe':
|
|
11
|
+
return { scene_type: 'friend', sub_type: 'decrease' };
|
|
12
|
+
default:
|
|
13
|
+
return { scene_type: 'wechat-mp', sub_type: eventName || 'unknown' };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function receiveWeChatMpSideEvent(
|
|
18
|
+
sideEvents: SideEventGateway | undefined,
|
|
19
|
+
configId: string,
|
|
20
|
+
msg: WeChatMessage,
|
|
21
|
+
logger: ReturnType<typeof getAdapterLogger>,
|
|
22
|
+
): boolean {
|
|
23
|
+
if (!sideEvents || msg.MsgType !== 'event') return false;
|
|
24
|
+
const eventName = msg.Event ?? 'unknown';
|
|
25
|
+
const parts = mapWeChatMpEventParts(eventName);
|
|
26
|
+
void sideEvents.receiveNotice(buildNotice(msg, {
|
|
27
|
+
$id: `wechat-mp:${formatInboundId(msg)}`,
|
|
28
|
+
$adapter: 'wechat-mp' as never,
|
|
29
|
+
$endpoint: configId,
|
|
30
|
+
$type: 'notice',
|
|
31
|
+
$scene_id: msg.FromUserName,
|
|
32
|
+
$scene_type: parts.scene_type,
|
|
33
|
+
$sub_type: parts.sub_type,
|
|
34
|
+
$actor: senderFromId(msg.FromUserName),
|
|
35
|
+
$timestamp: msg.CreateTime ?? Date.now(),
|
|
36
|
+
})).catch((err) => {
|
|
37
|
+
logger.warn(formatCompact({
|
|
38
|
+
op: 'wechat_mp_side_event_failed',
|
|
39
|
+
endpoint: configId,
|
|
40
|
+
event: eventName,
|
|
41
|
+
error: err instanceof Error ? err.message : String(err),
|
|
42
|
+
}));
|
|
43
|
+
});
|
|
44
|
+
return true;
|
|
45
|
+
}
|