From d4321784394d66255ca65db73f6d9f6383a9acef Mon Sep 17 00:00:00 2001 From: Phung Hoang Date: Wed, 13 Nov 2019 11:19:04 +0700 Subject: [PATCH 1/5] Test --- test.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.md diff --git a/test.md b/test.md new file mode 100644 index 000000000..e69de29bb From 5b7c36cec5308b4012d469cd56b7056e8adcf046 Mon Sep 17 00:00:00 2001 From: Phung Hoang Date: Wed, 13 Nov 2019 13:29:58 +0700 Subject: [PATCH 2/5] Add send api --- .../bot/sample/echo/Application.java | 5 ++- .../microsoft/bot/sample/echo/EchoBot.java | 32 +++++++++++++++---- .../bot/sample/echo/MessageController.java | 18 +++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/MessageController.java 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..c47a84cdc 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 @@ -14,7 +14,7 @@ /** * 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 +26,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); 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..8d0213513 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 @@ -8,6 +8,7 @@ import com.microsoft.bot.builder.MessageFactory; import com.microsoft.bot.builder.TurnContext; import com.microsoft.bot.schema.ChannelAccount; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; @@ -23,20 +24,39 @@ * to new conversation participants.

*/ @Component +@Slf4j public class EchoBot extends ActivityHandler { @Override protected CompletableFuture onMessageActivity(TurnContext turnContext) { 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) { + log.info("onConversationUpdateActivity"); + return super.onConversationUpdateActivity(turnContext); + } + + @Override + protected CompletableFuture onEventActivity(TurnContext turnContext) { + log.info("onEventActivity"); + return super.onEventActivity(turnContext); + } + + @Override + protected CompletableFuture onUnrecognizedActivityType(TurnContext turnContext) { + log.info("onUnrecognizedActivityType"); + return super.onUnrecognizedActivityType(turnContext); } @Override protected CompletableFuture onMembersAdded(List 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!"))) - .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/MessageController.java b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/MessageController.java new file mode 100644 index 000000000..522efb197 --- /dev/null +++ b/samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/MessageController.java @@ -0,0 +1,18 @@ +package com.microsoft.bot.sample.echo; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.concurrent.CompletableFuture; + +@RestController +public class MessageController { + @GetMapping("/api/send") + public CompletableFuture> incoming() { + return CompletableFuture.supplyAsync(() -> { + return new ResponseEntity<>("sucessss", HttpStatus.OK); + }); + } +} From 78d6fa57e0cf1ff48a8539780009b0c233c6c991 Mon Sep 17 00:00:00 2001 From: Phung Hoang Date: Wed, 13 Nov 2019 13:58:27 +0700 Subject: [PATCH 3/5] Add conversation references --- .../bot/sample/echo/Application.java | 6 +++ .../sample/echo/ConversationReferences.java | 8 ++++ .../microsoft/bot/sample/echo/EchoBot.java | 18 ++++++++- .../bot/sample/echo/MessageController.java | 37 ++++++++++++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/ConversationReferences.java 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 c47a84cdc..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,6 +10,7 @@ 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; /** @@ -42,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 8d0213513..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,9 @@ 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; @@ -26,8 +28,15 @@ @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); @@ -35,10 +44,15 @@ protected CompletableFuture onMessageActivity(TurnContext turnContext) { @Override protected CompletableFuture onConversationUpdateActivity(TurnContext turnContext) { - log.info("onConversationUpdateActivity"); + 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"); @@ -48,11 +62,13 @@ protected CompletableFuture onEventActivity(TurnContext 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!"))) 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 index 522efb197..1cb9acb24 100644 --- 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 @@ -1,16 +1,51 @@ package com.microsoft.bot.sample.echo; +import com.microsoft.bot.builder.Bot; +import com.microsoft.bot.integration.BotFrameworkHttpAdapter; +import com.microsoft.bot.integration.Configuration; +import com.microsoft.bot.integration.spring.BotController; +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.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; 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(BotController.class); + + private final BotFrameworkHttpAdapter adapter; + private final Bot bot; + private ConversationReferences conversationReferences; + private String appId; + + + public MessageController(BotFrameworkHttpAdapter withAdapter, Bot withBot, Configuration withConfiguration, ConversationReferences withReferences) { + adapter = withAdapter; + bot = withBot; + this.conversationReferences = withReferences; + appId = withConfiguration.getProperty("MicrosoftAppId"); + if (StringUtils.isEmpty(appId)) { + appId = UUID.randomUUID().toString(); + } + } + @GetMapping("/api/send") - public CompletableFuture> incoming() { + public CompletableFuture> incoming(@RequestParam String msg) { + String message = Optional.ofNullable(msg).orElse("---"); + for (ConversationReference reference : conversationReferences.values()) { + adapter.continueConversation(appId, reference, turnContext -> turnContext.sendActivity(message) + .thenApply(resourceResponse -> null)); + } + return CompletableFuture.supplyAsync(() -> { return new ResponseEntity<>("sucessss", HttpStatus.OK); }); From 1f5bd049c87cd7457ea94ab263d793d7bb18d6e5 Mon Sep 17 00:00:00 2001 From: Phung Hoang Date: Wed, 13 Nov 2019 14:07:26 +0700 Subject: [PATCH 4/5] OK --- .../bot/sample/echo/MessageController.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 index 1cb9acb24..77aa72191 100644 --- 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 @@ -1,9 +1,7 @@ package com.microsoft.bot.sample.echo; -import com.microsoft.bot.builder.Bot; import com.microsoft.bot.integration.BotFrameworkHttpAdapter; import com.microsoft.bot.integration.Configuration; -import com.microsoft.bot.integration.spring.BotController; import com.microsoft.bot.schema.ConversationReference; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -20,17 +18,15 @@ @RestController public class MessageController { - private Logger logger = LoggerFactory.getLogger(BotController.class); + private Logger logger = LoggerFactory.getLogger(MessageController.class); private final BotFrameworkHttpAdapter adapter; - private final Bot bot; private ConversationReferences conversationReferences; private String appId; - public MessageController(BotFrameworkHttpAdapter withAdapter, Bot withBot, Configuration withConfiguration, ConversationReferences withReferences) { + public MessageController(BotFrameworkHttpAdapter withAdapter, Configuration withConfiguration, ConversationReferences withReferences) { adapter = withAdapter; - bot = withBot; this.conversationReferences = withReferences; appId = withConfiguration.getProperty("MicrosoftAppId"); if (StringUtils.isEmpty(appId)) { @@ -41,12 +37,16 @@ public MessageController(BotFrameworkHttpAdapter withAdapter, Bot withBot, Confi @GetMapping("/api/send") public CompletableFuture> incoming(@RequestParam String msg) { String message = Optional.ofNullable(msg).orElse("---"); - for (ConversationReference reference : conversationReferences.values()) { - adapter.continueConversation(appId, reference, turnContext -> turnContext.sendActivity(message) - .thenApply(resourceResponse -> null)); - } - + 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<>("sucessss", HttpStatus.OK); }); } From 4b2dd838615182e25a4de45aa1da19ec3e1f34e1 Mon Sep 17 00:00:00 2001 From: Phung Hoang Date: Wed, 13 Nov 2019 14:23:39 +0700 Subject: [PATCH 5/5] Convert get to post --- .../java/com/microsoft/bot/sample/echo/Message.java | 8 ++++++++ .../microsoft/bot/sample/echo/MessageController.java | 12 ++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Message.java 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 index 77aa72191..286cca6a2 100644 --- 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 @@ -8,8 +8,8 @@ import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; +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; @@ -34,9 +34,9 @@ public MessageController(BotFrameworkHttpAdapter withAdapter, Configuration with } } - @GetMapping("/api/send") - public CompletableFuture> incoming(@RequestParam String msg) { - String message = Optional.ofNullable(msg).orElse("---"); + @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 { @@ -47,7 +47,7 @@ public CompletableFuture> incoming(@RequestParam String m } catch (Exception e) { e.printStackTrace(); } - return new ResponseEntity<>("sucessss", HttpStatus.OK); + return new ResponseEntity<>("success", HttpStatus.OK); }); } }