github.com/wit-ai/wit-go/v2@v2.0.2/intent.go (about) 1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 3 package witai 4 5 import ( 6 "bytes" 7 "encoding/json" 8 "fmt" 9 "net/http" 10 "net/url" 11 ) 12 13 // Intent - represents a wit-ai intent. 14 // 15 // https://wit.ai/docs/http/20200513/#get__intents_link 16 type Intent struct { 17 ID string `json:"id"` 18 Name string `json:"name"` 19 Entities []Entity `json:"entities,omitempty"` 20 } 21 22 // GetIntents - returns the list of intents. 23 // 24 // https://wit.ai/docs/http/20200513/#get__intents_link 25 func (c *Client) GetIntents() ([]Intent, error) { 26 resp, err := c.request(http.MethodGet, "/intents", "application/json", nil) 27 if err != nil { 28 return []Intent{}, err 29 } 30 31 defer resp.Close() 32 33 var intents []Intent 34 err = json.NewDecoder(resp).Decode(&intents) 35 return intents, err 36 } 37 38 // CreateIntent - creates a new intent with the given name. 39 // 40 // https://wit.ai/docs/http/20200513/#post__intents_link 41 func (c *Client) CreateIntent(name string) (*Intent, error) { 42 intentJSON, err := json.Marshal(Intent{Name: name}) 43 if err != nil { 44 return nil, err 45 } 46 47 resp, err := c.request(http.MethodPost, "/intents", "application/json", bytes.NewBuffer(intentJSON)) 48 if err != nil { 49 return nil, err 50 } 51 52 defer resp.Close() 53 54 var intentResp *Intent 55 err = json.NewDecoder(resp).Decode(&intentResp) 56 return intentResp, err 57 } 58 59 // GetIntent - returns intent by name. 60 // 61 // https://wit.ai/docs/http/20200513/#get__intents__intent_link 62 func (c *Client) GetIntent(name string) (*Intent, error) { 63 resp, err := c.request(http.MethodGet, fmt.Sprintf("/intents/%s", url.PathEscape(name)), "application/json", nil) 64 if err != nil { 65 return nil, err 66 } 67 68 defer resp.Close() 69 70 var intent *Intent 71 err = json.NewDecoder(resp).Decode(&intent) 72 return intent, err 73 } 74 75 // DeleteIntent - permanently deletes an intent by name. 76 // 77 // https://wit.ai/docs/http/20200513/#delete__intents__intent_link 78 func (c *Client) DeleteIntent(name string) error { 79 resp, err := c.request(http.MethodDelete, fmt.Sprintf("/intents/%s", url.PathEscape(name)), "application/json", nil) 80 if err == nil { 81 resp.Close() 82 } 83 84 return err 85 }