-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
122 lines (103 loc) · 2.58 KB
/
main.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
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
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path"
"path/filepath"
dialogflow "github.com/_/pythia/dialogflow/detect_intent"
"github.com/gin-gonic/gin"
)
func TempFileName(prefix, suffix string) string {
randBytes := make([]byte, 32)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix)
}
func main() {
router := gin.Default()
router.GET("/health-check", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "healthy",
})
})
router.GET("/text", func(c *gin.Context) {
text := c.Query("text")
if len(text) <= 1 {
c.JSON(400, gin.H{
"message": "?text= is too small",
"error": "invalid_text",
})
return
}
if len(text) > 1000 {
c.JSON(400, gin.H{
"message": "?text= is too big",
"error": "invalid_text",
})
return
}
res, err := dialogflow.DetectIntentText(os.Getenv("GOOGLE_CLOUD_PROJECT_NAME"), "web_search_session", text, "pt-BR")
if err != nil {
fmt.Println(err)
fmt.Printf("Failed to detect intent %s", err.Error())
c.JSON(500, gin.H{
"error": "failed to process request.",
})
return
}
fmt.Println(res)
c.JSON(200, gin.H{
"dialogflow_result": res,
})
})
router.POST("/audio", func(c *gin.Context) {
file, err := c.FormFile("audio")
if err != nil {
fmt.Printf("Failed to get FormFile %s", err.Error())
c.JSON(400, gin.H{
"message": "Não conseguimos abrir o arquivo de audio",
"error": "invalid_file",
})
return
}
if file == nil {
fmt.Printf("Missing FormFile")
c.JSON(400, gin.H{
"message": "Não conseguimos encontrar o arquivo de audio",
"error": "missing_file",
})
return
}
tmpExt := path.Ext(file.Filename)
if tmpExt == "" {
c.JSON(400, gin.H{
"message": "Não conseguimos encontrar a extensão do arquivo de audio",
"error": "invalid_file_ext",
})
return
}
filename := TempFileName("audio-upload", tmpExt)
fmt.Printf("saving %s into %s\n", file.Filename, filename)
c.SaveUploadedFile(file, filename)
defer func() {
e := os.Remove(filename)
if e != nil {
fmt.Printf("failed to remove %s: %s", filename, e.Error())
}
}()
res, err := dialogflow.DetectIntentAudio(os.Getenv("GOOGLE_CLOUD_PROJECT_NAME"), "web_search_session", filename, "pt-BR")
if err != nil {
fmt.Printf("Failed to detect intent %s", err.Error())
c.JSON(500, gin.H{
"message": "Aconteceu um erro ao consultar o serviço DialogFlow",
"error": "dialog_flow_error",
})
return
}
c.JSON(200, gin.H{
"dialogflow_result": res,
})
})
router.Run()
}