Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Oct 18, 2023
1 parent be420f7 commit f8cb6cd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
9 changes: 9 additions & 0 deletions petstore/petstoreassistant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,73 @@

package com.chtrembl.petstoreassistant;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codepoetics.protonpack.collectors.CompletableFutures;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.microsoft.bot.builder.ActivityHandler;
import com.microsoft.bot.builder.MessageFactory;
import com.microsoft.bot.builder.TurnContext;
import com.microsoft.bot.schema.Attachment;
import com.microsoft.bot.schema.ChannelAccount;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
* This class implements the functionality of the Bot.
*
* <p>
* This is where application specific logic for interacting with the users would be added. For this
* sample, the {@link #onMessageActivity(TurnContext)} echos the text back to the user. The {@link
* #onMembersAdded(List, TurnContext)} will send a greeting to new conversation participants.
* This is where application specific logic for interacting with the users would
* be added. For this
* sample, the {@link #onMessageActivity(TurnContext)} echos the text back to
* the user. The {@link
* #onMembersAdded(List, TurnContext)} will send a greeting to new conversation
* participants.
* </p>
*/
public class PetStoreAssistantBot extends ActivityHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(PetStoreAssistantBot.class);

@Override
protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {

LOGGER.info("channel data: "+ turnContext.getActivity().getChannelData().toString());

String text = turnContext.getActivity().getText().toLowerCase();
String digitalPersonResponse = null;
if(text.contains("ball")){
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 ball\"}}";
Attachment attachment = new Attachment();
attachment.setContentType("application/json");

attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class));
attachment.setName("public-image-ball");


return turnContext.sendActivity(
MessageFactory.attachment(attachment, "I have something nice to show @showcards(image-ball) you.")
).thenApply(sendResult -> null);
}

return turnContext.sendActivity(
MessageFactory.text("Echo: " + turnContext.getActivity().getText() + " channeldata:" + turnContext.getActivity().getChannelData() + " entities:" + turnContext.getActivity().getEntities().toString();)
).thenApply(sendResult -> null);
}

@Override
protected CompletableFuture<Void> onMembersAdded(
List<ChannelAccount> membersAdded,
TurnContext turnContext
) {
List<ChannelAccount> membersAdded,
TurnContext turnContext) {
return membersAdded.stream()
.filter(
member -> !StringUtils
.equals(member.getId(), turnContext.getActivity().getRecipient().getId())
).map(channel -> turnContext.sendActivity(MessageFactory.text("Hello and welcome to the Azure Pet Store!")))
.collect(CompletableFutures.toFutureList()).thenApply(resourceResponses -> null);
.filter(
member -> !StringUtils
.equals(member.getId(), turnContext.getActivity().getRecipient().getId()))
.map(channel -> turnContext
.sendActivity(MessageFactory.text("Hello and welcome to the Azure Pet Store!")))
.collect(CompletableFutures.toFutureList()).thenApply(resourceResponses -> null);
}
}

0 comments on commit f8cb6cd

Please sign in to comment.