fix: stop repeated/duplicate bot responses
Root cause: WpWebhook called OutboundWorker::processQueue() after every incoming message. BotRouter already sends synchronously in enqueueResponse(); failed messages stayed status='queued'. On the next incoming message processQueue() would re-send ALL previously-failed messages, causing the bot to repeat old responses. - Remove OutboundWorker::processQueue() from WpWebhook::processEvent() BotRouter handles immediate delivery; retries should be manual/cron only - Fix chatSend(): instead of queuing + processQueue() (which drains ALL queued messages), call WhatsAppSender::sendText() directly for the one message being sent Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
cb348bd602
commit
e3be2d5b3f
@@ -2532,18 +2532,26 @@ HTML;
|
||||
|
||||
$db = db();
|
||||
|
||||
// Enqueue the message then process immediately so it's sent to WhatsApp right away
|
||||
$payload = json_encode(['text' => $text]);
|
||||
$db->prepare("INSERT INTO outbound_queue (company_id, to_number, message_type, payload, status, created_at) VALUES (?, ?, 'text', ?, 'queued', NOW())")
|
||||
->execute([$companyId, $phone, $payload]);
|
||||
// Look up the WhatsApp phone_number_id for this company
|
||||
$company = CompanyRepository::findById($companyId);
|
||||
$phoneNumberId = $company['phone_number_id'] ?? env('WHATSAPP_PHONE_NUMBER_ID', '');
|
||||
|
||||
// Add to conversations log
|
||||
$msgId = 'admin_' . time() . '_' . $phone;
|
||||
// Send directly — do NOT call processQueue() which would re-send ALL queued/failed messages
|
||||
$sendResult = WhatsAppSender::sendText($phone, $text, $phoneNumberId);
|
||||
|
||||
// Log to conversations regardless of WhatsApp delivery result
|
||||
$msgId = 'admin_' . time() . '_' . $phone;
|
||||
$status = $sendResult['success'] ? 'sent' : 'failed';
|
||||
$db->prepare("INSERT INTO conversations (company_id, message_id, phone_number, direction, message_type, content, created_at) VALUES (?, ?, ?, 'outbound', 'text', ?, NOW())")
|
||||
->execute([$companyId, $msgId, $phone, $text]);
|
||||
|
||||
// Send now — don't wait for the manual queue processor
|
||||
OutboundWorker::processQueue();
|
||||
// Also keep an outbound_queue record for auditing
|
||||
$db->prepare("INSERT INTO outbound_queue (company_id, to_number, message_type, payload, status, wam_id, created_at) VALUES (?, ?, 'text', ?, ?, ?, NOW())")
|
||||
->execute([$companyId, $phone, json_encode(['text' => $text]), $status, $sendResult['wam_id'] ?? null]);
|
||||
|
||||
if (!$sendResult['success']) {
|
||||
jsonResponse(500, ['error' => $sendResult['error'] ?? 'Error al enviar']);
|
||||
}
|
||||
|
||||
jsonResponse(200, ['status' => 'sent']);
|
||||
}
|
||||
|
||||
@@ -144,13 +144,9 @@ class WpWebhook
|
||||
|
||||
// Persistir evento crudo para auditoría
|
||||
self::saveRawEvent($raw);
|
||||
|
||||
// Procesar cola outbound inmediatamente para respuestas automáticas
|
||||
try {
|
||||
OutboundWorker::processQueue();
|
||||
} catch (\Throwable $e) {
|
||||
self::log('WARN', 'OutboundWorker falló: ' . $e->getMessage());
|
||||
}
|
||||
// Note: BotRouter already sends synchronously in enqueueResponse().
|
||||
// Running processQueue() here would re-send ALL previously-failed messages
|
||||
// on every incoming webhook, causing duplicate/repeated responses.
|
||||
}
|
||||
|
||||
private static function resolveCompany(array $payload): void
|
||||
|
||||
Reference in New Issue
Block a user