from django.contrib.auth.mixins import LoginRequiredMixin from django.views import View from django.shortcuts import render from django.http import JsonResponse from .chatbot_logic import get_gpt4_response import json class ChatbotView(LoginRequiredMixin, View): def get(self, request): return render(request, "haikalbot/chatbot.html") def post(self, request): dealer = request.user.dealer try: data = json.loads(request.body) user_message = data.get("message", "").strip() if not user_message: return JsonResponse({"error": "Message cannot be empty."}, status=400) chatbot_response = get_gpt4_response(user_message, dealer) return JsonResponse({"response": chatbot_response}, status=200) except json.JSONDecodeError: return JsonResponse({"error": "Invalid JSON format."}, status=400)