chat.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package chatbot
  2. //Licensed under the Apache License, Version 2.0 (the "License");
  3. //you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. //
  6. //http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. //Unless required by applicable law or agreed to in writing, software
  9. //distributed under the License is distributed on an "AS IS" BASIS,
  10. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. //See the License for the specific language governing permissions and
  12. //limitations under the License.
  13. //
  14. // Code generated by Alibaba Cloud SDK Code Generator.
  15. // Changes may cause incorrect behavior and will be lost if the code is regenerated.
  16. import (
  17. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  19. )
  20. func (client *Client) Chat(request *ChatRequest) (response *ChatResponse, err error) {
  21. response = CreateChatResponse()
  22. err = client.DoAction(request, response)
  23. return
  24. }
  25. func (client *Client) ChatWithChan(request *ChatRequest) (<-chan *ChatResponse, <-chan error) {
  26. responseChan := make(chan *ChatResponse, 1)
  27. errChan := make(chan error, 1)
  28. err := client.AddAsyncTask(func() {
  29. defer close(responseChan)
  30. defer close(errChan)
  31. response, err := client.Chat(request)
  32. responseChan <- response
  33. errChan <- err
  34. })
  35. if err != nil {
  36. errChan <- err
  37. close(responseChan)
  38. close(errChan)
  39. }
  40. return responseChan, errChan
  41. }
  42. func (client *Client) ChatWithCallback(request *ChatRequest, callback func(response *ChatResponse, err error)) <-chan int {
  43. result := make(chan int, 1)
  44. err := client.AddAsyncTask(func() {
  45. var response *ChatResponse
  46. var err error
  47. defer close(result)
  48. response, err = client.Chat(request)
  49. callback(response, err)
  50. result <- 1
  51. })
  52. if err != nil {
  53. defer close(result)
  54. callback(nil, err)
  55. result <- 0
  56. }
  57. return result
  58. }
  59. type ChatRequest struct {
  60. *requests.RpcRequest
  61. SenderId string `position:"Query" name:"SenderId"`
  62. Tag string `position:"Query" name:"Tag"`
  63. KnowledgeId string `position:"Query" name:"KnowledgeId"`
  64. Utterance string `position:"Query" name:"Utterance"`
  65. SenderNick string `position:"Query" name:"SenderNick"`
  66. SessionId string `position:"Query" name:"SessionId"`
  67. InstanceId string `position:"Query" name:"InstanceId"`
  68. }
  69. type ChatResponse struct {
  70. *responses.BaseResponse
  71. RequestId string `json:"RequestId" xml:"RequestId"`
  72. SessionId string `json:"SessionId" xml:"SessionId"`
  73. MessageId string `json:"MessageId" xml:"MessageId"`
  74. Tag string `json:"Tag" xml:"Tag"`
  75. Messages []struct {
  76. Type string `json:"Type" xml:"Type"`
  77. Text struct {
  78. Content string `json:"Content" xml:"Content"`
  79. AnswerSource string `json:"AnswerSource" xml:"AnswerSource"`
  80. } `json:"Text" xml:"Text"`
  81. Knowledge struct {
  82. Id string `json:"Id" xml:"Id"`
  83. Title string `json:"Title" xml:"Title"`
  84. Summary string `json:"Summary" xml:"Summary"`
  85. Content string `json:"Content" xml:"Content"`
  86. AnswerSource string `json:"AnswerSource" xml:"AnswerSource"`
  87. } `json:"Knowledge" xml:"Knowledge"`
  88. Recommends []struct {
  89. KnowledgeId string `json:"KnowledgeId" xml:"KnowledgeId"`
  90. Title string `json:"Title" xml:"Title"`
  91. AnswerSource string `json:"AnswerSource" xml:"AnswerSource"`
  92. } `json:"Recommends" xml:"Recommends"`
  93. } `json:"Messages" xml:"Messages"`
  94. }
  95. func CreateChatRequest() (request *ChatRequest) {
  96. request = &ChatRequest{
  97. RpcRequest: &requests.RpcRequest{},
  98. }
  99. request.InitWithApiInfo("Chatbot", "2017-10-11", "Chat", "", "")
  100. return
  101. }
  102. func CreateChatResponse() (response *ChatResponse) {
  103. response = &ChatResponse{
  104. BaseResponse: &responses.BaseResponse{},
  105. }
  106. return
  107. }