-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
91 lines (78 loc) · 3.04 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
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
var local = {};
local.avatar = "https://image.flaticon.com/icons/png/128/149/149071.png";
var remote = {};
remote.avatar = "https://developers.viber.com/images/apps/apiai-icon.png";
var accessToken = "BOT_CLIENT_ACCESS_TOKEN_HERE";
var baseUrl = "https://api.api.ai/v1/";
function formatTime(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
function insertChat(who, text){
var control = "";
var date = formatTime(new Date());
if (who == "local"){
control = '<li style="width:100%;">' +
'<div class="msj-rta macro">' +
'<div class="text text-r">' +
'<p>'+text+'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'<div class="avatar" style="padding:0px 0px 0px 10px !important"><img class="img-circle" style="width:100%;" src="'+local.avatar+'" /></div>' +
'</li>';
}else{
control = '<li style="width:100%">' +
'<div class="msj macro">' +
'<div class="avatar"><img class="img-circle" style="width:100%;" src="'+ remote.avatar +'" /></div>' +
'<div class="text text-l">' +
'<p>'+ text +'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
$("#messages").append(control);
var objDiv = document.getElementById("messages");
objDiv.scrollTop = objDiv.scrollHeight;
}
$("#chat-panel").on('click',function(){
$(".innerframe").toggle();
});
function resetChat(){
$("#messages").empty();
}
$(".mytext").on("keyup", function(e){
if (e.which == 13){
var text = $(this).val();
if (text !== ""){
insertChat("local", text);
$(this).val('');
queryBot(text)
}
}
});
resetChat();
function queryBot(text) {
$.ajax({
type: "POST",
url: baseUrl + "query?v=20150910",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
data: JSON.stringify({ query: text, lang: "en", sessionId: "somerandomthing" }),
success: function(data) {
insertChat("remote",data.result.fulfillment.speech);
},
error: function() {
insertChat("remote","Internal Server Error");
}
});
}