Skip to content

Commit 53cf262

Browse files
Merge pull request #407 from watson-developer-cloud/conversation-v1
Add Conversation v1
2 parents b27b782 + a99ad01 commit 53cf262

File tree

9 files changed

+890
-2
lines changed

9 files changed

+890
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ System.out.println(result);
250250
```
251251

252252
### Conversation
253-
Use the experimental [Conversation][conversation] service to identify intents, entities, and conduct conversations.
253+
Use the [Conversation][conversation] service to identify intents, entities, and conduct conversations.
254254

255255
```java
256-
ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_05_19);
256+
ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_07_11);
257257
service.setUsernameAndPassword("<username>", "<password>");
258258

259259
MessageRequest newMessage = new MessageRequest.Builder().inputText("Hi").build();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. 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 distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.ibm.watson.developer_cloud.conversation.v1;
15+
16+
import java.util.Map;
17+
18+
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;
19+
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;
20+
import com.ibm.watson.developer_cloud.http.ServiceCallback;
21+
22+
import jersey.repackaged.jsr166e.CompletableFuture;
23+
24+
/**
25+
* Example of how to call the {@link ConversationService#message(String, MessageRequest)} method
26+
* synchronous, asynchronous, and using react.
27+
* @version v1-experimental
28+
*/
29+
public class ConversationExample {
30+
31+
public static void main(String[] args) throws Exception {
32+
ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_07_11);
33+
service.setUsernameAndPassword("<username>", "<password>");
34+
35+
// sync
36+
MessageRequest newMessage = new MessageRequest.Builder().inputText("Hi").build();
37+
MessageResponse response = service.message("<workspace-id>", newMessage).execute();
38+
System.out.println(response);
39+
40+
41+
// async
42+
service.message("<workspace-id>", newMessage).enqueue(new ServiceCallback<MessageResponse>() {
43+
@Override
44+
public void onResponse(MessageResponse response) {
45+
System.out.println(response);
46+
}
47+
48+
@Override
49+
public void onFailure(Exception e) {}
50+
});
51+
52+
// rx callback
53+
service.message("<workspace-id>", newMessage).rx()
54+
.thenApply(new CompletableFuture.Fun<MessageResponse, Map<String, Object>>() {
55+
@Override
56+
public Map<String, Object> apply(MessageResponse message) {
57+
return message.getOutput();
58+
}
59+
}).thenAccept(new CompletableFuture.Action<Map<String, Object>>() {
60+
@Override
61+
public void accept(Map<String, Object> output) {
62+
System.out.println(output);
63+
}
64+
});
65+
66+
// rx async callback
67+
service.message("<workspace-id>", newMessage).rx()
68+
.thenApplyAsync(new CompletableFuture.Fun<MessageResponse, Map<String, Object>>() {
69+
@Override
70+
public Map<String, Object> apply(MessageResponse message) {
71+
return message.getOutput();
72+
}
73+
}).thenAccept(new CompletableFuture.Action<Map<String, Object>>() {
74+
@Override
75+
public void accept(Map<String, Object> output) {
76+
System.out.println(output);
77+
}
78+
});
79+
80+
// rx sync
81+
MessageResponse rxMessageResponse = service.message("<workspace-id>", newMessage).rx().get();
82+
System.out.println(rxMessageResponse);
83+
}
84+
85+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. 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 distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.ibm.watson.developer_cloud.conversation.v1;
15+
16+
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;
17+
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;
18+
import com.ibm.watson.developer_cloud.http.RequestBuilder;
19+
import com.ibm.watson.developer_cloud.http.ServiceCall;
20+
import com.ibm.watson.developer_cloud.service.WatsonService;
21+
import com.ibm.watson.developer_cloud.util.GsonSingleton;
22+
import com.ibm.watson.developer_cloud.util.ResponseConverterUtils;
23+
import com.ibm.watson.developer_cloud.util.Validator;
24+
25+
/**
26+
* Thin wrapper around the Conversation Service REST API.
27+
*
28+
* @version v1
29+
* @see <a href=
30+
* "http://www.ibm.com/watson/developercloud/conversation.html">
31+
* Conversation</a>
32+
* @api.version_date 2016-07-11
33+
*/
34+
public final class ConversationService extends WatsonService {
35+
36+
/** The Constant VERSION_DATE_2016-07-11. */
37+
public static final String VERSION_DATE_2016_07_11 = "2016-07-11";
38+
private static final String URL = "https://gateway.watsonplatform.net/conversation/api";
39+
private static final String SERVICE_NAME = "conversation";
40+
private static final String PATH_MESSAGE = "/v1/workspaces/%s/message";
41+
private static final String VERSION_PARAM = "version";
42+
private final String versionDate;
43+
44+
45+
/**
46+
* Returns an instance of the Conversation Service using the service's default endpoint (URL).
47+
*
48+
* @param versionDate Version of the API which is to be invoked by the REST client.
49+
*/
50+
public ConversationService(final String versionDate) {
51+
super(SERVICE_NAME);
52+
if (getEndPoint() == null || getEndPoint().isEmpty())
53+
setEndPoint(URL);
54+
55+
Validator.isTrue(versionDate != null && !versionDate.isEmpty(),
56+
"'version cannot be null. Use " + VERSION_DATE_2016_07_11);
57+
this.versionDate = versionDate;
58+
}
59+
60+
/**
61+
* Returns an instance of the Conversation Service using the service's default endpoint (URL), username and password.
62+
* @param versionDate Version of the API which is to be invoked by the REST client.
63+
* @param username the username
64+
* @param password the password
65+
*/
66+
public ConversationService(final String versionDate, String username, String password) {
67+
this(versionDate);
68+
setUsernameAndPassword(username, password);
69+
}
70+
71+
/**
72+
* Sends a message to the service through a {@link MessageRequest}.
73+
*
74+
* @param workspaceId the workspace id
75+
* @param request the request
76+
* @return The response for the given message.
77+
*/
78+
public ServiceCall<MessageResponse> message(String workspaceId, MessageRequest request) {
79+
Validator.isTrue(workspaceId != null && !workspaceId.isEmpty(), "'workspaceId' cannot be null or empty");
80+
Validator.notNull(request, "'request' cannot be null");
81+
Validator.isTrue(request.input() != null && !request.input().isEmpty(), "'request.input' cannot be null or empty");
82+
83+
RequestBuilder builder = RequestBuilder.post(String.format(PATH_MESSAGE, workspaceId));
84+
builder.query(VERSION_PARAM, this.versionDate);
85+
builder.bodyJson(GsonSingleton.getGson().toJsonTree(request).getAsJsonObject());
86+
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(MessageResponse.class));
87+
}
88+
89+
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. 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 distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.ibm.watson.developer_cloud.conversation.v1.model;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
import com.google.gson.annotations.SerializedName;
20+
import com.ibm.watson.developer_cloud.conversation.v1.ConversationService;
21+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
22+
23+
/**
24+
* Object that represents the input and context of a conversation. This is used by the
25+
* {@link ConversationService#message(String, MessageRequest)} method
26+
*
27+
* @see <a href="http://www.ibm.com/watson/developercloud/conversation.html"> http://www.ibm.com/
28+
* watson/developercloud/conversation.html</a>
29+
*
30+
*/
31+
public class MessageRequest extends GenericModel {
32+
33+
/**
34+
* The Class Builder.
35+
*/
36+
public static class Builder {
37+
private static final String TEXT = "text";
38+
private Map<String, Object> context;
39+
private Map<String, Object> input;
40+
private Boolean alternateIntents;
41+
42+
/**
43+
* Instantiates a new Builder.
44+
*/
45+
public Builder() {}
46+
47+
/**
48+
* Instantiates a new builder.
49+
*
50+
* @param messageRequest the message request
51+
*/
52+
private Builder(MessageRequest messageRequest) {
53+
this.input = new HashMap<String, Object>(messageRequest.input);
54+
this.context = new HashMap<String, Object>(messageRequest.context);
55+
this.alternateIntents = messageRequest.alternateIntents;
56+
}
57+
58+
/**
59+
* Generates a new {@link MessageRequest} object. It will contain the parameters set in the
60+
* builder.
61+
*
62+
* @return a {@link MessageRequest} instance
63+
*/
64+
public MessageRequest build() {
65+
return new MessageRequest(this);
66+
}
67+
68+
/**
69+
* Sets the context/state which is to be sent to the message API as a part of the service
70+
* request. Each response from the message API returns a <code>context</code> object which
71+
* represents the state as defined by the service. The state is not maintained by the service,
72+
* so the client must keep the state from each API call, and pass that state in as a part of any
73+
* subsequent requests.
74+
*
75+
* @param context a map containing key value pairs representing the state/context of the
76+
* conversation
77+
*
78+
* @return a builder object
79+
*/
80+
public Builder context(final Map<String, Object> context) {
81+
this.context = context != null ? new HashMap<String, Object>(context) : context;
82+
return this;
83+
}
84+
85+
/**
86+
* Sets the alternate intents flag. Set to true to return all matching intents
87+
*
88+
* @param alternateIntents the alternate intents flag
89+
* @return a builder object
90+
*/
91+
public Builder alternateIntents(final Boolean alternateIntents) {
92+
this.alternateIntents = alternateIntents;
93+
return this;
94+
}
95+
96+
/**
97+
* Sets the input which is to be sent to the message API as a part of the service request.
98+
* Typically the input will contain a <code>text</code> property (key and value). The
99+
* <code>text</code> property is generally interpreted as being the user/system input which the
100+
* service must parse for intents, entities etc..<br>
101+
* In advanced cases the client may pass in more than just text as a part of the service input.
102+
* For the majority of cases calling the {@link #inputText(String)} method is sufficient to send
103+
* text to the service on behalf of the user/system.
104+
*
105+
* @param input a map of properties to be sent to the service under the input property
106+
* @return a builder object
107+
*/
108+
public Builder input(Map<String, Object> input) {
109+
this.input = input != null ? new HashMap<String, Object>(input) : null;
110+
return this;
111+
}
112+
113+
/**
114+
* Sets the input text which is to be sent to the message API as a part of the service request.
115+
*
116+
* @param text the textual value to be assigned to the 'text' property on the input object.
117+
* @return a builder object
118+
*/
119+
public Builder inputText(String text) {
120+
if (input == null) {
121+
input = new HashMap<String, Object>();
122+
}
123+
input.put(TEXT, text);
124+
return this;
125+
}
126+
}
127+
128+
private Map<String, Object> context;
129+
private Map<String, Object> input;
130+
@SerializedName("alternate_intents")
131+
private Boolean alternateIntents;
132+
133+
/**
134+
* Creates a new instance of the MessageRequest for the {@link ConversationService} service.
135+
* Clients must use the {@link Builder} class to construct new instances of the class.
136+
*
137+
* @param options a builder configured with the various parameters for the request
138+
*/
139+
private MessageRequest(Builder options) {
140+
this.context = options.context;
141+
this.input = options.input;
142+
this.alternateIntents = options.alternateIntents;
143+
}
144+
145+
/**
146+
* Returns a map used to store context/state for the message API. Each response from the message
147+
* API will return a context object as a part of the payload. This context must be maintained and
148+
* passed in as a part of subsequent API calls.
149+
*
150+
* @return a map of properties
151+
*/
152+
public Map<String, Object> context() {
153+
return context;
154+
}
155+
156+
/**
157+
* Whether to return more than one intent. Set to true to return all matching intents boolean<br/>
158+
* Default: false
159+
*
160+
* @return the boolean indicating if alternate intents should be returned
161+
*/
162+
public Boolean alternateIntents() {
163+
return alternateIntents;
164+
}
165+
166+
167+
/**
168+
* Returns a map storing the input which is to be sent to the service as a part of the API
169+
* request.
170+
*
171+
* @return a map of properties
172+
*/
173+
public Map<String, Object> input() {
174+
return input;
175+
}
176+
177+
178+
/**
179+
* Convenience method which allows the developer to quickly retrieve the 'text' property from the
180+
* input object. This is equivalent to calling:
181+
*
182+
* <pre>
183+
* Map<String, Object> input = request.getInput();
184+
* String text = null;
185+
* if (input != null) {
186+
* text = input.get("text");
187+
* }
188+
* </pre>
189+
*
190+
* @return the value of the <code>input.text</code> property
191+
*/
192+
public String inputText() {
193+
return (input != null && input.get("text") != null) ? input.get("text").toString() : null;
194+
}
195+
196+
/**
197+
* New builder.
198+
*
199+
* @return the builder
200+
*/
201+
public Builder newBuilder() {
202+
return new Builder(this);
203+
}
204+
}

0 commit comments

Comments
 (0)