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
| const ( USERID = "123456" APIKEY = "11337ff965a546b1ae22576f160f1a08" URL = "http://openapi.tuling123.com/openapi/api/v2" )
type Request struct { ReqType int `json:"reqType"` Perception map[string]interface{} `json:"perception"` UserInfo map[string]string `json:"userInfo"` }
type Result struct { ResultType string `json:"resultType"` Values map[string]interface{} `json:"values"` GroupType int `json:"groupType"` }
type Response struct { Intent map[string]interface{} `json:"intent"` Results []Result }
func NewRobot() *Request { userInfo := map[string]string{ "apiKey": APIKEY, "userId": USERID, }
return &Request{ ReqType: 0, Perception: nil, UserInfo: userInfo, } }
func (r *Request) Chat(msg string) ([]interface{}, error) { inputText := map[string]string{ "text": msg, }
r.Perception = map[string]interface{}{ "inputText": inputText, }
jsonData, err := json.Marshal(r) if err != nil { return nil, err }
return r.Post(jsonData) }
func (r *Request) Post(data []byte) ([]interface{}, error) { body := bytes.NewBuffer(data) req, err := http.NewRequest("POST", URL, body) if err != nil { return nil, err }
req.Header.Add("Accept", "application/json") req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err }
var respData Response err = json.Unmarshal(respBody, &respData) if err != nil { return nil, err }
var results []interface{} for _, v := range respData.Results { for _, val := range v.Values { results = append(results, val) } }
return results, nil }
|