{% extends 'home/homeLayout.html.twig' %} {% block stylesheets %} {{ parent() }}
<link rel="stylesheet" href="{{ asset('css/forum.css') }}?v={{ random() }}"> {% endblock %} {% block body %}
<div class="chatbot_page">
<div class="container-sm">
<div class="holder">
<div>
<div class="indicators">
<div class="single_in">
<img src="{{ asset('images/forum/how.png') }}" alt="">
<div class="txt">
How
</div>
</div>
<div class="single_in">
<img src="{{ asset('images/forum/may i.png') }}" alt="">
<div class="txt">
may i
</div>
</div>
<div class="single_in">
<img src="{{ asset('images/forum/help.png') }}" alt="">
<div class="txt">
help you ?
</div>
</div>
</div>
<div class="list" id="chatContainer">
</div>
</div>
<div class="send_box">
<input type="text" class="form-control" placeholder="Type your message.... " id="messageInput">
<button id="sendMessage">Envoyer</button>
</div>
</div>
</div>
</div>
{% block javascripts %}
<script>
var userImage = "{{ userImage|default(asset('images/cours/person.jpg')) }}";
console.log("fffffffffffffffffffffffff");
document.getElementById("sendMessage").addEventListener("click", function() {
console.log("clicked!");
var messageInput = document.getElementById("messageInput");
var userMessage = messageInput.value;
if (userMessage.trim() !== '') {
fetch('{{ path('home_forum_chatbot_action') }}', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest', // Important for Symfony to recognize it as an AJAX request
},
body: JSON.stringify({message: userMessage})
})
.then(response => response.json())
.then(data => {
addMessageToChat(false, userMessage); // Display user's message
addMessageToChat(true, data.message); // Display bot's response
messageInput.value = ''; // Clear input field
})
.catch(error => console.error('Error:', error));
}
});
function addMessageToChat(isBot, message) {
var chatContainer = document.getElementById("chatContainer");
var messageElement = document.createElement("div");
messageElement.innerHTML = '<div class="single ' + (isBot ? 'bot' : '') + '"><div class="avatar"><img src="' + (isBot ? '{{ asset("images/forum/chat.jpg") }}' : userImage) + '" alt=""></div><div class="text">' + message + '</div></div>';
chatContainer.appendChild(messageElement);
}
</script>
{% endblock %}
{% endblock %}