diff --git a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Application.java b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Application.java index 8788e254e..5031be87e 100644 --- a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Application.java +++ b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Application.java @@ -10,11 +10,12 @@ import com.microsoft.bot.integration.spring.BotDependencyConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; /** * This is the starting point of the Sprint Boot Bot application. - * + *

* This class also provides overrides for dependency injections. A class that extends the * {@link com.microsoft.bot.builder.Bot} interface should be annotated with @Component. * @@ -26,8 +27,7 @@ // could be used by eliminating this import and creating a new RestController. The default // controller is created by the Spring Boot container using dependency injection. The // default route is /api/messages. -@Import({BotController.class}) - +@Import({BotController.class, MessageController.class}) public class Application extends BotDependencyConfiguration { public static void main(String[] args) { SpringApplication.run(Application.class, args); @@ -43,4 +43,9 @@ public static void main(String[] args) { public BotFrameworkHttpAdapter getBotFrameworkHttpAdaptor(Configuration configuration) { return new AdapterWithErrorHandler(configuration); } + + @Bean + public ConversationReferences getConversationReferences() { + return new ConversationReferences(); + } } diff --git a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/ConversationReferences.java b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/ConversationReferences.java new file mode 100644 index 000000000..ff4498b01 --- /dev/null +++ b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/ConversationReferences.java @@ -0,0 +1,8 @@ +package com.microsoft.bot.sample.echo; + +import com.microsoft.bot.schema.ConversationReference; + +import java.util.concurrent.ConcurrentHashMap; + +public class ConversationReferences extends ConcurrentHashMap { +} diff --git a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/EchoBot.java b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/EchoBot.java index 9036c67c9..214089b75 100644 --- a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/EchoBot.java +++ b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/EchoBot.java @@ -7,7 +7,10 @@ import com.microsoft.bot.builder.ActivityHandler; import com.microsoft.bot.builder.MessageFactory; import com.microsoft.bot.builder.TurnContext; +import com.microsoft.bot.schema.Activity; import com.microsoft.bot.schema.ChannelAccount; +import com.microsoft.bot.schema.ConversationReference; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; @@ -23,20 +26,53 @@ * to new conversation participants.

*/ @Component +@Slf4j public class EchoBot extends ActivityHandler { + private ConversationReferences conversationReferences; + + public EchoBot(ConversationReferences withReferences) { + conversationReferences = withReferences; + } + @Override protected CompletableFuture onMessageActivity(TurnContext turnContext) { + addConversationReference(turnContext.getActivity()); return turnContext - .sendActivity(MessageFactory.text("Echo: " + turnContext.getActivity().getText())) - .thenApply(sendResult -> null); + .sendActivity(MessageFactory.text("Echo: " + turnContext.getActivity().getText())) + .thenApply(sendResult -> null); + } + + @Override + protected CompletableFuture onConversationUpdateActivity(TurnContext turnContext) { + addConversationReference(turnContext.getActivity()); + return super.onConversationUpdateActivity(turnContext); + } + + private void addConversationReference(Activity activity) { + ConversationReference conversationReference = activity.getConversationReference(); + conversationReferences.put(conversationReference.getUser().getId(), conversationReference); + } + + @Override + protected CompletableFuture onEventActivity(TurnContext turnContext) { + log.info("onEventActivity"); + return super.onEventActivity(turnContext); + } + + @Override + protected CompletableFuture onUnrecognizedActivityType(TurnContext turnContext) { + log.info("onUnrecognizedActivityType"); + addConversationReference(turnContext.getActivity()); + return super.onUnrecognizedActivityType(turnContext); } @Override protected CompletableFuture onMembersAdded(List membersAdded, TurnContext turnContext) { + addConversationReference(turnContext.getActivity()); return membersAdded.stream() - .filter(member -> !StringUtils.equals(member.getId(), turnContext.getActivity().getRecipient().getId())) - .map(channel -> turnContext.sendActivity(MessageFactory.text("Hello and welcome!"))) - .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!"))) + .collect(CompletableFutures.toFutureList()) + .thenApply(resourceResponses -> null); } } diff --git a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Message.java b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Message.java new file mode 100644 index 000000000..b529dcb52 --- /dev/null +++ b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Message.java @@ -0,0 +1,8 @@ +package com.microsoft.bot.sample.echo; + +import lombok.Data; + +@Data +public class Message { + String message; +} diff --git a/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/MessageController.java b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/MessageController.java new file mode 100644 index 000000000..286cca6a2 --- /dev/null +++ b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/MessageController.java @@ -0,0 +1,53 @@ +package com.microsoft.bot.sample.echo; + +import com.microsoft.bot.integration.BotFrameworkHttpAdapter; +import com.microsoft.bot.integration.Configuration; +import com.microsoft.bot.schema.ConversationReference; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +@RestController +public class MessageController { + private Logger logger = LoggerFactory.getLogger(MessageController.class); + + private final BotFrameworkHttpAdapter adapter; + private ConversationReferences conversationReferences; + private String appId; + + + public MessageController(BotFrameworkHttpAdapter withAdapter, Configuration withConfiguration, ConversationReferences withReferences) { + adapter = withAdapter; + this.conversationReferences = withReferences; + appId = withConfiguration.getProperty("MicrosoftAppId"); + if (StringUtils.isEmpty(appId)) { + appId = UUID.randomUUID().toString(); + } + } + + @PostMapping("/api/send") + public CompletableFuture> incoming(@RequestBody Message msg) { + String message = Optional.ofNullable(msg).map(Message::getMessage).orElse("---"); + logger.info("send : {}", message); + return CompletableFuture.supplyAsync(() -> { + try { + for (ConversationReference reference : conversationReferences.values()) { + adapter.continueConversation(appId, reference, turnContext -> turnContext.sendActivity(message) + .thenApply(resourceResponse -> null)); + } + } catch (Exception e) { + e.printStackTrace(); + } + return new ResponseEntity<>("success", HttpStatus.OK); + }); + } +} diff --git a/test.md b/test.md new file mode 100644 index 000000000..e69de29bb