Spaces:
Running
Running
axeronis commited on
Commit ·
7368ae3
1
Parent(s): e07475a
feat: Add QR refresh endpoint for auto-refresh UI
Browse files- backend/app/api/channels.py +51 -0
backend/app/api/channels.py
CHANGED
|
@@ -358,6 +358,57 @@ async def generate_whatsapp_qr(
|
|
| 358 |
raise HTTPException(status_code=500, detail=f"QR generation error: {str(e)}")
|
| 359 |
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
@router.get("/whatsapp/qr/status")
|
| 362 |
async def get_whatsapp_qr_status(
|
| 363 |
current_user: User = Depends(get_current_user),
|
|
|
|
| 358 |
raise HTTPException(status_code=500, detail=f"QR generation error: {str(e)}")
|
| 359 |
|
| 360 |
|
| 361 |
+
@router.get("/whatsapp/qr/refresh")
|
| 362 |
+
async def refresh_whatsapp_qr(
|
| 363 |
+
current_user: User = Depends(get_current_user),
|
| 364 |
+
session = Depends(get_session)
|
| 365 |
+
):
|
| 366 |
+
"""
|
| 367 |
+
Refresh QR code for WhatsApp connection (for auto-refresh).
|
| 368 |
+
"""
|
| 369 |
+
try:
|
| 370 |
+
tenant_id = str(current_user.tenant_id)
|
| 371 |
+
instance_name = f"tenant_{tenant_id}"
|
| 372 |
+
|
| 373 |
+
# Get fresh QR from Baileys
|
| 374 |
+
async with httpx.AsyncClient(timeout=10.0) as client:
|
| 375 |
+
response = await client.get(
|
| 376 |
+
f"{settings.EVOLUTION_API_URL}/instance/qr/{instance_name}",
|
| 377 |
+
headers={'apikey': settings.EVOLUTION_API_KEY}
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
if response.status_code == 200:
|
| 381 |
+
data = response.json()
|
| 382 |
+
|
| 383 |
+
if data.get('status') == 'connected':
|
| 384 |
+
return {
|
| 385 |
+
"ok": True,
|
| 386 |
+
"connected": True,
|
| 387 |
+
"message": "Already connected"
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
qr_code = data.get('qrcode', {}).get('base64')
|
| 391 |
+
if qr_code:
|
| 392 |
+
return {
|
| 393 |
+
"ok": True,
|
| 394 |
+
"qr_code": qr_code,
|
| 395 |
+
"expires_in": data.get('expiresIn', 60)
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
# Waiting for new QR
|
| 399 |
+
return {
|
| 400 |
+
"ok": True,
|
| 401 |
+
"waiting": True,
|
| 402 |
+
"message": "Waiting for new QR code"
|
| 403 |
+
}
|
| 404 |
+
else:
|
| 405 |
+
raise HTTPException(status_code=500, detail="Failed to refresh QR")
|
| 406 |
+
|
| 407 |
+
except Exception as e:
|
| 408 |
+
logger.error(f"Failed to refresh WhatsApp QR: {e}")
|
| 409 |
+
raise HTTPException(status_code=500, detail=f"QR refresh error: {str(e)}")
|
| 410 |
+
|
| 411 |
+
|
| 412 |
@router.get("/whatsapp/qr/status")
|
| 413 |
async def get_whatsapp_qr_status(
|
| 414 |
current_user: User = Depends(get_current_user),
|