feat: add stable proactive delivery

This commit is contained in:
xmanrui 2026-08-30 11:24:53 +08:00
parent 410cb822d7
commit 31792d7bc8
88 changed files with 6427 additions and 488 deletions

View file

@ -299,6 +299,20 @@ export class WeixinController {
});
}
async sendProactiveText(botId, target, text, options = {}) {
const config = this.#configStore.get(botId);
if (!config) throw new Error('Unknown Weixin account');
return this.#withBotTransition(botId, async () => {
const runtime = this.#runtimes.get(botId);
if (!runtime?.status?.ready || typeof runtime.sendProactiveText !== 'function') {
const error = new Error(t('微信连接当前离线'));
error.code = 'bot-not-connected';
throw error;
}
return runtime.sendProactiveText(target, text, options);
});
}
async deleteBot(botId) {
const config = this.#configStore.get(botId);
if (!config) throw new Error('Unknown Weixin account');

View file

@ -329,4 +329,28 @@ export class WeixinRuntime {
});
return { sent: true };
}
async sendProactiveText(target, text, { signal } = {}) {
const toUserId = typeof target?.route?.toUserId === 'string'
? target.route.toUserId.trim() : '';
if (target?.kind !== 'user' || !toUserId) {
const error = new TypeError('Invalid Weixin proactive delivery target');
error.code = 'invalid-target';
throw error;
}
if (!this.#status.ready || !this.#abortController) {
const error = new Error('Weixin runtime is not connected');
error.code = 'bot-not-connected';
throw error;
}
signal?.throwIfAborted();
await this.#api.sendText({
baseUrl: this.#config.baseUrl,
token: this.#token,
toUserId,
text,
signal: signal ?? this.#abortController.signal,
});
return { sent: true };
}
}