response.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package dialog
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type apiResponse struct {
  7. RequestID string `json:"request_id"`
  8. Code int64 `json:"code"`
  9. Msg string `json:"msg"`
  10. Data json.RawMessage `json:"data"`
  11. }
  12. // APIError 智能对话接口错误.
  13. type APIError struct {
  14. APIName string
  15. Code int64
  16. Msg string
  17. RequestID string
  18. Data json.RawMessage
  19. }
  20. func (e *APIError) Error() string {
  21. return fmt.Sprintf("%s Error , code=%d , msg=%s , request_id=%s", e.APIName, e.Code, e.Msg, e.RequestID)
  22. }
  23. type accessTokenData struct {
  24. AccessToken string `json:"access_token"`
  25. }
  26. // ImportJSONResponse 简单问答导入响应.
  27. // 官方文档: https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/import.html
  28. type ImportJSONResponse struct {
  29. RequestID string `json:"-"`
  30. TaskID string `json:"task_id"`
  31. }
  32. // PublishResponse 发布响应.
  33. // 官方文档: https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/publish.html
  34. type PublishResponse struct {
  35. RequestID string `json:"-"`
  36. TaskID string `json:"task_id"`
  37. }
  38. // EffectiveProgressResponse 发布进度查询响应.
  39. // 官方文档: https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/progress.html
  40. type EffectiveProgressResponse struct {
  41. RequestID string `json:"-"`
  42. EndTime string `json:"end_time"`
  43. Progress int `json:"progress"`
  44. Status int `json:"status"`
  45. }
  46. // FetchAsyncResponse 异步任务查询响应.
  47. // 官方文档: https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/fetch.html
  48. type FetchAsyncResponse struct {
  49. RequestID string `json:"-"`
  50. State int `json:"state"`
  51. Msg string `json:"msg"`
  52. Progress int `json:"progress"`
  53. Start int64 `json:"start"`
  54. End int64 `json:"end"`
  55. URL string `json:"url"`
  56. TotalCount int `json:"total_count"`
  57. SuccessCount int `json:"success_count"`
  58. FailCount int `json:"fail_count"`
  59. ReplaceCount int `json:"replace_count"`
  60. SuccessUploadCount int `json:"success_upload_count"`
  61. SuccessSkillInfo []SkillInfo `json:"success_skill_info"`
  62. FailSkillInfo []SkillInfo `json:"fail_skill_info"`
  63. }
  64. // SkillInfo 技能信息.
  65. type SkillInfo struct {
  66. ID int64 `json:"id"`
  67. Name string `json:"name"`
  68. Intents []IntentInfo `json:"intents"`
  69. }
  70. // IntentInfo 意图信息.
  71. type IntentInfo struct {
  72. ID int64 `json:"id"`
  73. Name string `json:"name"`
  74. }
  75. // QueryResponse 调用智能对话响应.
  76. // 官方文档: https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/query.html
  77. type QueryResponse struct {
  78. RequestID string `json:"-"`
  79. Answer string `json:"answer"`
  80. RawAnswer json.RawMessage `json:"-"`
  81. AnswerType string `json:"answer_type"`
  82. SkillName string `json:"skill_name"`
  83. IntentName string `json:"intent_name"`
  84. MsgID string `json:"msg_id"`
  85. Options []Option `json:"options"`
  86. Status string `json:"status"`
  87. Slots []SlotDetail `json:"slots"`
  88. }
  89. // Option 推荐问题.
  90. type Option struct {
  91. AnsNodeName string `json:"ans_node_name"`
  92. Title string `json:"title"`
  93. Answer string `json:"answer"`
  94. Confidence float64 `json:"confidence"`
  95. }
  96. // SlotDetail 槽位数据.
  97. type SlotDetail struct {
  98. Name string `json:"name"`
  99. Value string `json:"value"`
  100. Norm string `json:"norm"`
  101. }
  102. func decodeResponse(response []byte, data interface{}, apiName string) (string, error) {
  103. var res apiResponse
  104. if err := json.Unmarshal(response, &res); err != nil {
  105. return "", fmt.Errorf("json Unmarshal Error, err=%v", err)
  106. }
  107. if res.Code != 0 {
  108. return "", &APIError{
  109. APIName: apiName,
  110. Code: res.Code,
  111. Msg: res.Msg,
  112. RequestID: res.RequestID,
  113. Data: res.Data,
  114. }
  115. }
  116. if data != nil && len(res.Data) > 0 && string(res.Data) != "null" {
  117. if err := json.Unmarshal(res.Data, data); err != nil {
  118. return "", fmt.Errorf("json Unmarshal Data Error, err=%v", err)
  119. }
  120. }
  121. return res.RequestID, nil
  122. }