File size: 2,929 Bytes
1836e17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chatbot</title>
    <style>

        /* Tambahkan gaya CSS Anda di sini */

        body {

            font-family: Arial, sans-serif;

        }

        .container {

            max-width: 800px;

            margin: 0 auto;

            padding: 20px;

        }

        .user-message {

            background-color: #007BFF;

            color: white;

            padding: 10px;

            border-radius: 10px;

            margin-bottom: 10px;

            text-align: right;

        }

        .bot-message {

            background-color: #28A745;

            color: white;

            padding: 10px;

            border-radius: 10px;

            margin-bottom: 10px;

        }

    </style>
</head>
<body>
    <div class="container">
        <h1>Chatbot</h1>
        <div id="chat-container">
            <!-- Pesan dari chatbot akan ditambahkan di sini -->
        </div>
        <form id="chat-form">
            <input type="text" id="user-input" placeholder="Tulis pesan Anda...">
            <button type="submit">Kirim</button>
        </form>
    </div>
    <script>

        // Script JavaScript untuk mengirim pesan ke server dan menerima balasan

        document.addEventListener('DOMContentLoaded', function () {

            const chatContainer = document.getElementById('chat-container');

            const chatForm = document.getElementById('chat-form');

            const userInput = document.getElementById('user-input');



            chatForm.addEventListener('submit', function (e) {

                e.preventDefault();

                const userMessage = userInput.value.trim();

                if (userMessage === '') return;



                // Tampilkan pesan pengguna

                chatContainer.innerHTML += '<div class="user-message">' + userMessage + '</div>';

                userInput.value = '';



                // Kirim pesan ke server (ganti dengan endpoint yang sesuai)

                fetch('/', {

                    method: 'POST',

                    body: new URLSearchParams({ user_input: userMessage }),  // Kirim data sebagai URLSearchParams

                    headers: {

                        'Content-Type': 'application/x-www-form-urlencoded'  // Set header sesuai dengan data yang dikirim

                    }

                })

                .then(response => response.json())

                .then(data => {

                    // Tampilkan balasan dari server

                    chatContainer.innerHTML += '<div class="bot-message">' + data.response + '</div>';

                })

                .catch(error => {

                    console.error('Error:', error);

                });

            });

        });

    </script>
</body>
</html>