-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (53 loc) · 2.05 KB
/
app.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
$(document).ready(function () {
var chatContainer = $('#chat-container');
var userInput = $('#user-input');
var sendButton = $('#send-btn');
// Function to add a user message to the chat container
function addUserMessage(message) {
chatContainer.append('<div class="user-message"><span class="user-icon"><i class="fas fa-user"></i></span>' + message + '</div>');
}
// Function to add a bot message to the chat container
function addBotMessage(message) {
chatContainer.append('<div class="bot-message"><span class="bot-icon"><i class="fas fa-robot"></i></span>' + message + '</div>');
}
// Function to send user input to the Rasa server
function sendMessage() {
var userMessage = userInput.val();
addUserMessage(userMessage);
// Make a POST request to the Rasa server API
$.ajax({
url: 'http://localhost:5005/webhooks/rest/webhook',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
sender: 'user',
message: userMessage
}),
success: function (data) {
if (data && data.length > 0) {
data.forEach(element => {
var botResponse = element.text;
addBotMessage(botResponse);
});
//var botResponse = data[0].text;
//addBotMessage(botResponse);
}
},
error: function () {
addBotMessage('Oops! Something went wrong. Please try again.');
}
});
// Clear the user input field after sending the message
userInput.val('');
}
// Bind event listener to the send button
sendButton.on('click', function () {
sendMessage();
});
// Bind event listener to the user input field to handle "Enter" key press
userInput.on('keyup', function (event) {
if (event.key === 'Enter') {
sendMessage();
}
});
});