-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeech.ts
52 lines (46 loc) · 1.66 KB
/
speech.ts
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
import { resolve } from "path/posix"
const request = require('request')
const fetch = require('node-fetch')
function wait() {
return new Promise(resolve => resolve(20))
}
function getAudioUrl(
key: string,
secretKey: string,
carachter: string,
text: string
) {
if (carachter === undefined) throw new Error('Define the carachter voice.')
if (key === undefined) throw new Error('Define the key you got from uberduck')
if (carachter === undefined) throw new Error('Define the secret key u got from uberduck.')
return new Promise(async (resolve, reject) => {
await request({
url: 'https://api.uberduck.ai/speak',
method: 'POST',
body: `{"speech": "${text}","voice": "${carachter}"}`,
auth: {
'user': key,
'pass': secretKey
}
}, async (erro, response, body) => {
if (erro) throw new Error('Error when making request, verify if yours params (key, secretKey, character) are correct.')
const audioResponse: string = 'https://api.uberduck.ai/speak-status?uuid=' + JSON.parse(body).uuid
let jsonResponse: any = false
async function getJson(url) {
let jsonResult: any = undefined
await fetch(url)
.then(res => res.json())
.then(json => {
jsonResult = json
})
return jsonResult
}
jsonResponse = await getJson(audioResponse)
while (jsonResponse.path === null) jsonResponse = await getJson(audioResponse)
resolve(jsonResponse.path)
})
})
}
export {
getAudioUrl
}