-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
273 lines (243 loc) · 9.58 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* created by Samyak Jain
on 15 July 2020 */
/// onload function
onload = function () {
console.log("here onload");
askedWeather = false;
askedNews = false;
rnd2 = 'd61d4157accb44';
newsCnt = 0;
jokesCnt = 0;
replyCnt = -1;
chatEnd = false;
/// sounds initialisation
userMsgSound = new Sound("media/userMsgSound.mp3");
botMsgSound = new Sound("media/botMsgSound.mp3");
/// chat object
var chatObj = {
/// initialisation
init: function () {
this.cacheDom();
this.eventBind();
rnd3 = 'top-news?country=in&token=114059f';
// botMsgSound.play();
},
/// accesing dom elements
cacheDom: function () {
this.chatBox = document.getElementById("chatbox");
this.msgBox = document.getElementById("msgbox");
this.msgList = document.getElementById("msgList");
this.typeBox = document.getElementById("typebox");
this.sendBtn = document.getElementById("sendBtn");
},
/// adding event listeners
eventBind: function () {
sendBtn.addEventListener("click", this.addMsg);
typeBox.addEventListener("keyup", this.addMsgEnter);
rnd4 = 'current.json?key=c2cf5e10';
},
/// renders user message on screen
userRender: async function () {
// console.log("here render");
scrollBottom();
if (this.newMsg.length !== 0) {
userMsgSound.play();
showMsg(this.newMsg, "userMsg");
typeBox.value = "";
setTimeout(function () {
chatObj.botRender();
}, 750);
}
scrollBottom();
},
/// rrenders bot message on screen
botRender: async function () {
let totalOptions = 4;
if (this.newMsg.toLowerCase() === "bye" || this.newMsg.toLowerCase() === "'bye'") {
if (chatEnd === false) {
botMsgSound.play();
showMsg("Chat has ended !", "botMsg");
setTimeout(
function () {
showMsg("Enter 'reset' to start the chat again !", "botMsg");
botMsgSound.play();
}, 1000
);
chatEnd = true;
}
else {
botMsgSound.play();
showMsg("Chat has ended ! Enter 'reset' to start the chat again !", "botMsg");
}
return;
}
if (chatEnd === true) {
if (this.newMsg.toLowerCase() === "reset" || this.newMsg.toLowerCase() === "'reset'") {
location.reload();
}
else {
botMsgSound.play();
showMsg("Chat has ended ! Enter 'reset' to start the chat again !", "botMsg");
}
return;
}
if (this.newMsg === "#" || this.newMsg === "'#'" || this.newMsg === "' # '") {
botMsgSound.play();
showMenu();
return;
}
let optionNo = parseInt(this.newMsg);
if (isNaN(optionNo) == false && 1 <= optionNo && optionNo <= totalOptions) {
var data = await functionsArr[optionNo - 1]();
replyCnt++;
// var data = await eval("functionsArr[optionNo - 1]()");
}
else {
data = "Please enter a valid option number (1 to 4) and try again ! :) <br>     Or<br> Enter 'bye' if you want to end the chat.";
}
botMsgSound.play();
showMsg(data, "botMsg");
if (replyCnt === 0) {
setTimeout(
function () {
showMsg("Great !! Keep entering option numbers to keep chatting with me. <br><br>You can enter ' # ' anytime to see the options menu again ! :) <br><br> Enter 'bye' to end the chat at any time.", "botMsg");
botMsgSound.play();
}, 1000
);
replyCnt = 1;
}
},
/// gets called when send button is clicked
addMsg: function () {
// console.log("here addMsg");
chatObj.newMsg = typeBox.value.trim();
chatObj.userRender();
},
/// gets called when enter is pressed
addMsgEnter: function (e) {
// console.log("here addMsg");
if (e.key === "Enter") {
chatObj.newMsg = typeBox.value.trim();
chatObj.userRender();
}
}
}
rnd1 = 'b8d7166488c'
/// calling init function
chatObj.init();
}
/// stores functions for each option
var functionsArr = [
getNews,
getWeather,
getFact,
getJoke
]
rnd6 = '40202407&q=Delhi';
/// will store json returned from api calls
var jsonObj = {
}
/// function to get news
async function getNews() {
if (askedNews === false) {
console.log("news api called");
const response = await fetch('https://gnews.io/api/v3/' + rnd3 + rnd1 + rnd2);
const jsonResp = await response.json();
jsonObj.newsJson = jsonResp;
console.log("total articles= " + jsonObj.newsJson.articleCount);
askedNews = true;
newsCnt++;
let artLinkStr = jsonResp.articles[0].source.url;
return jsonResp.articles[0].title + "<br><br>Source: " + jsonResp.articles[0].source.name + "<br><a href=\"" + artLinkStr + "\" target=\"_blank\">" + "Article Link" + "</a>" + "<br><br>Enter 1 again to read another news or choose another option !";
}
else {
let foo = jsonObj.newsJson;
let ind = newsCnt % (foo.articleCount);
console.log("ind= " + ind);
newsCnt++;
let artLinkStr = foo.articles[ind].source.url;
return foo.articles[ind].title + "<br><br>Source: " + foo.articles[ind].source.name + "<br><a href=\"" + artLinkStr + "\" target=\"_blank\">" + "Article Link" + "</a>" + "<br><br>Enter 1 again to read another news or choose another option !";
}
// return "This is a news";
}
/// function to get weather
async function getWeather() {
if (askedWeather === false) {
console.log("weather api called");
const response = await fetch('https://api.weatherapi.com/v1/' + rnd4 + rnd5 + rnd6);
const jsonResp = await response.json();
askedWeather = true;
jsonObj.weatherJson = jsonResp;
return "Current Temperature: " + jsonResp.current.temp_c + "\xB0C<br>Feels Like: " + jsonResp.current.feelslike_c + "\xB0C<br>Cloud Cover: " + jsonResp.current.cloud + "%<br>Humidity: " + jsonResp.current.humidity + "%" + "<br><br>Enter 2 again to get the weather update again or choose another option !";
}
else {
let foo = jsonObj.weatherJson;
return "Current Temperature: " + foo.current.temp_c + "\xB0C<br>Feels Like: " + foo.current.feelslike_c + "\xB0C<br>Cloud Cover: " + foo.current.cloud + "%<br>Humidity: " + foo.current.humidity + "%" + "<br><br>Enter 2 again to get the weather update again or choose another option !";
}
// return "This is the weather";
}
/// function to get fact
async function getFact() {
const response = await fetch('https://uselessfacts.jsph.pl//random.json?language=en');
const jsonResp = await response.json();
return jsonResp.text + "<br><br>Enter 3 again to read another fun fact or choose another option !";
// return "Fun Fact: I am a Bot but I have feelings too!! :)";
}
/// function to get joke
async function getJoke() {
if (jokesCnt % 10 === 9) {
const response = await fetch('https://api.icndb.com/jokes/random');
const jsonResp = await response.json();
jokesCnt++;
return jsonResp.value.joke + "<br><br>Enter 4 again to read another joke or choose another option !";
}
else {
const options = {
headers: {
Accept: "application/json"
}
};
const response = await fetch("https://icanhazdadjoke.com/", options);
const jsonResp = await response.json();
jokesCnt++;
return jsonResp.joke + "<br><br>Enter 4 again to read another joke or choose another option !";
}
}
/// function to scroll to bottom of page
function scrollBottom() {
var scrollElm = document.scrollingElement;
scrollElm.scrollTop = scrollElm.scrollHeight;
}
/// function that prints menu
function showMenu() {
let nMsg = document.createElement('li');
nMsg.innerHTML = "Menu: <br>1.Check the news <br>2.Get weather updates <br>3.Read a fun fact <br>4.Read a joke <br><br>Enter an option number to continue or enter 'bye' to end the chat.";
nMsg.className = "botMsg";
msgList.appendChild(nMsg);
}
rnd5 = '2508406abcc1446';
/// function that prints messages
function showMsg(data, msgClass) {
let nMsg = document.createElement('li');
nMsg.innerHTML = data;
nMsg.className = msgClass;
msgList.appendChild(nMsg);
}
/// class to handle sounds
class Sound {
constructor(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
/// method definition
this.play = function () {
this.sound.play();
};
this.stop = function () {
this.sound.pause();
};
}
}