Skip to content

Commit

Permalink
SYMPHONYP-1135-summary-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekdahiya-db committed Jun 27, 2024
1 parent a0df023 commit e7eb77b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.finos.springbot.workflow.response;

import java.util.Map;

import org.finos.springbot.workflow.content.Addressable;
import org.finos.springbot.workflow.response.handlers.ResponseHandler;

import java.util.Map;

/**
* A Response that contains some JSON data to be included in the message.
*
Expand All @@ -17,28 +17,13 @@ public class DataResponse implements Response {
private final String templateName;
private final Addressable to;

private String summary;

public DataResponse(Addressable to, Map<String, Object> data, String templateName) {
super();
this.to = to;
this.data = data;
this.templateName = templateName;
}

public DataResponse(Addressable to, Map<String, Object> data, String templateName, String summary) {
this(to,data,templateName);
this.summary = summary;
}

public void setSummary(String summary) {
this.summary = summary;
}

public String getSummary() {
return summary;
}

public Map<String, Object> getData() {
return data;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.finos.springbot.workflow.response;

import java.util.HashMap;
import java.util.Map;

import org.finos.springbot.workflow.content.Addressable;
import org.finos.springbot.workflow.content.Content;
import org.finos.springbot.workflow.content.Message;

import java.util.HashMap;
import java.util.Map;

/**
* A Response that includes a message {@link Content} to send back to the user.
*
Expand All @@ -25,10 +25,6 @@ public MessageResponse(Addressable stream, Map<String, Object> data, Content m,
public MessageResponse(Addressable stream, String textContent) {
this(stream, Message.of(textContent));
}
public MessageResponse(Addressable stream, String textContent, String responseSummary) {
this(stream, Message.of(textContent));
setSummary(responseSummary);
}

public MessageResponse(Addressable stream, Content m) {
super(stream, new HashMap<>(), null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.finos.springbot.workflow.response;

import java.util.HashMap;
import java.util.Map;

import org.finos.springbot.workflow.annotations.Template;
import org.finos.springbot.workflow.annotations.WorkMode;
import org.finos.springbot.workflow.content.Addressable;
Expand All @@ -11,6 +8,9 @@
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import java.util.HashMap;
import java.util.Map;

/**
* Returns @Work-annotated object back to the user, either as form if editable=true, or
* displayed in the chat if editable=false.
Expand Down Expand Up @@ -43,11 +43,6 @@ public WorkResponse(Addressable to, Object o, WorkMode m, ButtonList buttons, Er
public WorkResponse(Addressable to, Object o, WorkMode m) {
this(to, o, m, null, null);
}

public WorkResponse(Addressable to, Object o, WorkMode m, String responseSummary){
this(to,o,m);
setSummary(responseSummary);
}

public static Map<String, Object> createEntityMap(Object o, ButtonList buttons, ErrorMap errors) {
Map<String, Object> json = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.finos.springbot.workflow.response.templating;

import org.finos.springbot.workflow.annotations.WorkMode;
import org.finos.springbot.workflow.content.Addressable;
import org.finos.springbot.workflow.form.ButtonList;
import org.finos.springbot.workflow.form.ErrorMap;
import org.finos.springbot.workflow.response.WorkResponse;

import java.util.Map;

public class TeamsWorkResponse extends WorkResponse {
private final String summary;

public TeamsWorkResponse(Addressable to, Map<String, Object> data, String templateName, WorkMode m, Class<?> formClass, String summary) {
super(to, data, templateName, m, formClass);
this.summary = summary;
}

public TeamsWorkResponse(Addressable to, Object o, WorkMode m, ButtonList buttons, ErrorMap errors, String summary) {
super(to, o, m, buttons, errors);
this.summary = summary;
}

public TeamsWorkResponse(Addressable to, Object o, WorkMode m, String summary) {
super(to, o, m);
this.summary = summary;
}

public String getSummary() {
return summary;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.finos.springbot.teams.handlers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.BiFunction;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.bot.connector.rest.ErrorResponseException;
import com.microsoft.bot.schema.*;
import org.apache.commons.lang3.StringUtils;
import org.finos.springbot.teams.TeamsException;
import org.finos.springbot.teams.content.TeamsAddressable;
Expand All @@ -23,12 +21,10 @@
import org.finos.springbot.workflow.actions.Action;
import org.finos.springbot.workflow.actions.ErrorAction;
import org.finos.springbot.workflow.annotations.WorkMode;
import org.finos.springbot.workflow.response.AttachmentResponse;
import org.finos.springbot.workflow.response.ErrorResponse;
import org.finos.springbot.workflow.response.MessageResponse;
import org.finos.springbot.workflow.response.Response;
import org.finos.springbot.workflow.response.WorkResponse;
import org.finos.springbot.workflow.response.*;
import org.finos.springbot.workflow.response.handlers.ResponseHandler;
import org.finos.springbot.workflow.response.templating.TeamsWorkResponse;
import org.finos.springbot.workflow.tags.HeaderDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,16 +33,10 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ErrorHandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.bot.connector.rest.ErrorResponseException;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.Attachment;
import com.microsoft.bot.schema.Entity;
import com.microsoft.bot.schema.ResourceResponse;
import com.microsoft.bot.schema.TextFormatTypes;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.BiFunction;

public class TeamsResponseHandler implements ResponseHandler<ResourceResponse>, ApplicationContextAware {

Expand Down Expand Up @@ -104,7 +94,7 @@ public ResourceResponse apply(Response t) {
attachment = attachmentHandler.formatAttachment((AttachmentResponse) mr);
}

return sendXMLResponse(content, attachment, ta, entities, mr.getData(), mr.getSummary())
return sendXMLResponse(content, attachment, ta, entities, mr.getData())
.handle(handleErrorAndStorage(content, ta, mr.getData(), t)).get();

} else if (t instanceof WorkResponse) {
Expand All @@ -113,13 +103,14 @@ public ResourceResponse apply(Response t) {

if (tt == TemplateType.ADAPTIVE_CARD) {
JsonNode cardJson = workTemplater.template(wr);
return sendCardResponse(cardJson, ta, wr.getData(), wr.getSummary())
Optional<String> responseSummaryOptional = t instanceof TeamsWorkResponse ? Optional.of(((TeamsWorkResponse) t).getSummary()) : Optional.empty() ;
return sendCardResponse(cardJson, ta, wr.getData(), responseSummaryOptional)
.handle(handleErrorAndStorage(cardJson, ta, wr.getData(), t)).get();
} else {
MarkupAndEntities mae = displayTemplater.template(wr);
String content = mae.getContents();
List<Entity> entities = mae.getEntities();
return sendXMLResponse(content, null, ta, entities, wr.getData(), wr.getSummary())
return sendXMLResponse(content, null, ta, entities, wr.getData())
.handle(handleButtonsIfNeeded(tt, wr))
.handle(handleErrorAndStorage(content, ta, wr.getData(), t)).get();

Expand Down Expand Up @@ -151,12 +142,11 @@ protected TemplateType getTemplateType(WorkResponse wr) {
return tt;
}

protected CompletableFuture<ResourceResponse> sendXMLResponse(String xml, Attachment attachment, TeamsAddressable address, List<Entity> entities, Map<String, Object> data, String summary) throws Exception {
protected CompletableFuture<ResourceResponse> sendXMLResponse(String xml, Attachment attachment, TeamsAddressable address, List<Entity> entities, Map<String, Object> data) throws Exception {
Activity out = Activity.createMessageActivity();
if(Objects.nonNull(attachment)) {
out.getAttachments().add(attachment);
}else {
if(StringUtils.isNotBlank(summary)) out.setSummary(summary);
out.setEntities(entities);
out.setTextFormat(TextFormatTypes.XML);
}
Expand All @@ -173,7 +163,8 @@ private BiFunction<? super ResourceResponse, Throwable, ResourceResponse> handle
JsonNode buttonsJson = workTemplater.template(null);
wr.getData().put(AdaptiveCardTemplateProvider.FORMID_KEY, "just-buttons");
JsonNode expandedJson = workTemplater.applyTemplate(buttonsJson, wr);
return sendCardResponse(expandedJson, (TeamsAddressable) wr.getAddress(), wr.getData(), wr.getSummary()).get();
Optional<String> responseSummaryOptional = wr instanceof TeamsWorkResponse ? Optional.of(((TeamsWorkResponse) wr).getSummary()) : Optional.empty() ;
return sendCardResponse(expandedJson, (TeamsAddressable) wr.getAddress(), wr.getData(), responseSummaryOptional).get();
} else {
return rr;
}
Expand Down Expand Up @@ -221,12 +212,13 @@ private BiFunction<? super ResourceResponse, Throwable, ResourceResponse> handle
};
}

protected CompletableFuture<ResourceResponse> sendCardResponse(JsonNode json, TeamsAddressable address, Map<String, Object> data, String summary) throws Exception {
protected CompletableFuture<ResourceResponse> sendCardResponse(JsonNode json, TeamsAddressable address, Map<String, Object> data, Optional<String> summaryOptional) throws Exception {
Activity out = Activity.createMessageActivity();
Attachment body = new Attachment();
body.setContentType("application/vnd.microsoft.card.adaptive");
body.setContent(json);
if(StringUtils.isNotBlank(summary)) out.setSummary(summary);
if(summaryOptional.isPresent() && StringUtils.isNotBlank(summaryOptional.get()))
out.setSummary(summaryOptional.get());
out.getAttachments().add(body);
return ah.handleActivity(out, address);
}
Expand Down

0 comments on commit e7eb77b

Please sign in to comment.