-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
160 lines (140 loc) · 5.93 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
onload = function(){
// outputs a javascript object from the parsed json
var chat = {
messageToSend: '',
messageResponses: [
'Why did the web developer leave the restaurant? Because of the table layout.',
'How do you comfort a JavaScript bug? You console it.',
'An SQL query enters a bar, approaches two tables and asks: "May I join you?"',
'What is the most used language in programming? Profanity.',
'What is the object-oriented way to become wealthy? Inheritance.',
'An SEO expert walks into a bar, bars, pub, tavern, public house, Irish pub, drinks, beer, alcohol'
],
init: async function() {
this.chatTree = new ChatTree();
await this.chatTree.init();
this.cacheDOM();
this.bindEvents();
await this.render();
},
cacheDOM: function() {
this.$chatHistory = $('.chat-history');
this.$button = $('button');
this.$textarea = $('#message-to-send');
this.$chatHistoryList = this.$chatHistory.find('ul');
},
bindEvents: function() {
this.$button.on('click', this.addMessage.bind(this));
this.$textarea.on('keyup', this.addMessageEnter.bind(this));
},
render: async function() {
this.scrollToBottom();
if (this.messageToSend.trim() !== '') {
var template = Handlebars.compile( $("#message-template").html());
var context = {
messageOutput: this.messageToSend,
time: this.getCurrentTime()
};
this.input = this.messageToSend;
this.$chatHistoryList.append(template(context));
this.scrollToBottom();
this.$textarea.val('');
// responses
var templateResponse = Handlebars.compile( $("#message-response-template").html());
var contextResponse = {
response: await this.chatTree.getMessage(this.input),
time: this.getCurrentTime()
};
setTimeout(function() {
this.$chatHistoryList.append(templateResponse(contextResponse));
this.scrollToBottom();
}.bind(this), 1000);
}
},
addMessage: function() {
this.messageToSend = this.$textarea.val();
this.render();
},
addMessageEnter: function(event) {
// enter was pressed
if (event.keyCode === 13) {
this.addMessage();
}
},
scrollToBottom: function() {
this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
},
getCurrentTime: function() {
return new Date().toLocaleTimeString().
replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
}
};
chat.init();
};
class ChatTree {
constructor() {
}
async init(){
const data = await this.reset();
this.chat_tree = data;
this.firstMsg = true;
console.log("inside done");
return "Chat has now been terminated. Send hi to begin chat again !";
}
async reset(){
const response = await fetch('chat_tree.json');
const jsonResponse = await response.json();
return jsonResponse;
}
async getMessage(input){
let resp = '';
//input = new String(input.trim());
//console.log(input);
if(this.firstMsg===true) {
this.firstMsg = false;
resp += "Hey there buddy<br>";
} else {
if(("message" in this.chat_tree) && (input.trim()==="Reset")) {
return this.init();
}
if(isNaN(parseInt(input)) || parseInt(input)<=0 || parseInt(input) > this.chat_tree['children'].length+1)
return 'It seems like you gave a wrong input ! Go ahead try again !';
if(parseInt(input)-1===this.chat_tree['children'].length){
this.init();
}
this.chat_tree = this.chat_tree['children'][parseInt(input)-1];
}
if("message" in this.chat_tree){
let data;
if(this.chat_tree['type']==="function"){
// console.log(String(this.chat_tree['message']),String("getJoke()"));
if(this.chat_tree['message']==="getJoke()"){
data = await eval(this.chat_tree['message']);
data = data.value.joke;
} else{
data = await eval(this.chat_tree['message']);
data = data.articles[0].title;
}
} else{
data = this.chat_tree['message'];
}
resp += data;
resp += "<br><br>Please input <b>Reset</b> to reset chat now";
} else {
for (let i in this.chat_tree['child_msg']) {
resp += String(parseInt(i) + 1) + ". " + this.chat_tree['child_msg'][parseInt(i)] + "<br>";
}
}
return resp;
}
}
async function getJoke() {
const response = await fetch('http://api.icndb.com/jokes/random');
const jsonResp = await response.json();
return jsonResp;
}
async function getNews() {
const response = await fetch('http://newsapi.org/v2/top-headlines?country=in&pageSize=1&apiKey=a876816f98574cdfa23ffdc7d531c7bc');
const jsonResp = await response.json();
return jsonResp;
}