-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
139 lines (119 loc) · 5.14 KB
/
script.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const chatbotToggler = document.querySelector(".chatbot-toggler");
const closeBtn = document.querySelector(".close-btn");
const chatbox = document.querySelector(".chatbox");
const chatbot = document.querySelector(".chatbot");
const chatInput = document.querySelector(".chat-input textarea");
const sendChatBtn = document.querySelector(".chat-input span");
let userMessage = null; // Variable to store user's message
const inputInitHeight = chatInput.scrollHeight;
const btn = document.getElementById('access-chatbot');
btn.addEventListener('click', function handleClick() {
const initialText = 'Go to Comet - our Emotional Support Bot';
if (btn.textContent.toLowerCase().includes(initialText.toLowerCase())) {
btn.textContent = 'Close chats';
console.log("Change mein aaya toh")
} else {
btn.textContent = initialText;
}
});
const createChatLi = (message, className) => {
// Create a chat <li> element with passed message and className
const chatLi = document.createElement("li");
chatLi.classList.add("chat", `${className}`);
let chatContent = className === "outgoing" ? `<p></p>` : `<span class="material-symbols-outlined">🤖</span><p></p>`;
chatLi.innerHTML = chatContent;
chatLi.querySelector("p").textContent = message;
return chatLi; // return chat <li> element
}
const handleChat = () => {
userMessage = chatInput.value.trim(); // Get user entered message and remove extra whitespace
if (!userMessage) {
return;
}
// Clear the input textarea and set its height to default
chatInput.value = "";
chatInput.style.height = `${inputInitHeight}px`;
// Append the user's message to the chatbox
chatbox.appendChild(createChatLi(userMessage, "outgoing"));
chatbox.scrollTo(0, chatbox.scrollHeight);
console.log(userMessage)
fetch('http://localhost:5001/api/call_python_function', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ input: userMessage })
})
.then(response => response.json())
.then(data => {
console.log(data)
let res = data["result"]
const incomingChatLi = createChatLi(res, "incoming");
chatbox.appendChild(incomingChatLi);
chatbox.scrollTo(0, chatbox.scrollHeight);
// debugg er
const c = document.getElementsByClassName("chat-input");
// document.getElementsByClassName("chat-input").scrollIntoView(false);
// document.getElementsByClassName("chat-input").scrollIntoView({ block: "end" });
// document.getElementsByClassName("chat-input").scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
chatbot.scrollBy(300, 300);
chatInput.focus();
})
.catch(error => {
console.error('Error:', error);
});
}
chatInput.addEventListener("input", () => {
// Adjust the height of the input textarea based on its content
chatInput.style.height = `${inputInitHeight}px`;
chatInput.style.height = `${chatInput.scrollHeight}px`;
});
chatInput.addEventListener("keydown", (e) => {
// If Enter key is pressed without Shift key and the window
// width is greater than 800px, handle the chat
if (e.key === "Enter" && !e.shiftKey && window.innerWidth > 800) {
e.preventDefault();
handleChat();
}
});
sendChatBtn.addEventListener("click", handleChat);
closeBtn.addEventListener("click", () => document.body.classList.remove("show-chatbot"));
chatbotToggler.addEventListener("click", () => document.body.classList.toggle("show-chatbot"));
function showInformation() {
// Get the selected option value
var selectedOption = document.getElementById("resourcesDropdown").value;
// Hide all information divs
var informationDivs = document.querySelectorAll('.resource-info');
informationDivs.forEach(function (div) {
div.style.display = 'none';
});
// Show the selected information div
var selectedInfoDiv = document.getElementById(selectedOption + '-info');
if (selectedInfoDiv) {
selectedInfoDiv.style.display = 'block';
}
var logoDiv = document.getElementById("logo-resource");
logoDiv.style.display = 'none';
}
function submitForm(event) {
event.preventDefault(); // Prevent the default form submission
// Get form data
const formData = new FormData(event.target);
// Make an asynchronous request to the Web3Forms API
fetch("https://api.web3forms.com/submit", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
// Handle the response data if needed
console.log(data);
document.getElementById('msg').innerText = 'Form submitted successfully!';
// Optionally, reset the form
event.target.reset();
})
.catch(error => {
console.error('Error:', error);
document.getElementById('msg').innerText = 'Error submitting the form.';
});
}