import type { EndpointProfileConfiguration } from 'zhin.js/adapter'; import { createMessages, type Locale } from 'zhin.js/i18n'; /** * WeChat Official Account (MP) protocol helpers — no legacy Adapter/Endpoint. * Canonicalization is owned by gateway/core before endpoint.send. */ import { createHash, createDecipheriv, createCipheriv, randomBytes, timingSafeEqual } from 'node:crypto'; import * as xml2js from 'xml2js'; import type { ConversationRef } from '@zhin.js/im-contract'; /** One endpoint config after AdapterIndex expands `plugins..endpoints`. */ export interface WeChatMpEndpointConfig extends EndpointProfileConfiguration { readonly id: string; readonly appId: string; readonly appSecret: string; readonly token: string; readonly encodingAESKey?: string; readonly path?: string; readonly encrypt?: boolean; /** * plain:明文入站/出站 * compatible:入站可解密,被动回复用明文(微信兼容模式推荐) * secure:入站/出站均加密 */ readonly encryptMode?: 'plain' | 'compatible' | 'secure'; /** * passive:订阅号默认,在 webhook 响应内被动回复(5 秒内) * customer_service:走客服消息 API(需接口权限) */ readonly replyMode?: 'passive' | 'customer_service'; /** 被动回复等待入站处理的最长时间(毫秒),默认 4500 */ readonly passiveReplyTimeoutMs?: number; } export interface ResolvedWeChatMpConfig { readonly context: 'wechat-mp'; readonly id: string; readonly appId: string; readonly appSecret: string; readonly token: string; readonly encodingAESKey?: string; readonly path: string; readonly encrypt: boolean; readonly encryptMode: 'plain' | 'compatible' | 'secure'; readonly replyMode: 'passive' | 'customer_service'; readonly passiveReplyTimeoutMs: number; } export interface WeChatMessage { readonly ToUserName: string; readonly FromUserName: string; readonly CreateTime: number; readonly MsgType: string; readonly MsgId?: string; readonly Content?: string; readonly PicUrl?: string; readonly MediaId?: string; readonly Format?: string; readonly Recognition?: string; readonly ThumbMediaId?: string; readonly Location_X?: string; readonly Location_Y?: string; readonly Scale?: string; readonly Label?: string; readonly Title?: string; readonly Description?: string; readonly Url?: string; readonly Event?: string; readonly EventKey?: string; readonly Encrypt?: string; } export interface TokenResponse { readonly access_token: string; readonly expires_in: number; } export interface WeChatAPIResponse { readonly errcode?: number; readonly errmsg?: string; readonly msgid?: number; } export interface WeChatWireSegment { readonly type: string; readonly data?: Record; } export function resolveWeChatMpConfig(config: WeChatMpEndpointConfig): ResolvedWeChatMpConfig { const id = requiredEndpointField(config.id, 'id'); const appId = requiredEndpointField(config.appId, 'appId'); const appSecret = requiredEndpointField(config.appSecret, 'appSecret'); const token = requiredEndpointField(config.token, 'token'); const path = optionalEndpointField(config.path) ?? '/wechat/webhook'; const encodingAESKey = optionalEndpointField(config.encodingAESKey); const encrypt = config.encrypt ?? false; const encryptMode = config.encryptMode ?? (encrypt ? 'compatible' : 'plain'); const replyMode = config.replyMode ?? 'passive'; const passiveReplyTimeoutMs = config.passiveReplyTimeoutMs ?? 4500; return { context: 'wechat-mp', id, appId, appSecret, token, encodingAESKey, path: path.startsWith('/') ? path : `/${path}`, encrypt: !!encrypt, encryptMode, replyMode, passiveReplyTimeoutMs, }; } function requiredEndpointField( value: unknown, field: 'id' | 'appId' | 'appSecret' | 'token', ): string { const resolved = optionalEndpointField(value); if (!resolved) { throw new TypeError(`WeChat MP endpoint requires a non-empty ${field}`); } return resolved; } function optionalEndpointField(value: unknown): string | undefined { return typeof value === 'string' && value.trim() ? value.trim() : undefined; } export function queryParam(value: string | null | undefined): string { return value ?? ''; } /** URL 查询里的 Base64 可能把 `+` 解码成空格 */ export function normalizeEchostrParam(echostr: string): string { return echostr.replace(/ /g, '+'); } export function computeSignatureHash( token: string, params: { readonly timestamp: string; readonly nonce: string; readonly echostr?: string }, ): string { const { timestamp, nonce, echostr } = params; const arr = echostr ? [token, timestamp, nonce, echostr] : [token, timestamp, nonce]; arr.sort(); return createHash('sha1').update(arr.join('')).digest('hex'); } export function verifySignature( token: string, params: { readonly signature: string; readonly timestamp: string; readonly nonce: string; readonly echostr?: string; }, ): boolean { const { signature, timestamp, nonce, echostr } = params; if (!signature || !timestamp || !nonce) return false; return safeEqualHex(computeSignatureHash(token, { timestamp, nonce, echostr }), signature); } /** Constant-time comparison for hex signatures (same approach as the other webhook adapters). */ function safeEqualHex(expected: string, actual: string): boolean { const a = Buffer.from(expected); const b = Buffer.from(actual); return a.length === b.length && timingSafeEqual(a, b); } export function getAESKey(encodingAESKey: string): Buffer { return Buffer.from(`${encodingAESKey}=`, 'base64'); } /** 微信安全模式加密 echostr 为较长 Base64;明文/兼容模式多为短字符串 */ export function isEncryptedEchostr(echostr: string): boolean { if (echostr.length < 32) return false; return /^[A-Za-z0-9+/]+={0,2}$/.test(echostr); } export function decryptEchostr( encrypted: string, encodingAESKey: string, appId: string, ): string { const aesKey = getAESKey(encodingAESKey); const iv = aesKey.subarray(0, 16); const decipher = createDecipheriv('aes-256-cbc', aesKey, iv); decipher.setAutoPadding(false); const decrypted = Buffer.concat([ decipher.update(Buffer.from(encrypted, 'base64')), decipher.final(), ]); const pad = decrypted[decrypted.length - 1]!; const content = decrypted.subarray(0, decrypted.length - pad); const msgLen = content.readUInt32BE(16); const plain = content.subarray(20, 20 + msgLen).toString('utf8'); const gotAppId = content.subarray(20 + msgLen).toString('utf8'); if (gotAppId !== appId) { throw new Error(`AppID mismatch: expected ${appId}, got ${gotAppId}`); } return plain; } export async function parseXMLMessage(xmlString: string): Promise { try { const parser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true }); const result = await parser.parseStringPromise(xmlString); return result.xml as WeChatMessage; } catch { return null; } } export async function decryptMessage( encryptedXml: string, msgSignature: string, timestamp: string, nonce: string, token: string, encodingAESKey: string, appId: string, ): Promise { const parsed = await parseXMLMessage(encryptedXml); const encrypt = parsed?.Encrypt; if (!encrypt) throw new Error('Missing Encrypt field in encrypted message'); const expected = createHash('sha1') .update([token, timestamp, nonce, encrypt].sort().join('')) .digest('hex'); if (!safeEqualHex(expected, msgSignature)) { throw new Error('msg_signature verification failed'); } const aesKey = getAESKey(encodingAESKey); const iv = aesKey.subarray(0, 16); const decipher = createDecipheriv('aes-256-cbc', aesKey, iv); decipher.setAutoPadding(false); const decrypted = Buffer.concat([ decipher.update(Buffer.from(encrypt, 'base64')), decipher.final(), ]); const pad = decrypted[decrypted.length - 1]!; const content = decrypted.subarray(0, decrypted.length - pad); const msgLen = content.readUInt32BE(16); const xmlContent = content.subarray(20, 20 + msgLen).toString('utf8'); const gotAppId = content.subarray(20 + msgLen).toString('utf8'); if (gotAppId !== appId) { throw new Error(`AppID mismatch: expected ${appId}, got ${gotAppId}`); } return xmlContent; } export function encryptMessage( replyXml: string, token: string, encodingAESKey: string, appId: string, requestTimestamp?: string, ): string { const aesKey = getAESKey(encodingAESKey); const iv = aesKey.subarray(0, 16); const random = randomBytes(16); const msgBuf = Buffer.from(replyXml, 'utf8'); const appIdBuf = Buffer.from(appId, 'utf8'); const lenBuf = Buffer.alloc(4); lenBuf.writeUInt32BE(msgBuf.length, 0); const plaintext = Buffer.concat([random, lenBuf, msgBuf, appIdBuf]); const blockSize = 32; const padLen = blockSize - (plaintext.length % blockSize); const padBuf = Buffer.alloc(padLen, padLen); const padded = Buffer.concat([plaintext, padBuf]); const cipher = createCipheriv('aes-256-cbc', aesKey, iv); cipher.setAutoPadding(false); const encrypted = Buffer.concat([cipher.update(padded), cipher.final()]); const encryptStr = encrypted.toString('base64'); const timestamp = requestTimestamp || Math.floor(Date.now() / 1000).toString(); const nonce = randomBytes(8).toString('hex'); const signature = createHash('sha1') .update([token, timestamp, nonce, encryptStr].sort().join('')) .digest('hex'); return [ '', ``, ``, `${timestamp}`, ``, '', ].join('\n'); } export function buildTextReply( wechatMsg: Pick, content: string, ): string { const cdata = (value: string) => value.replace(/]]>/g, ']]]]>'); const createTime = Math.floor(Date.now() / 1000); return [ '', ``, ``, `${createTime}`, ``, ``, '', ].join(''); } /** * 入站归一化:公众号只有粉丝单聊场景,一律 kind='private',id=openid(FromUserName), * 无群/频道容器(parent 恒缺省)。 */ export function wechatMpInboundConversation( endpointKey: string, msg: WeChatMessage, ): ConversationRef { return { endpoint: { id: endpointKey, adapter: endpointKey.split('\0')[0] ?? endpointKey }, kind: 'private', id: msg.FromUserName, }; } /** * 入站消息 id:普通消息用 MsgId;无 MsgId 的事件消息拼 Event/EventKey, * 避免只用秒级 CreateTime 时同秒多事件 id 碰撞。 */ export function formatInboundId(msg: WeChatMessage): string { if (msg.MsgId) return msg.MsgId; return [msg.CreateTime, msg.Event, msg.EventKey] .filter((part) => part != null && part !== '') .join(':'); } /** Build inbound text for OutboundMessageService.receive. */ export function formatInboundContent(msg: WeChatMessage): string { switch (msg.MsgType) { case 'text': return msg.Content || '(空消息)'; case 'image': return msg.PicUrl ? `[image: ${msg.PicUrl}]` : '[image]'; case 'voice': return msg.Recognition ? msg.Recognition : `[voice${msg.Format ? `: ${msg.Format}` : ''}]`; case 'video': case 'shortvideo': return '[video]'; case 'location': return `[location: ${msg.Location_X},${msg.Location_Y}${msg.Label ? ` ${msg.Label}` : ''}]`; case 'link': return `[link: ${msg.Title ?? ''}${msg.Url ? ` ${msg.Url}` : ''}]`; case 'event': return `[event: ${msg.Event ?? ''}${msg.EventKey ? ` ${msg.EventKey}` : ''}]`; default: return `[不支持的消息类型: ${msg.MsgType}]`; } } /** * Built-in passive XML for subscribe etc. Empty string = fall through to gateway. */ const passiveMessages = createMessages({ subscribed: { 'zh-CN': '感谢关注!', en: 'Thank you for following!' }, }); export function resolveEventPassiveReply(msg: WeChatMessage, locale?: Locale): string { if (msg.MsgType !== 'event') return ''; if (msg.Event === 'subscribe') { return buildTextReply(msg, passiveMessages(locale, 'subscribed')); } return ''; } /** * Wire-encode an already-rendered outbound payload into客服消息 JSON body. */ export function formatCustomerServiceBody( target: string, payload: unknown, ): Record { const messageData: Record = { touser: target, msgtype: 'text', text: { content: '' }, }; if (typeof payload === 'string') { (messageData.text as { content: string }).content = payload; return messageData; } const segments: Array = Array.isArray(payload) ? payload as Array : payload && typeof payload === 'object' && 'type' in (payload as object) ? [payload as WeChatWireSegment] : []; if (segments.length === 0) { (messageData.text as { content: string }).content = payload == null ? '' : Array.isArray(payload) ? '' : typeof payload === 'object' ? JSON.stringify(payload) : String(payload); return messageData; } const textParts: string[] = []; let hasMedia = false; for (const item of segments) { if (typeof item === 'string') { textParts.push(item); continue; } const data = item.data ?? {}; switch (item.type) { case 'text': textParts.push(String(data.text ?? data.content ?? '')); break; case 'image': if (!hasMedia && data.mediaId) { messageData.msgtype = 'image'; messageData.image = { media_id: data.mediaId }; delete messageData.text; hasMedia = true; } break; case 'voice': case 'audio': if (!hasMedia && data.mediaId) { messageData.msgtype = 'voice'; messageData.voice = { media_id: data.mediaId }; delete messageData.text; hasMedia = true; } break; case 'video': if (!hasMedia && data.mediaId) { messageData.msgtype = 'video'; messageData.video = { media_id: data.mediaId, title: data.title || '', description: data.description || '', }; delete messageData.text; hasMedia = true; } break; default: break; } } if (!hasMedia && textParts.length > 0 && messageData.text) { (messageData.text as { content: string }).content = textParts.join('\n'); } return messageData; } /** Extract plain text from a rendered outbound payload (for passive XML). */ export function extractOutboundText(payload: unknown): string { if (typeof payload === 'string') return payload; if (!Array.isArray(payload)) { if (payload && typeof payload === 'object' && 'type' in (payload as object)) { const seg = payload as WeChatWireSegment; if (seg.type === 'text') return String(seg.data?.text ?? seg.data?.content ?? ''); } return payload == null ? '' : String(payload); } const parts: string[] = []; for (const item of payload) { if (typeof item === 'string') { parts.push(item); continue; } if (item && typeof item === 'object' && 'type' in item) { const seg = item as WeChatWireSegment; if (seg.type === 'text') { parts.push(String(seg.data?.text ?? seg.data?.content ?? '')); } } } return parts.join('\n'); }