-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
49 lines (45 loc) · 1.15 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
const button = document.getElementById('button')
const audioElement = document.getElementById('audio')
// Disable/Enable button
function toogleButton() {
button.disabled = !button.disabled
}
// Passing Joke to VoiceRSS API
function tellMe(joke) {
VoiceRSS.speech({
key: 'ae6a6c313f7249d8bbb25c411e20994a',
src: joke,
hl: 'en-us',
v: 'Linda',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false,
})
}
// Get Jokes from API
async function getJokes() {
let joke = ''
const apiUrl =
'https://dad-jokes.p.rapidapi.com/random/joke/?rapidapi-key=66ed413666msh76bcdace46a32a9p1a7be3jsn92826b81fe72'
try {
const response = await fetch(apiUrl)
const data = await response.json()
// if (data.setup) {
joke = `${data.body[0].setup} ... ${data.body[0].punchline}`
// } else {
// joke = data.joke
// }
// console.log(joke)
// Text-To-Speech
tellMe(joke)
// Disable button
toogleButton()
} catch (error) {
// Catch errors here
console.log('Whooops', error)
}
}
// Event Listeners
button.addEventListener('click', getJokes)
audioElement.addEventListener('ended', toogleButton)