-
Notifications
You must be signed in to change notification settings - Fork 9
/
query.go
145 lines (126 loc) · 3.92 KB
/
query.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package apiai
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type Platform struct {
Source string `json:"source"`
Data map[string]string `json:"data"`
}
type Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type Event struct {
Name string `json:"name"`
Data map[string]string `json:"data"`
}
type Query struct {
Query []string `json:"query"`
Event Event `json:"event"`
Version string `json:"-"`
SessionId string `json:"sessionId"`
Language string `json:"lang"`
Contexts []Context `json:"contexts"`
ResetContexts bool `json:"resetContexts"`
Entities []EntityDescription `json:"entities"`
Timezone string `json:"timezone"`
Location Location `json:"location"`
OriginalRequest Platform `json:"originalRequest"`
}
type CreationResponse struct {
Id string `json:"id"`
Status Status `json:"status"`
}
type CardButton struct {
Text string
Postback string
}
type Metadata struct {
IntentId string `json:"intentId"`
WebhookUsed string `json:"webhookUsed"`
WebhookForSlotFillingUsed string `json:"webhookForSlotFillingUsed"`
IntentName string `json:"intentName"`
}
type Message struct {
Type interface{} `json:"type"`
Speech string `json:"speech"`
ImageUrl string `json:"imageUrl"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Buttons []CardButton `json:"buttons"`
Replies []string `json:"replies"`
Payload interface{} `json:"payload"`
}
type Fulfilment struct {
Speech string `json:"speech"`
Messages []Message `json:"messages"`
}
type Status struct {
Code int `json:"code"`
ErrorType string `json:"errorType"`
ErrorId string `json:"errorId"`
ErrorDetails string `json:"errorDetails"`
}
type Result struct {
Source string `json:"source"`
ResolvedQuery string `json:"resolvedQuery"`
Action string `json:"action"`
ActionIncomplete bool `json:"actionIncomplete"`
Params map[string]interface{} `json:"parameters"`
Contexts []Context `json:"contexts"`
Fulfillment Fulfilment `json:"fulfillment"`
Score float64 `json:"score"`
Metadata Metadata `json:"metadata"`
}
type QueryResponse struct {
Id string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Language string `json:"lang"`
Result Result `json:"result"`
Status Status `json:"status"`
SessionId string `json:"sessionId"`
}
func (c *ApiClient) Query(q Query) (*QueryResponse, error) {
q.Version = c.config.Version
q.Language = c.config.QueryLang
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(q)
if err != nil {
return nil, fmt.Errorf("apiai: error on request, %v", err)
}
req, err := http.NewRequest("POST", c.buildUrl("query", nil), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-type", "application/json, charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.config.Token)
httpClient := http.DefaultClient
if len(c.config.ProxyURL) > 0 {
url, err := url.Parse(c.config.ProxyURL)
if err == nil {
httpClient.Transport = &http.Transport{Proxy: http.ProxyURL(url)}
}
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var response *QueryResponse
switch resp.StatusCode {
case http.StatusOK:
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&response)
if err != nil {
return nil, err
}
return response, nil
default:
return nil, fmt.Errorf("apiai: wops something happens because status code is %v", resp.StatusCode)
}
}