-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialogflow.go
247 lines (212 loc) · 5.72 KB
/
dialogflow.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
var projectID = "PROJECT_ID"
var baseURL = "https://dialogflow.googleapis.com/v2/projects/" + projectID + "/agent"
func NewDialogflowClient() (*http.Client, error) {
var scope = "https://www.googleapis.com/auth/dialogflow"
data, err := ioutil.ReadFile("keys.json")
if err != nil {
log.Fatal(err)
}
conf, err := google.JWTConfigFromJSON(data, scope)
if err != nil {
log.Fatal(err)
}
//ts := conf.TokenSource(oauth2.NoContext)
DialogflowClient := conf.Client(oauth2.NoContext)
return DialogflowClient, nil
}
func getAgent(client *http.Client) {
type Agent struct {
Parent string `json:"parent"`
DisplayName string `json:"displayName"`
DefaultLanguageCode string `json:"defaultLanguageCode"`
TimeZone string `json:"timeZone"`
EnableLogging bool `json:"enableLogging"`
MatchMode string `json:"matchMode"`
ClassificationThreshold float64 `json:"classificationThreshold"`
APIVersion string `json:"apiVersion"`
Tier string `json:"tier"`
}
resp, err := client.Get(baseURL)
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
agent := &Agent{}
json.Unmarshal(body, agent)
fmt.Println(agent)
defer resp.Body.Close()
}
func listIntents(client *http.Client) {
type Intent struct {
Intents []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Priority int `json:"priority"`
WebhookState string `json:"webhookState,omitempty"`
Parameters []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Value string `json:"value"`
EntityTypeDisplayName string `json:"entityTypeDisplayName"`
Mandatory bool `json:"mandatory"`
} `json:"parameters,omitempty"`
Messages []struct {
Text struct {
} `json:"text"`
} `json:"messages,omitempty"`
DefaultResponsePlatforms []string `json:"defaultResponsePlatforms,omitempty"`
Events []string `json:"events,omitempty"`
Action string `json:"action,omitempty"`
IsFallback bool `json:"isFallback,omitempty"`
} `json:"intents"`
}
url := baseURL + "/intents"
resp, err := client.Get(url)
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
intents := &Intent{}
json.Unmarshal(body, intents)
fmt.Println(intents)
defer resp.Body.Close()
}
func listEntities(client *http.Client) {
type Entity struct {
EntityTypes []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Kind string `json:"kind"`
Entities []struct {
Value string `json:"value"`
Synonyms []string `json:"synonyms"`
} `json:"entities,omitempty"`
} `json:"entityTypes"`
}
url := baseURL + "/entityTypes"
resp, err := client.Get(url)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
body, _ := ioutil.ReadAll(resp.Body)
entities := &Entity{}
json.Unmarshal(body, entities)
fmt.Println(entities)
defer resp.Body.Close()
}
func createIntent(client *http.Client, name string) {
url := baseURL + "/intents"
var jsonbody = []byte(`{
"displayName": "` + name + `",
"webhookState": "WEBHOOK_STATE_ENABLED",
"priority": 500000,
"events": [
"asdfghjkssdf"
],
"defaultResponsePlatforms": [
"ACTIONS_ON_GOOGLE"
],
"trainingPhrases": [
{
"type": "TYPE_UNSPECIFIED",
"parts": [
{
"text": "yeet1"
}
]
}
],
"parameters": [
{
"displayName": "yeetyeet",
"value": "yeet yeet yeet",
"entityTypeDisplayName": "yeet",
"mandatory": true,
"prompts": [
"What do you want?"
],
"isList": false
}
]
}`)
resp, err := client.Post(url, "application/json", bytes.NewReader(jsonbody))
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
defer resp.Body.Close()
}
func createEntity(client *http.Client, name string) {
url := baseURL + "/entityTypes"
var jsonbody = []byte(`{
"displayName": "` + name + `",
"kind": "KIND_MAP"
}`)
resp, err := client.Post(url, "application/json", bytes.NewReader(jsonbody))
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
defer resp.Body.Close()
}
func deleteIntent(client *http.Client, intentID string) {
url := baseURL + "/intents/" + intentID
req, err := http.NewRequest("DELETE", url, nil)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return
}
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Non-OK HTTP status:", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(body)
defer resp.Body.Close()
}
func deleteEntity(client *http.Client, entityID string) {
url := baseURL + "/entityTypes/" + entityID
req, err := http.NewRequest("DELETE", url, nil)
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
return
}
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
fmt.Println("Non-OK HTTP status:", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
fmt.Println("congratulations! success!")
defer resp.Body.Close()
}
func main() {
client, err := NewDialogflowClient()
if err != nil {
log.Fatal(err)
}
//getAgent(client)
listIntents(client)
listEntities(client)
createIntent(client, "apitest12345678")
createEntity(client, "yeet123123245")
deleteIntent(client, IntentID)
deleteEntity(client, EntityID)
}