Spaces:
Running
Running
axeronis commited on
Commit ·
f233de2
1
Parent(s): 39869c1
fix(backend): force instance cleanup on QR refresh to prevent stale codes
Browse files
backend/app/api/channels.py
CHANGED
|
@@ -379,8 +379,8 @@ async def refresh_whatsapp_qr(
|
|
| 379 |
"message": "Already connected"
|
| 380 |
}
|
| 381 |
|
| 382 |
-
# Generate a fresh QR code
|
| 383 |
-
qr_url = await whatsapp_qr_service.generate_qr(tenant_id)
|
| 384 |
|
| 385 |
return {
|
| 386 |
"ok": True,
|
|
|
|
| 379 |
"message": "Already connected"
|
| 380 |
}
|
| 381 |
|
| 382 |
+
# Generate a fresh QR code (force refresh)
|
| 383 |
+
qr_url = await whatsapp_qr_service.generate_qr(tenant_id, force_refresh=True)
|
| 384 |
|
| 385 |
return {
|
| 386 |
"ok": True,
|
backend/app/services/whatsapp_qr.py
CHANGED
|
@@ -50,12 +50,13 @@ class WhatsAppQRSession:
|
|
| 50 |
'Content-Type': 'application/json'
|
| 51 |
}
|
| 52 |
|
| 53 |
-
async def generate_qr(self, tenant_id: str) -> str:
|
| 54 |
"""
|
| 55 |
Генерация QR-кода для подключения WhatsApp через Evolution API
|
| 56 |
|
| 57 |
Args:
|
| 58 |
tenant_id: ID тенанта
|
|
|
|
| 59 |
|
| 60 |
Returns:
|
| 61 |
Base64 QR code image
|
|
@@ -70,6 +71,13 @@ class WhatsAppQRSession:
|
|
| 70 |
try:
|
| 71 |
instance_name = f"tenant_{tenant_id}"
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 74 |
# 1. Create instance in Baileys API
|
| 75 |
create_url = f"{self.evolution_url}/instance/create"
|
|
|
|
| 50 |
'Content-Type': 'application/json'
|
| 51 |
}
|
| 52 |
|
| 53 |
+
async def generate_qr(self, tenant_id: str, force_refresh: bool = False) -> str:
|
| 54 |
"""
|
| 55 |
Генерация QR-кода для подключения WhatsApp через Evolution API
|
| 56 |
|
| 57 |
Args:
|
| 58 |
tenant_id: ID тенанта
|
| 59 |
+
force_refresh: Если True, удаляет старую сессию перед генерацией
|
| 60 |
|
| 61 |
Returns:
|
| 62 |
Base64 QR code image
|
|
|
|
| 71 |
try:
|
| 72 |
instance_name = f"tenant_{tenant_id}"
|
| 73 |
|
| 74 |
+
# If force refresh requested, delete old instance first
|
| 75 |
+
if force_refresh:
|
| 76 |
+
logger.info(f"Force refreshing instance {instance_name}")
|
| 77 |
+
await self.disconnect(tenant_id)
|
| 78 |
+
# Give it a moment to cleanup
|
| 79 |
+
await asyncio.sleep(2)
|
| 80 |
+
|
| 81 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 82 |
# 1. Create instance in Baileys API
|
| 83 |
create_url = f"{self.evolution_url}/instance/create"
|