Spaces:
Running
Running
| <html lang="ar" dir="rtl"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>الدردشة مع نورا</title> | |
| <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"> | |
| <style> | |
| #chat-box { | |
| height: 70vh; | |
| overflow-y: auto; | |
| border: 1px solid #ddd; | |
| padding: 15px; | |
| background-color: #fff; | |
| } | |
| .message { | |
| margin-bottom: 10px; | |
| } | |
| .user { | |
| text-align: right; | |
| font-weight: bold; | |
| } | |
| .noura { | |
| text-align: left; | |
| color: #0d6efd; | |
| } | |
| </style> | |
| </head> | |
| <body class="bg-light p-4"> | |
| <div class="container"> | |
| <h3 class="text-center mb-4">مرحبًا بك يا {{ username }}، تحدث مع نورا</h3> | |
| <div id="chat-box" class="mb-3"></div> | |
| <form id="chat-form" class="d-flex flex-column gap-2"> | |
| <div class="d-flex"> | |
| <input type="text" id="user-input" class="form-control me-2" placeholder="اكتب رسالتك..." autocomplete="off"> | |
| <input type="file" id="file-input" class="form-control me-2" multiple> | |
| <button type="submit" class="btn btn-primary">إرسال</button> | |
| </div> | |
| </form> | |
| </div> | |
| <script> | |
| const chatBox = document.getElementById("chat-box"); | |
| const chatForm = document.getElementById("chat-form"); | |
| const userInput = document.getElementById("user-input"); | |
| const fileInput = document.getElementById("file-input"); | |
| const sendButton = chatForm.querySelector("button"); | |
| chatForm.addEventListener("submit", async (e) => { | |
| e.preventDefault(); | |
| const message = userInput.value.trim(); | |
| const files = fileInput.files; | |
| if (!message && files.length === 0) return; | |
| if (message) { | |
| chatBox.innerHTML += `<div class="message user">أنت: ${message}</div>`; | |
| } | |
| const formData = new FormData(); | |
| formData.append("username", "{{ username }}"); | |
| formData.append("message", message); | |
| for (const file of files) { | |
| formData.append("files", file); | |
| } | |
| userInput.value = ""; | |
| fileInput.value = ""; | |
| sendButton.disabled = true; | |
| try { | |
| const res = await fetch("/api", { | |
| method: "POST", | |
| body: formData | |
| }); | |
| if (!res.ok) throw new Error("فشل في الاتصال بالخادم."); | |
| const data = await res.json(); | |
| chatBox.innerHTML += `<div class="message noura">نورا: ${data.reply}</div>`; | |
| } catch (error) { | |
| chatBox.innerHTML += `<div class="message noura text-danger">حدث خطأ أثناء إرسال الرسالة أو الملفات.</div>`; | |
| } finally { | |
| chatBox.scrollTop = chatBox.scrollHeight; | |
| sendButton.disabled = false; | |
| } | |
| }); | |
| userInput.addEventListener("keydown", (e) => { | |
| if (e.key === "Enter" && !e.shiftKey) { | |
| e.preventDefault(); | |
| chatForm.dispatchEvent(new Event("submit")); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> | |