Skip to content

Commit 8513d39

Browse files
committed
Fixes and add support to interactions references.
1 parent ba5410d commit 8513d39

9 files changed

Lines changed: 374 additions & 64 deletions

File tree

src/main/java/io/notifye/botengine/client/Engine.java

Lines changed: 139 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.io.IOException;
44
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collections;
57
import java.util.List;
8+
import java.util.Objects;
9+
import java.util.Optional;
610

711
import org.slf4j.Logger;
812
import org.slf4j.LoggerFactory;
@@ -16,6 +20,7 @@
1620
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
1721
import org.springframework.web.client.RestTemplate;
1822

23+
import com.fasterxml.jackson.annotation.JsonIgnore;
1924
import com.fasterxml.jackson.annotation.JsonInclude;
2025
import com.fasterxml.jackson.annotation.JsonProperty;
2126
import com.fasterxml.jackson.databind.JsonNode;
@@ -24,6 +29,7 @@
2429
import io.notifye.botengine.client.bots.Bot;
2530
import io.notifye.botengine.client.exception.BotException;
2631
import io.notifye.botengine.client.interceptor.LoggingRequestInterceptor;
32+
import io.notifye.botengine.client.model.Context;
2733
import io.notifye.botengine.client.model.Entity;
2834
import io.notifye.botengine.client.model.Interaction;
2935
import io.notifye.botengine.client.model.Query;
@@ -36,9 +42,11 @@ public final class Engine {
3642

3743
private static final String STORIES_RESOURCE = "%s/stories";
3844
private static final String STORY_RESOURCE = "%s/stories/%s";
39-
private static final String WELCOME_RESOURCE = "%s/stories/%s/interactions/welcome";
40-
private static final String INTERACTIONS_RESOURCE = "%s/stories/%s/interactions";
41-
private static final String FALLBACK_RESOURCE = "%s/stories/%s/interactions/fallback";
45+
private static final String REFERENCE_RESOURCE = STORY_RESOURCE + "/contexts";
46+
private static final String INTERACTIONS_RESOURCE = STORY_RESOURCE + "/interactions";
47+
private static final String WELCOME_RESOURCE = INTERACTIONS_RESOURCE + "/welcome";
48+
private static final String INTERACTIONS_CTX_RESOURCE = INTERACTIONS_RESOURCE + "/%s";
49+
private static final String FALLBACK_RESOURCE = INTERACTIONS_RESOURCE + "/fallback";
4250
private static final String QUERY_RESOURCE = "%s/query";
4351

4452
private static RestTemplate client;
@@ -114,7 +122,7 @@ public static void deleteStory(String id, Token token) {
114122
}
115123

116124
//Interactions
117-
public static Interaction creatInteraction(Story story, Interaction interaction, Token token) {
125+
public static Interaction creatInteraction(Story story, Interaction interaction, Token token) throws BotException {
118126
switch (interaction.getType()) {
119127
case welcome:
120128
return createOrUpdateWelcomeMessage(story, interaction, token);
@@ -127,7 +135,7 @@ public static Interaction creatInteraction(Story story, Interaction interaction,
127135
}
128136
}
129137

130-
public static Interaction createUserInteraction(Story story, Interaction interaction, Token token){
138+
public static Interaction createUserInteraction(Story story, Interaction interaction, Token token) throws BotException{
131139
final String INTERACTION_URI_RESOURCE = String.format(INTERACTIONS_RESOURCE, Bot.API_URL, story.getId());
132140
HttpHeaders headers = getDevHeaders(token);
133141
HttpEntity<Interaction> request = new HttpEntity<>(interaction, headers);
@@ -136,6 +144,21 @@ public static Interaction createUserInteraction(Story story, Interaction interac
136144
log.debug("Interaction Response -> {}", response);
137145
if(response.getStatusCode().is2xxSuccessful()){
138146
log.debug("Interactions created suscessfull");
147+
JsonNode node;
148+
try {
149+
node = getObjectMapper().readValue(response.getBody(), JsonNode.class);
150+
JsonNode idNode = node.get("id");
151+
JsonNode nameNode = node.get("name");
152+
JsonNode descriptionNode = node.get("description");
153+
interaction.setId(idNode.asText());
154+
} catch (IOException e) {
155+
log.error("Error on get Id of interaction", e);
156+
if(log.isDebugEnabled()) {
157+
e.printStackTrace();
158+
}
159+
throw new BotException("Error on get Id of interaction", e);
160+
}
161+
139162
}
140163
return interaction;
141164
}
@@ -172,10 +195,84 @@ public static Interaction createOrUpdateFalbackInteraction(Story story, Interact
172195
return interaction;
173196
}
174197

175-
public static Interaction createChildInteraction(Story story, Interaction root, Interaction child, Token token){
176-
return child;
198+
public static Interaction createChildInteraction(Story story, Interaction root, Interaction child, Token token) throws BotException{
199+
final String INTERACTION_CTX_URI_RESOURCE = String.format(INTERACTIONS_CTX_RESOURCE, Bot.API_URL, story.getId(), root.getId());
200+
HttpHeaders headers = getDevHeaders(token);
201+
202+
initializeEmptyProperties(child);
203+
HttpEntity<Interaction> request = new HttpEntity<>(child, headers);
204+
205+
ResponseEntity<String> response = getClient().exchange(INTERACTION_CTX_URI_RESOURCE, HttpMethod.POST, request, String.class);
206+
log.debug("Interaction Response -> {}", response);
207+
if(response.getStatusCode().is2xxSuccessful()){
208+
log.debug("Interactions created suscessfull");
209+
JsonNode node;
210+
try {
211+
node = getObjectMapper().readValue(response.getBody(), JsonNode.class);
212+
JsonNode idNode = node.get("id");
213+
JsonNode nameNode = node.get("name");
214+
JsonNode descriptionNode = node.get("description");
215+
root.getChilds().remove(child);
216+
child.setId(idNode.asText());
217+
root.getChilds().add(child);
218+
log.debug("Create Child Interaction suscessfully. \n\tRoot -> {}. \n\tChild -> {}", root, child);
219+
} catch (IOException e) {
220+
log.error("Error on get Id of interaction", e);
221+
if(log.isDebugEnabled()) {
222+
e.printStackTrace();
223+
}
224+
throw new BotException("Error on get Id of interaction", e);
225+
}
226+
}
227+
return root;
177228
}
178229

230+
public static void createReference(Story story, List<Interaction> interactions, Token token, Context context) {
231+
final String REFERENCE_URI_RESOURCE = String.format(REFERENCE_RESOURCE, Bot.API_URL, story.getId());
232+
HttpHeaders headers = getDevHeaders(token);
233+
234+
switch (context.getReferenceType()) {
235+
case BY_NAME:
236+
setContextIds(interactions, context);
237+
break;
238+
default:
239+
break;
240+
}
241+
242+
HttpEntity<List<Context>> request = new HttpEntity<>(Arrays.asList(context), headers);
243+
ResponseEntity<String> response = getClient().exchange(REFERENCE_URI_RESOURCE, HttpMethod.PUT, request, String.class);
244+
log.debug("Referenced Response -> {}", response);
245+
if(response.getStatusCode().is2xxSuccessful()){
246+
log.debug("Referenced created suscessfull");
247+
}
248+
}
249+
250+
/**
251+
* @param interaction
252+
*/
253+
public static void initializeEmptyProperties(Interaction interaction) {
254+
//childs
255+
if(interaction.getChilds() == null) {
256+
interaction.setChilds(Collections.emptyList());
257+
}
258+
259+
//parameters
260+
if(interaction.getParameters() == null) {
261+
interaction.setParameters(Collections.emptyList());
262+
}
263+
264+
// entities
265+
if(interaction.getEntities() == null) {
266+
interaction.setEntities(Collections.emptyList());
267+
}
268+
269+
//triggers
270+
if(interaction.getTriggers() == null) {
271+
interaction.setTriggers(Collections.emptyList());
272+
}
273+
274+
}
275+
179276
//Entities
180277
public static Entity createEntity(Entity entity, Token token){
181278
final String ENTITY_URI_RESOURCE = String.format("%s/entities", Bot.API_URL);
@@ -204,13 +301,13 @@ public static QueryResponse query(Story story, String query, Token token, String
204301
}
205302

206303
public static QueryResponse query(Query query, Token token, String session){
207-
if(query.getSessionId() == null){
304+
if(Objects.isNull(query.getSessionId())){
208305
query.setSessionId(session);
209306
}
210307
return q(query, token);
211308
}
212309
public static QueryResponse query(Story story, Query query, Token token, String session){
213-
if(query.getStoryId() == null){
310+
if(Objects.isNull(query.getStoryId())){
214311
query.setStoryId(story.getId());
215312
}
216313
return q(query, token);
@@ -220,6 +317,37 @@ private static int getDefaultLifespan() {
220317
return 2;
221318
}
222319

320+
private static void setContextIds(List<Interaction> interactions, Context context) {
321+
String interactionNameIdIn = context.getId();
322+
List<String> outIds = context.getContextOut();
323+
log.debug("Ids -> {}", outIds.get(0));
324+
Optional<Interaction> in = interactions.stream().filter(root -> root.getName().equals(interactionNameIdIn)).findFirst();
325+
Optional<Interaction> out = interactions.stream().filter(child -> child.getName().equals(outIds.get(0))).findFirst();
326+
if(!out.isPresent()) {
327+
328+
for (Interaction interaction : interactions) {
329+
log.debug("Trying get child for interaction {}", interaction);
330+
out = getChild(interaction, outIds.get(0));
331+
if(out.isPresent()) {
332+
break;
333+
}
334+
}
335+
}
336+
337+
log.debug("Out interaction -> {}", out);
338+
context.setId(in.orElse(null).getId());
339+
context.setContextOut(Arrays.asList(out.get().getId()));
340+
}
341+
342+
private static Optional<Interaction> getChild(Interaction interaction, String name){
343+
List<Interaction> interactions = interaction.getChilds();
344+
if(interactions != null && interactions.size() > 0 && name != null && !name.isEmpty()) {
345+
log.debug("Get child interaction with name \n\t{} \n\tin interaction {}", name, interaction);
346+
return interactions.stream().filter(c -> c.getName().equals(name)).findFirst();
347+
}
348+
return Optional.empty();
349+
}
350+
223351
public static String getSession(){
224352
long number = (long) Math.floor(Math.random() * 9_000_000_000L) + 1_000_000_000L;
225353
return String.valueOf(number);
@@ -257,7 +385,7 @@ private static HttpHeaders getDevHeaders(Token token){
257385
}
258386

259387
private static RestTemplate getClient(){
260-
if(client == null){
388+
if(Objects.isNull(client)){
261389
client = new RestTemplate();
262390
List<HttpMessageConverter<?>> converters = new ArrayList<>();
263391
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
@@ -291,7 +419,7 @@ public static final class WelcomeMessageWrapper {
291419
@JsonProperty("responses")
292420
private List<ResponseInteraction> responses;
293421

294-
@JsonProperty("disabled")
422+
@JsonIgnore
295423
private boolean disabled = false;
296424

297425
public WelcomeMessageWrapper(final List<ResponseInteraction> responses){

src/main/java/io/notifye/botengine/client/action/InteractionAction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@
66
public interface InteractionAction extends Action {
77

88
public InteractionAction add();
9+
910
public InteractionAction welcome(Interaction welcomeInteraction);
11+
1012
public InteractionAction fallback(Interaction fallbackInteraction);
13+
1114
public InteractionAction interaction(Interaction interaction);
15+
16+
public InteractionAction referenceById(String interactionIdIn, String interactionIdOut);
17+
public InteractionAction referenceById(String interactionIdIn, String interactionIdOut, boolean root);
18+
19+
public InteractionAction referenceByName(String interactionNameIn, String interactionNameOut);
20+
public InteractionAction referenceByName(String interactionNameIn, String interactionNameOut, boolean root);
21+
1222
public Bot build();
1323

1424
}

0 commit comments

Comments
 (0)