47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
$(document).ready(function() {
|
|
// Toggle chatbot visibility
|
|
$('#chatbot-toggle').click(function() {
|
|
$('#chatbot-container').fadeIn();
|
|
$('#chatbot-toggle').hide();
|
|
});
|
|
|
|
// Close chatbot
|
|
$('#chatbot-close').click(function() {
|
|
$('#chatbot-container').fadeOut();
|
|
$('#chatbot-toggle').show();
|
|
});
|
|
|
|
// Send message on button click
|
|
$('#chatbot-send').click(function() {
|
|
sendMessage();
|
|
});
|
|
|
|
// Send message on "Enter" key
|
|
$('#chatbot-input').keypress(function(e) {
|
|
if (e.which == 13) {
|
|
sendMessage();
|
|
}
|
|
});
|
|
|
|
function sendMessage() {
|
|
var message = $('#chatbot-input').val().trim();
|
|
if (message === '') return;
|
|
|
|
$('#chatbot-body').append('<div class="message user-message"><strong>You:</strong> ' + message + '</div>');
|
|
$('#chatbot-input').val('');
|
|
$('#chatbot-body').scrollTop($('#chatbot-body')[0].scrollHeight);
|
|
|
|
$.ajax({
|
|
url: '{{ route('chatbot.query') }}',
|
|
type: 'POST',
|
|
data: { message: message, _token: '{{ csrf_token() }}' },
|
|
success: function(response) {
|
|
$('#chatbot-body').append('<div class="message bot-message"><strong>Bot:</strong> ' + response.reply + '</div>');
|
|
$('#chatbot-body').scrollTop($('#chatbot-body')[0].scrollHeight);
|
|
},
|
|
error: function() {
|
|
$('#chatbot-body').append('<div class="message bot-message"><strong>Bot:</strong> Sorry, something went wrong.</div>');
|
|
}
|
|
});
|
|
}
|
|
}); |