feat: return full alternatives

This commit is contained in:
Vincent Young 2023-03-16 20:23:21 +08:00
parent a5b256d7ad
commit db722920cd
No known key found for this signature in database
GPG Key ID: 84A0830C90354A56
3 changed files with 13 additions and 3 deletions

1
go.mod
View File

@ -10,6 +10,7 @@ require (
)
require (
github.com/Jeffail/gabs/v2 v2.7.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/Jeffail/gabs/v2 v2.7.0 h1:Y2edYaTcE8ZpRsR2AtmPu5xQdFDIthFG0jYhu5PY8kg=
github.com/Jeffail/gabs/v2 v2.7.0/go.mod h1:dp5ocw1FvBBQYssgHsG7I1WYsiLRtkUaB1FEtSwvNUw=
github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4=
github.com/abadojack/whatlanggo v1.0.1/go.mod h1:66WiQbSbJBIlOZMsvbKe5m6pzQovxCH9B/K8tQB2uoc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

13
main.go
View File

@ -193,6 +193,7 @@ func main() {
body, _ := io.ReadAll(resp.Body)
res := gjson.ParseBytes(body)
// display response
// fmt.Println(res)
if res.Get("error.code").String() == "-32600" {
@ -210,10 +211,16 @@ func main() {
"message": "Too Many Requests",
})
} else {
var alternatives []string
res.Get("result.texts.0.alternatives").ForEach(func(key, value gjson.Result) bool {
alternatives = append(alternatives, value.Get("text").String())
return true
})
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": id,
"data": res.Get("result.texts.0.text").String(),
"code": http.StatusOK,
"id": id,
"data": res.Get("result.texts.0.text").String(),
"alternatives": alternatives,
})
}
}