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