-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
135 lines (113 loc) · 4.16 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
// on input/text enter--------------------------------------------------------------------------------------
$('.usrInput').on('keyup keypress', function (e) {
var keyCode = e.keyCode || e.which;
var text = $(".usrInput").val();
if (keyCode === 13) {
if (text == "" || $.trim(text) == '') {
e.preventDefault();
return false;
} else {
$(".usrInput").blur();
setUserResponse(text);
send(text);
e.preventDefault();
return false;
}
}
});
//------------------------------------- Set user response------------------------------------
function setUserResponse(val) {
var UserResponse = '<img class="userAvatar" src=' + "./static/img/userAvatar.jpg" + '><p class="userMsg">' + val + ' </p><div class="clearfix"></div>';
$(UserResponse).appendTo('.chats').show('slow');
$(".usrInput").val('');
scrollToBottomOfResults();
$('.suggestions').remove();
}
//---------------------------------- Scroll to the bottom of the chats-------------------------------
function scrollToBottomOfResults() {
var terminalResultsDiv = document.getElementById('chats');
terminalResultsDiv.scrollTop = terminalResultsDiv.scrollHeight;
}
function send(message) {
console.log("User Message:", message)
$.ajax({
url: 'https://rasablog.herokuapp.com/webhooks/rest/webhook',
type: 'POST',
data: JSON.stringify({
"message": message,
"sender": "username"
}),
success: function (data, textStatus) {
setBotResponse(data);
console.log("Rasa Response: ", data, "\n Status:", textStatus)
},
error: function (errorMessage) {
setBotResponse("");
console.log('Error' + errorMessage);
}
});
}
//------------------------------------ Set bot response -------------------------------------
function setBotResponse(val) {
setTimeout(function () {
if (val.length < 1) {
//if there is no response from Rasa
msg = 'I couldn\'t get that. Let\' try something else!';
var BotResponse = '<img class="botAvatar" src="./static/img/botAvatar.png"><p class="botMsg">' + msg + '</p><div class="clearfix"></div>';
$(BotResponse).appendTo('.chats').hide().fadeIn(1000);
} else {
//if we get response from Rasa
for (i = 0; i < val.length; i++) {
//check if there is text message
if (val[i].hasOwnProperty("text")) {
var BotResponse = '<img class="botAvatar" src="./static/img/botAvatar.png"><p class="botMsg">' + val[i].text + '</p><div class="clearfix"></div>';
$(BotResponse).appendTo('.chats').hide().fadeIn(1000);
}
//check if there is image
if (val[i].hasOwnProperty("image")) {
var BotResponse = '<div class="singleCard">' +
'<img class="imgcard" src="' + val[i].image + '">' +
'</div><div class="clearfix">'
$(BotResponse).appendTo('.chats').hide().fadeIn(1000);
}
//check if there is button message
if (val[i].hasOwnProperty("buttons")) {
addSuggestion(val[i].buttons);
}
}
scrollToBottomOfResults();
}
}, 500);
}
// ------------------------------------------ Toggle chatbot -----------------------------------------------
$('#profile_div').click(function () {
$('.profile_div').toggle();
$('.widget').toggle();
scrollToBottomOfResults();
});
$('#close').click(function () {
$('.profile_div').toggle();
$('.widget').toggle();
});
// ------------------------------------------ Suggestions -----------------------------------------------
function addSuggestion(textToAdd) {
setTimeout(function () {
var suggestions = textToAdd;
var suggLength = textToAdd.length;
$(' <div class="singleCard"> <div class="suggestions"><div class="menu"></div></div></diV>').appendTo('.chats').hide().fadeIn(1000);
// Loop through suggestions
for (i = 0; i < suggLength; i++) {
$('<div class="menuChips" data-payload=\''+(suggestions[i].payload)+'\'>' + suggestions[i].title + "</div>").appendTo(".menu");
}
scrollToBottomOfResults();
}, 1000);
}
// on click of suggestions, get the value and send to rasa
$(document).on("click", ".menu .menuChips", function () {
var text = this.innerText;
var payload= this.getAttribute('data-payload');
console.log("button payload: ",this.getAttribute('data-payload'))
setUserResponse(text);
send(payload);
$('.suggestions').remove(); //delete the suggestions
});