-
Notifications
You must be signed in to change notification settings - Fork 9
/
tts.go
45 lines (38 loc) · 819 Bytes
/
tts.go
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
package apiai
import (
"hash/fnv"
"io"
"net/http"
"os"
"strconv"
)
func (c *ApiClient) Tts(text string) (string, error) {
req, err := http.NewRequest("GET", c.buildUrl("tts", map[string]string{
"text": text,
}), nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Bearer "+c.config.Token)
req.Header.Set("Accept-Language", c.config.SpeechLang)
httpClient := http.DefaultClient
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
filePath := "/tmp/" + hash(text)
file, err := os.Create(filePath)
if err != nil {
return "", err
}
defer file.Close()
io.Copy(file, resp.Body)
return filePath, nil
}
func hash(s string) string {
h := fnv.New64a()
h.Write([]byte(s))
hValue := strconv.FormatUint(h.Sum64(), 10)
return hValue
}