feat: send test messages during bot connection checks

This commit is contained in:
xmanrui 2026-08-19 03:52:52 +08:00
parent 94da24538f
commit fb61c4235f
78 changed files with 3460 additions and 210 deletions

View file

@ -1,5 +1,6 @@
import { randomUUID } from 'node:crypto';
import { connectionTestMessage } from '../shared/connection-test.mjs';
import { deriveWhatsappBotId, maskWhatsappAccount } from './config-store.mjs';
const ACTIVE_ATTEMPT_STATES = new Set(['starting', 'pending', 'connecting']);
@ -198,6 +199,25 @@ export class WhatsappController {
return this.status();
}
async sendConnectionTest(botId) {
const config = this.#configStore.get(botId);
if (!config) throw new Error('Unknown WhatsApp bot');
return this.#withBotTransition(botId, async () => {
const runtime = this.#runtimes.get(botId);
if (!runtime?.status?.ready || typeof runtime.sendConnectionTest !== 'function') {
const error = new Error('WhatsApp机器人尚未连接');
error.code = 'test-target-unavailable';
throw error;
}
return runtime.sendConnectionTest(
connectionTestMessage(
`${config.name}(${maskWhatsappAccount(config.accountJid)})`,
'WhatsApp机器人',
),
);
});
}
async deleteBot(botId) {
const config = this.#configStore.get(botId);
if (!config) throw new Error('Unknown WhatsApp bot');

View file

@ -421,6 +421,22 @@ export class WhatsappRuntime {
return this.stop();
}
async sendConnectionTest(text) {
if (!this.#status.ready || !this.#client) {
const error = new Error('WhatsApp机器人尚未连接');
error.code = 'test-target-unavailable';
throw error;
}
if (typeof text !== 'string' || !text.trim()) {
throw new TypeError('WhatsApp connection test text is required');
}
await this.#client.sendText({
jid: this.#config.accountJid,
selfChat: true,
}, text);
return { sent: true };
}
async stop() {
const session = this.#session;
const client = this.#client;