85 lines
3.3 KiB
Python
85 lines
3.3 KiB
Python
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.http import HttpResponse
|
|
from django.utils import timezone
|
|
import json
|
|
import logging
|
|
from .models import Fanpage, Flow, MessageLog, ChatSession
|
|
from .utils import handle_webhook_event
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def home(request):
|
|
if request.user.is_authenticated:
|
|
return redirect('dashboard')
|
|
return render(request, 'core/index.html')
|
|
|
|
@login_required
|
|
def dashboard(request):
|
|
fanpages = Fanpage.objects.all()
|
|
flows = Flow.objects.all()
|
|
recent_logs = MessageLog.objects.order_by('-timestamp')[:10]
|
|
|
|
# Count messages from today
|
|
today = timezone.now().date()
|
|
today_messages_count = MessageLog.objects.filter(timestamp__date=today).count()
|
|
|
|
context = {
|
|
'fanpage_count': fanpages.count(),
|
|
'flow_count': flows.count(),
|
|
'fanpages': fanpages,
|
|
'recent_logs': recent_logs,
|
|
'today_messages_count': today_messages_count,
|
|
}
|
|
return render(request, 'core/dashboard.html', context)
|
|
|
|
@login_required
|
|
def fanpage_list(request):
|
|
fanpages = Fanpage.objects.all()
|
|
return render(request, 'core/fanpage_list.html', {'fanpages': fanpages})
|
|
|
|
@login_required
|
|
def flow_list(request):
|
|
flows = Flow.objects.all()
|
|
return render(request, 'core/flow_list.html', {'flows': flows})
|
|
|
|
@csrf_exempt
|
|
def webhook(request):
|
|
if request.method == 'GET':
|
|
# Facebook Webhook verification
|
|
mode = request.GET.get('hub.mode')
|
|
token = request.GET.get('hub.verify_token')
|
|
challenge = request.GET.get('hub.challenge')
|
|
|
|
if mode == 'subscribe' and token:
|
|
print(f"DEBUG: Webhook verification attempt - mode: {mode}, token: {token}")
|
|
# Check if this token matches any Fanpage verify_token
|
|
# or the default 'pages_messaging'
|
|
if Fanpage.objects.filter(verify_token=token).exists() or token == 'pages_messaging':
|
|
print(f"DEBUG: Webhook verification SUCCESS")
|
|
return HttpResponse(challenge)
|
|
else:
|
|
print(f"DEBUG: Webhook verification FAILED - Token mismatch: {token}")
|
|
return HttpResponse('Verification failed: Token mismatch', 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':
|
|
# Handle incoming messages
|
|
try:
|
|
data = json.loads(request.body.decode('utf-8'))
|
|
# Process the webhook payload
|
|
handle_webhook_event(data)
|
|
return HttpResponse('EVENT_RECEIVED')
|
|
except Exception as e:
|
|
# Log the error if necessary
|
|
print(f"ERROR processing webhook: {str(e)}")
|
|
return HttpResponse('Error processing request', status=400)
|
|
|
|
return HttpResponse('Method not allowed', status=405) |