File size: 1,348 Bytes
c33cdb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
async function sendMessage() {
    const userInput = document.getElementById("user-input").value;
    if (!userInput) return;

    addMessageToChatBox("Usuário: " + userInput);
    document.getElementById("user-input").value = "";

    try {
        const response = await fetch('https://fabiosantos-api-chatbot.hf.space/ask', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ "text": userInput })
        });

        if (!response.ok) {
            throw new Error("Erro na resposta da API");
        }

        const data = await response.json();
        if (data && data.response) {
            addMessageToChatBox("Chatbot: " + data.response);
        } else {
            addMessageToChatBox("Chatbot: Resposta não encontrada.");
        }
    } catch (error) {
        console.error("Erro:", error);
        addMessageToChatBox("Chatbot: Desculpe, ocorreu um erro ao processar sua solicitação.");
    }
}

function addMessageToChatBox(message) {
    const chatBox = document.getElementById("chat-box");
    const messageElement = document.createElement("div");
    messageElement.textContent = message;
    chatBox.appendChild(messageElement);
    chatBox.scrollTop = chatBox.scrollHeight;
}