github.com/wit-ai/wit-go/v2@v2.0.2/message.go (about) 1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 3 package witai 4 5 import ( 6 "encoding/json" 7 "errors" 8 "fmt" 9 "io" 10 "net/http" 11 "net/url" 12 ) 13 14 // MessageResponse - https://wit.ai/docs/http/20200513/#get__message_link 15 type MessageResponse struct { 16 ID string `json:"msg_id"` 17 Text string `json:"text"` 18 Intents []MessageIntent `json:"intents"` 19 Entities map[string][]MessageEntity `json:"entities"` 20 Traits map[string][]MessageTrait `json:"traits"` 21 } 22 23 // MessageEntity - https://wit.ai/docs/http/20200513/#get__message_link 24 type MessageEntity struct { 25 ID string `json:"id"` 26 Name string `json:"name"` 27 Role string `json:"role"` 28 Start int `json:"start"` 29 End int `json:"end"` 30 Body string `json:"body"` 31 Value string `json:"value"` 32 Confidence float64 `json:"confidence"` 33 Entities []MessageEntity `json:"entities"` 34 Extra map[string]interface{} `json:"-"` 35 } 36 37 // MessageTrait - https://wit.ai/docs/http/20200513/#get__message_link 38 type MessageTrait struct { 39 ID string `json:"id"` 40 Value string `json:"value"` 41 Confidence float64 `json:"confidence"` 42 Extra map[string]interface{} `json:"-"` 43 } 44 45 // MessageIntent - https://wit.ai/docs/http/20200513/#get__message_link 46 type MessageIntent struct { 47 ID string `json:"id"` 48 Name string `json:"name"` 49 Confidence float64 `json:"confidence"` 50 } 51 52 // MessageRequest - https://wit.ai/docs/http/20200513/#get__message_link 53 type MessageRequest struct { 54 Query string `json:"q"` 55 Tag string `json:"tag"` 56 N int `json:"n"` 57 Context *MessageContext `json:"context"` 58 Speech *Speech `json:"-"` 59 } 60 61 // Speech - https://wit.ai/docs/http/20170307#post__speech_link 62 type Speech struct { 63 File io.Reader `json:"file"` 64 ContentType string `json:"content_type"` // Example: audio/raw;encoding=unsigned-integer;bits=16;rate=8000;endian=big 65 } 66 67 // MessageContext - https://wit.ai/docs/http/20170307#context_link 68 type MessageContext struct { 69 ReferenceTime string `json:"reference_time"` // "2014-10-30T12:18:45-07:00" 70 Timezone string `json:"timezone"` 71 Locale string `json:"locale"` 72 Coords MessageCoords `json:"coords"` 73 } 74 75 // MessageCoords - https://wit.ai/docs/http/20170307#context_link 76 type MessageCoords struct { 77 Lat float32 `json:"lat"` 78 Long float32 `json:"long"` 79 } 80 81 // Parse - parses text and returns entities 82 func (c *Client) Parse(req *MessageRequest) (*MessageResponse, error) { 83 if req == nil { 84 return nil, errors.New("invalid request") 85 } 86 87 q := buildParseQuery(req) 88 89 resp, err := c.request(http.MethodGet, "/message"+q, "application/json", nil) 90 if err != nil { 91 return nil, err 92 } 93 94 defer resp.Close() 95 96 var msgResp *MessageResponse 97 decoder := json.NewDecoder(resp) 98 err = decoder.Decode(&msgResp) 99 return msgResp, err 100 } 101 102 // Speech - sends audio file for parsing 103 func (c *Client) Speech(req *MessageRequest) (*MessageResponse, error) { 104 if req == nil || req.Speech == nil { 105 return nil, errors.New("invalid request") 106 } 107 108 q := buildParseQuery(req) 109 110 resp, err := c.request(http.MethodPost, "/speech"+q, req.Speech.ContentType, req.Speech.File) 111 if err != nil { 112 return nil, err 113 } 114 115 defer resp.Close() 116 117 var msgResp *MessageResponse 118 decoder := json.NewDecoder(resp) 119 err = decoder.Decode(&msgResp) 120 return msgResp, err 121 } 122 123 func buildParseQuery(req *MessageRequest) string { 124 q := fmt.Sprintf("?q=%s", url.PathEscape(req.Query)) 125 if req.N != 0 { 126 q += fmt.Sprintf("&n=%d", req.N) 127 } 128 if req.Tag != "" { 129 q += fmt.Sprintf("&tag=%s", req.Tag) 130 } 131 if req.Context != nil { 132 b, _ := json.Marshal(req.Context) 133 if b != nil { 134 q += fmt.Sprintf("&context=%s", url.PathEscape(string(b))) 135 } 136 } 137 138 return q 139 }