This commit is contained in:
Flatlogic Bot 2026-02-07 19:44:36 +00:00
parent 1bd025b08b
commit a7f729c130
2 changed files with 16 additions and 2 deletions

View File

@ -4,9 +4,12 @@ from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse from django.http import HttpResponse
from django.utils import timezone from django.utils import timezone
import json import json
import logging
from .models import Fanpage, Flow, MessageLog, ChatSession from .models import Fanpage, Flow, MessageLog, ChatSession
from .utils import handle_webhook_event from .utils import handle_webhook_event
logger = logging.getLogger(__name__)
def home(request): def home(request):
if request.user.is_authenticated: if request.user.is_authenticated:
return redirect('dashboard') return redirect('dashboard')
@ -50,12 +53,22 @@ def webhook(request):
challenge = request.GET.get('hub.challenge') challenge = request.GET.get('hub.challenge')
if mode == 'subscribe' and token: if mode == 'subscribe' and token:
print(f"DEBUG: Webhook verification attempt - mode: {mode}, token: {token}")
# Check if this token matches any Fanpage verify_token # Check if this token matches any Fanpage verify_token
# or the one specifically mentioned by the user # or the default 'pages_messaging'
if Fanpage.objects.filter(verify_token=token).exists() or token == 'pages_messaging': if Fanpage.objects.filter(verify_token=token).exists() or token == 'pages_messaging':
print(f"DEBUG: Webhook verification SUCCESS")
return HttpResponse(challenge) return HttpResponse(challenge)
else:
print(f"DEBUG: Webhook verification FAILED - Token mismatch: {token}")
return HttpResponse('Verification failed: Token mismatch', status=403)
return HttpResponse('Verification failed', status=403) # If no parameters, return 200 with instructions instead of 403 to avoid confusion
if not mode and not token:
print(f"DEBUG: Webhook endpoint accessed via browser (GET without parameters)")
return HttpResponse('Facebook Webhook Endpoint is active. Please use this URL in your Facebook App settings for Webhooks configuration.', status=200)
return HttpResponse('Verification failed: Missing parameters', status=403)
elif request.method == 'POST': elif request.method == 'POST':
# Handle incoming messages # Handle incoming messages
@ -66,6 +79,7 @@ def webhook(request):
return HttpResponse('EVENT_RECEIVED') return HttpResponse('EVENT_RECEIVED')
except Exception as e: except Exception as e:
# Log the error if necessary # Log the error if necessary
print(f"ERROR processing webhook: {str(e)}")
return HttpResponse('Error processing request', status=400) return HttpResponse('Error processing request', status=400)
return HttpResponse('Method not allowed', status=405) return HttpResponse('Method not allowed', status=405)