-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (162 loc) Β· 5.3 KB
/
main.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
import sendToDb from "./db.js";
const speak = document.querySelector("#speak");
const me = document.querySelector("#me");
const result = document.querySelector("#resultsContainer");
const listen = new webkitSpeechRecognition();
listen.continues = false;
listen.lang = "en-US";
listen.interimResults = false;
let synth = window.speechSynthesis;
let isRecording = false;
speak.onclick = () => {
listen.start();
speak.classList.add("onRecording");
speak.classList.remove("stopRecording");
};
let yoxo = new SpeechSynthesisUtterance("hey, how are you");
yoxo.onend = () => {
listen.start();
};
listen.onresult = (e) => {
let textContent = e.results[e.results.length - 1][0].transcript.trim();
let response = textContent.split(" ");
me.innerHTML = textContent;
if (
response.includes("what") ||
response.includes("about") ||
response.includes("who")
) {
let wikipediaUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&prop=info&inprop=url&utf8=&format=json&origin=*&srlimit=20&srsearch=${textContent}`;
fetch(wikipediaUrl)
.then((re) => {
return re.json();
})
.then((data) => {
let random = Math.random() * data.query.search.length;
let index = Math.floor(random);
console.log(data.query.search[index]);
listen.stop();
yoxo.text = data.query.search[0].snippet;
result.innerHTML = data.query.search[0].snippet;
synth.speak(yoxo);
});
} else if (response.includes("youxo")) {
say("yes their , can i help you ?", textContent, "π€");
} else if (response.includes("hello")) {
say("Hello their, can i help you ? ", textContent, "Hello mr yassin");
} else if (response.includes("location")) {
listen.stop();
navigator.geolocation.getCurrentPosition((position) => {
const { latitude, longitude } = position.coords;
const url =
"https://nominatim.openstreetmap.org/reverse?format=json&lat=" +
latitude +
"&lon=" +
longitude +
"&zoom=18&addressdetails=1";
fetch(url)
.then((res) => {
return res.json();
})
.then((data) => {
say("you'r location right now is, " + data.display_name, textContent);
sendToDb( textContent ,data.display_name)
})
.catch((err) => {
console.log(err);
});
});
synth.speak(yoxo);
} else if (response.includes("yes")) {
say("what do you want?", textContent);
} else if (response.includes("info")) {
say(
"Hello my name is youxo i developed by yassin elhrdouf, you can ask me about movie, or music, or if you need advice, im here for you ",
textContent
);
} else if (response.includes("f***")) {
say("fuck you to", textContent, "π€¬");
} else if (response.includes("thank")) {
say("you welcome, can i help you more ?", textContent);
} else if (response.includes("are") && response.includes("you")) {
say("fine and you ?", textContent, "π₯°");
} else if (
(response.includes("I") && response.includes("fine")) ||
response.includes("good")
) {
say("thats goode", textContent, "π€");
} else if (response.includes("okay")) {
say("okay i'm withing for you'r questions", textContent, "πͺ");
} else if (
response.includes("what's") &&
response.includes("your") &&
response.includes("name")
) {
say("My name is youxo and i developed by yassine", textContent);
} else if (response.includes("tell") && response.includes("joke")) {
fetch("https://official-joke-api.appspot.com/jokes/random")
.then((res) => {
return res.json();
})
.then((data) => {
say(
"okay, this joke for you, " +
data.setup +
" Because " +
data.punchline +
" hahahahahahahahaha",
textContent,
"π"
);
});
} else if (
response.includes("how") &&
response.includes("old") &&
response.includes("you")
) {
say("you know that im robot π", textContent, "π");
} else if (response.includes("time")) {
say(
"its " + date.getHours() + " : " + date.getMinutes() + " pm",
textContent,
"π§"
);
} else if (response.includes("advice")) {
fetch("https://api.adviceslip.com/advice")
.then((res) => {
return res.json();
})
.then((data) => {
say(data.slip.advice, textContent);
});
} else if (response.includes("yassin")) {
say("Yassin Elhrdouf is programmer with high experience in JavaScript");
} else if (response.includes("start")) {
say("Hello world,", textContent);
} else if (response.includes("zakaria") || response.includes("solali")) {
say(
"Zakariya Solali is sportive man with high experience in street workout "
);
} else {
say("Sorry i don't know what you need !", textContent);
}
};
async function say(text, textContent, log) {
listen.stop();
let date = new Date();
yoxo.text = text;
synth.speak(yoxo);
console.log(log);
console.log(textContent);
me.innerHTML = textContent;
result.innerHTML = text;
sendToDb(textContent , text )
}
listen.onspeechstart = (e) => {
speak.classList.add("onRecording");
speak.classList.remove("stopRecording");
};
listen.onspeechend = (e) => {
speak.classList.add("stopRecording");
speak.classList.remove("onRecording");
};