api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dialog
  2. import (
  3. stdcontext "context"
  4. "encoding/json"
  5. "github.com/silenceper/wechat/v2/aispeech/encryptor"
  6. )
  7. // GetAccessToken 获取 access token.
  8. func (d *Dialog) GetAccessToken() (string, error) {
  9. return d.Context.GetAccessToken()
  10. }
  11. // GetAccessTokenContext 获取 access token.
  12. func (d *Dialog) GetAccessTokenContext(ctx stdcontext.Context) (string, error) {
  13. return d.Context.GetAccessTokenContext(ctx)
  14. }
  15. // ImportJSON 简单问答导入.
  16. func (d *Dialog) ImportJSON(req *ImportJSONRequest) (*ImportJSONResponse, error) {
  17. return d.ImportJSONContext(stdcontext.Background(), req)
  18. }
  19. // ImportJSONContext 简单问答导入.
  20. func (d *Dialog) ImportJSONContext(ctx stdcontext.Context, req *ImportJSONRequest) (*ImportJSONResponse, error) {
  21. var res ImportJSONResponse
  22. requestID, err := d.postJSON(ctx, importJSONPath, req, &res, "AISpeechImportJSON")
  23. res.RequestID = requestID
  24. return &res, err
  25. }
  26. // Publish 发布机器人.
  27. func (d *Dialog) Publish() (*PublishResponse, error) {
  28. return d.PublishContext(stdcontext.Background())
  29. }
  30. // PublishContext 发布机器人.
  31. func (d *Dialog) PublishContext(ctx stdcontext.Context) (*PublishResponse, error) {
  32. var res PublishResponse
  33. requestID, err := d.postEmpty(ctx, publishPath, &res, "AISpeechPublish")
  34. res.RequestID = requestID
  35. return &res, err
  36. }
  37. // GetEffectiveProgress 查询机器人发布进度.
  38. func (d *Dialog) GetEffectiveProgress(req *EffectiveProgressRequest) (*EffectiveProgressResponse, error) {
  39. return d.GetEffectiveProgressContext(stdcontext.Background(), req)
  40. }
  41. // GetEffectiveProgressContext 查询机器人发布进度.
  42. func (d *Dialog) GetEffectiveProgressContext(ctx stdcontext.Context, req *EffectiveProgressRequest) (*EffectiveProgressResponse, error) {
  43. var res EffectiveProgressResponse
  44. requestID, err := d.postJSON(ctx, effectiveProgressPath, req, &res, "AISpeechGetEffectiveProgress")
  45. res.RequestID = requestID
  46. return &res, err
  47. }
  48. // FetchAsync 查询异步任务.
  49. func (d *Dialog) FetchAsync(req *FetchAsyncRequest) (*FetchAsyncResponse, error) {
  50. return d.FetchAsyncContext(stdcontext.Background(), req)
  51. }
  52. // FetchAsyncContext 查询异步任务.
  53. func (d *Dialog) FetchAsyncContext(ctx stdcontext.Context, req *FetchAsyncRequest) (*FetchAsyncResponse, error) {
  54. var res FetchAsyncResponse
  55. requestID, err := d.postJSON(ctx, fetchAsyncPath, req, &res, "AISpeechFetchAsync")
  56. res.RequestID = requestID
  57. return &res, err
  58. }
  59. // Query 调用智能对话.
  60. func (d *Dialog) Query(req *QueryRequest) (*QueryResponse, error) {
  61. return d.QueryContext(stdcontext.Background(), req)
  62. }
  63. // QueryContext 调用智能对话.
  64. func (d *Dialog) QueryContext(ctx stdcontext.Context, req *QueryRequest) (*QueryResponse, error) {
  65. body, err := json.Marshal(req)
  66. if err != nil {
  67. return nil, err
  68. }
  69. encryptedBody, err := encryptor.Encrypt(d.AESKey, body)
  70. if err != nil {
  71. return nil, err
  72. }
  73. accessToken, err := d.GetAccessTokenContext(ctx)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if accessToken == "" {
  78. return nil, errEmptyAccessToken
  79. }
  80. response, err := post(ctx, d.Config, queryPath, []byte(encryptedBody), "text/plain", accessToken, "")
  81. if err != nil {
  82. return nil, err
  83. }
  84. plainResponse := response
  85. if !json.Valid(response) {
  86. plainResponse, err = encryptor.Decrypt(d.AESKey, string(response))
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. var res QueryResponse
  92. requestID, err := decodeResponse(plainResponse, &res, "AISpeechQuery")
  93. if err != nil {
  94. return nil, err
  95. }
  96. res.RequestID = requestID
  97. if json.Valid([]byte(res.Answer)) {
  98. res.RawAnswer = json.RawMessage(res.Answer)
  99. }
  100. return &res, nil
  101. }