first
This commit is contained in:
0
Modules/Chatbot/resources/assets/js/app.js
Normal file
0
Modules/Chatbot/resources/assets/js/app.js
Normal file
47
Modules/Chatbot/resources/assets/js/chatbot.js
Normal file
47
Modules/Chatbot/resources/assets/js/chatbot.js
Normal file
@@ -0,0 +1,47 @@
|
||||
$(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>');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user