From 383f7e1c4bf921edb61d51abe60e41a16e89d205 Mon Sep 17 00:00:00 2001 From: Chris Tremblay Date: Fri, 5 Jan 2024 15:59:54 -0500 Subject: [PATCH] updates --- .../PetStoreAssistantBot.java | 10 +++++++--- .../petstoreassistant/model/DPResponse.java | 9 ++++++++- .../service/AzureAIServices.java | 6 ++++-- .../utility/PetStoreAssistantUtilities.java | 20 +++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java index 93c4956c..ff3d9576 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java @@ -187,6 +187,10 @@ protected CompletableFuture onMessageActivity(TurnContext turnContext) { dpResponse.setDpResponseText(responseText); } + if(dpResponse.isImageContentCard()) + { + return PetStoreAssistantUtilities.getImageCard(turnContext, dpResponse); + } return turnContext.sendActivity( MessageFactory.text(dpResponse.getDpResponseText())).thenApply(sendResult -> null); } @@ -251,7 +255,7 @@ else if(azurePetStoreSessionInfo != null) } private CompletableFuture getDebug(TurnContext turnContext, String text, AzurePetStoreSessionInfo azurePetStoreSessionInfo) { - if (text.contains("debug")) + if (text.equals("debug")) { if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) { @@ -263,7 +267,7 @@ private CompletableFuture getDebug(TurnContext turnContext, String text, A MessageFactory.text("azurePetStoreSessionInfo was null, cache size: "+cache.estimatedSize())).thenApply(sendResult -> null); } } - if (text.contains("card")) { + if (text.equals("button card")) { if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) { text = azurePetStoreSessionInfo.getNewText(); @@ -281,7 +285,7 @@ private CompletableFuture getDebug(TurnContext turnContext, String text, A .thenApply(sendResult -> null); } - if (text.contains("ball")) + if (text.equals("image card")) { String jsonString = "{\"type\":\"image\",\"id\":\"image-ball\",\"data\":{\"url\": \"https://raw.githubusercontent.com/chtrembl/staticcontent/master/dog-toys/ball.jpg?raw=true\",\"alt\": \"This is a pretty ball\",\"caption\": \"ball blah blah blah\"}}"; Attachment attachment = new Attachment(); diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/DPResponse.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/DPResponse.java index 84f1fbb8..ede1681b 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/DPResponse.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/DPResponse.java @@ -11,7 +11,8 @@ public class DPResponse { private boolean updateCart = false; private boolean completeCart = false; private String aoaiResponse = null; - + private boolean imageContentCard = false; + public DPResponse() { super(); } @@ -56,4 +57,10 @@ public String getAoaiResponse() { public void setAoaiResponse(String aoaiResponse) { this.aoaiResponse = aoaiResponse; } + public boolean isImageContentCard() { + return imageContentCard; + } + public void setImageContentCard(boolean imageContentCard) { + this.imageContentCard = imageContentCard; + } } diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java index 40f640db..4e065039 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java @@ -175,7 +175,7 @@ public DPResponse search(String text, Classification classification) { DPResponse dpResponse = new DPResponse(); dpResponse.setClassification(classification); - String dpResponseText = "I found some information on our products. We have some a "; + String dpResponseText = "I found some information on our products. We have a "; String filter = ""; @@ -263,7 +263,9 @@ public DPResponse search(String text, Classification classification) { // this should become a content card with a carousel of product(s) for now just display description if there is 1 product and override the stuff above if(products.size() == 1 || classification.equals(Classification.MORE_PRODUCT_INFORMATION)) { - dpResponseText = "I found some information on our products. Here is a description of the " + products.get(0).getName() + ". " + products.get(0).getDescription(); + dpResponse.setImageContentCard(true); + dpResponseText = "Check out this product of ours, the " + products.get(0).getName(); + dpResponse.setDpResponseText(dpResponseText); } else diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java index 5472abbe..fcd1aee4 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java @@ -1,5 +1,6 @@ package com.chtrembl.petstoreassistant.utility; +import java.util.concurrent.CompletableFuture; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -7,6 +8,12 @@ import org.slf4j.LoggerFactory; import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo; +import com.chtrembl.petstoreassistant.model.DPResponse; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.microsoft.bot.builder.MessageFactory; +import com.microsoft.bot.builder.TurnContext; +import com.microsoft.bot.schema.Attachment; public class PetStoreAssistantUtilities { private static final Logger LOGGER = LoggerFactory.getLogger(PetStoreAssistantUtilities.class); @@ -33,4 +40,17 @@ public static AzurePetStoreSessionInfo getAzurePetStoreSessionInfo(String text) return azurePetStoreSessionInfo; } + + public static CompletableFuture getImageCard(TurnContext turnContext, DPResponse dpResponse) { + String jsonString = "{\"type\":\"image\",\"id\":\"image-product\",\"data\":{\"url\": \""+dpResponse.getProducts().get(0).getPhotoURL()+"\",\"alt\": \""+dpResponse.getProducts().get(0).getDescription()+"\",\"caption\": \""+dpResponse.getProducts().get(0).getDescription()+"\"}}"; + Attachment attachment = new Attachment(); + attachment.setContentType("application/json"); + + attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); + attachment.setName("public-image-product"); + + return turnContext.sendActivity( + MessageFactory.attachment(attachment, dpResponse.getDpResponseText() + " @showcards(image-product)")) + .thenApply(sendResult -> null); + } }