Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 5301ef2

Browse files
authored
Refactored 18.bot-authentication to match other samples (#968)
1 parent d133a1e commit 5301ef2

5 files changed

Lines changed: 74 additions & 49 deletions

File tree

samples/18.bot-authentication/src/main/java/com/microsoft/bot/sample/authentication/Application.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,62 @@
33

44
package com.microsoft.bot.sample.authentication;
55

6+
import com.microsoft.bot.builder.Bot;
7+
import com.microsoft.bot.builder.ConversationState;
8+
import com.microsoft.bot.builder.UserState;
9+
import com.microsoft.bot.dialogs.Dialog;
610
import com.microsoft.bot.integration.AdapterWithErrorHandler;
711
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
812
import com.microsoft.bot.integration.Configuration;
913
import com.microsoft.bot.integration.spring.BotController;
1014
import com.microsoft.bot.integration.spring.BotDependencyConfiguration;
1115
import org.springframework.boot.SpringApplication;
1216
import org.springframework.boot.autoconfigure.SpringBootApplication;
17+
import org.springframework.context.annotation.Bean;
1318
import org.springframework.context.annotation.Import;
1419

15-
/**
16-
* This is the starting point of the Sprint Boot Bot application.
17-
*
18-
* This class also provides overrides for dependency injections. A class that
19-
* extends the {@link com.microsoft.bot.builder.Bot} interface should be
20-
* annotated with @Component.
21-
*
22-
* @see RichCardsBot
23-
*/
20+
//
21+
// This is the starting point of the Sprint Boot Bot application.
22+
//
2423
@SpringBootApplication
2524

2625
// Use the default BotController to receive incoming Channel messages. A custom
2726
// controller could be used by eliminating this import and creating a new
28-
// RestController.
27+
// org.springframework.web.bind.annotation.RestController.
2928
// The default controller is created by the Spring Boot container using
3029
// dependency injection. The default route is /api/messages.
3130
@Import({BotController.class})
3231

32+
/**
33+
* This class extends the BotDependencyConfiguration which provides the default
34+
* implementations for a Bot application. The Application class should
35+
* override methods in order to provide custom implementations.
36+
*/
3337
public class Application extends BotDependencyConfiguration {
3438
public static void main(String[] args) {
3539
SpringApplication.run(Application.class, args);
3640
}
3741

42+
/**
43+
* Returns the Bot for this application.
44+
*
45+
* <p>
46+
* The @Component annotation could be used on the Bot class instead of this method
47+
* with the @Bean annotation.
48+
* </p>
49+
*
50+
* @return The Bot implementation for this application.
51+
*/
52+
@Bean
53+
public Bot getBot(
54+
Configuration configuration,
55+
ConversationState conversationState,
56+
UserState userState,
57+
MainDialog dialog
58+
) {
59+
return new AuthBot(conversationState, userState, new MainDialog(configuration));
60+
}
61+
3862
/**
3963
* Returns a custom Adapter that provides error handling.
4064
*

samples/18.bot-authentication/src/main/java/com/microsoft/bot/sample/authentication/AuthBot.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313
import com.microsoft.bot.dialogs.Dialog;
1414
import com.microsoft.bot.schema.ChannelAccount;
1515

16-
import org.springframework.stereotype.Component;
1716
import com.codepoetics.protonpack.collectors.CompletableFutures;
1817
import com.microsoft.bot.schema.Activity;
1918
import org.apache.commons.lang3.StringUtils;
2019

21-
22-
// Copyright (c) Microsoft Corporation. All rights reserved.
23-
// Licensed under the MIT License.
24-
25-
@Component
2620
public class AuthBot extends DialogBot<MainDialog> {
2721

2822
public AuthBot(ConversationState conversationState, UserState userState, MainDialog dialog) {
@@ -49,7 +43,7 @@ protected CompletableFuture<Void> onMembersAdded(
4943
@Override
5044
protected CompletableFuture<Void> onTokenResponseEvent(TurnContext turnContext) {
5145
// Run the Dialog with the new Token Response Event Activity.
52-
return Dialog.run(dialog, turnContext, conversationState.createProperty("DialogState"));
46+
return Dialog.run(dialog, turnContext, conversationState.createProperty("DialogState"));
5347
}
5448

5549
}

samples/18.bot-authentication/src/main/java/com/microsoft/bot/sample/authentication/DialogBot.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
import java.util.concurrent.CompletableFuture;
1313

1414
/**
15-
* This Bot implementation can run any type of Dialog. The use of type parameterization is to
16-
* allows multiple different bots to be run at different endpoints within the same project. This
17-
* can be achieved by defining distinct Controller types each with dependency on distinct IBot
18-
* types, this way ASP Dependency Injection can glue everything together without ambiguity. The
19-
* ConversationState is used by the Dialog system. The UserState isn't, however, it might have
20-
* been used in a Dialog implementation, and the requirement is that all BotState objects are
21-
* saved at the end of a turn.
15+
* This Bot implementation can run any type of Dialog. The use of type parameterization is to allows
16+
* multiple different bots to be run at different endpoints within the same project. This can be
17+
* achieved by defining distinct Controller types each with dependency on distinct IBot types, this
18+
* way ASP Dependency Injection can glue everything together without ambiguity. The
19+
* ConversationState is used by the Dialog system. The UserState isn't, however, it might have been
20+
* used in a Dialog implementation, and the requirement is that all BotState objects are saved at
21+
* the end of a turn.
2222
*/
2323
public class DialogBot<T extends Dialog> extends ActivityHandler {
24+
2425
protected Dialog dialog;
2526
protected BotState conversationState;
2627
protected BotState userState;

samples/18.bot-authentication/src/main/java/com/microsoft/bot/sample/authentication/LogoutDialog.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ public LogoutDialog(String id, String connectionName) {
2323

2424

2525
@Override
26-
protected CompletableFuture<DialogTurnResult> onBeginDialog(DialogContext innerDc, Object options) {
27-
DialogTurnResult result = interrupt(innerDc).join();
26+
protected CompletableFuture<DialogTurnResult> onBeginDialog(
27+
DialogContext innerDc, Object options
28+
) {
29+
DialogTurnResult result = interrupt(innerDc).join();
2830
if (result != null) {
2931
return CompletableFuture.completedFuture(result);
3032
}
@@ -34,7 +36,7 @@ protected CompletableFuture<DialogTurnResult> onBeginDialog(DialogContext innerD
3436

3537
@Override
3638
protected CompletableFuture<DialogTurnResult> onContinueDialog(DialogContext innerDc) {
37-
DialogTurnResult result = interrupt(innerDc).join();
39+
DialogTurnResult result = interrupt(innerDc).join();
3840
if (result != null) {
3941
return CompletableFuture.completedFuture(result);
4042
}
@@ -48,10 +50,12 @@ private CompletableFuture<DialogTurnResult> interrupt(DialogContext innerDc) {
4850

4951
if (text.equals("logout")) {
5052
// The bot adapter encapsulates the authentication processes.
51-
BotFrameworkAdapter botAdapter = (BotFrameworkAdapter) innerDc.getContext().getAdapter();
53+
BotFrameworkAdapter botAdapter = (BotFrameworkAdapter) innerDc.getContext()
54+
.getAdapter();
5255
botAdapter.signOutUser(innerDc.getContext(), getConnectionName(), null).join();
53-
innerDc.getContext().sendActivity(MessageFactory.text("You have been signed out.")).join();
54-
return innerDc.cancelAllDialogs();
56+
innerDc.getContext().sendActivity(MessageFactory.text("You have been signed out."))
57+
.join();
58+
return innerDc.cancelAllDialogs();
5559
}
5660
}
5761

samples/18.bot-authentication/src/main/java/com/microsoft/bot/sample/authentication/MainDialog.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import com.microsoft.bot.integration.Configuration;
1919
import com.microsoft.bot.schema.TokenResponse;
2020

21-
import org.springframework.stereotype.Component;
22-
23-
@Component
24-
class MainDialog extends LogoutDialog {
21+
class MainDialog extends LogoutDialog {
2522

2623
public MainDialog(Configuration configuration) {
2724
super("MainDialog", configuration.getProperty("ConnectionName"));
@@ -51,28 +48,31 @@ public MainDialog(Configuration configuration) {
5148
}
5249

5350
private CompletableFuture<DialogTurnResult> promptStep(WaterfallStepContext stepContext) {
54-
return stepContext.beginDialog("OAuthPrompt", null);
51+
return stepContext.beginDialog("OAuthPrompt", null);
5552
}
5653

5754
private CompletableFuture<DialogTurnResult> loginStep(WaterfallStepContext stepContext) {
5855
// Get the token from the previous step. Note that we could also have gotten the
5956
// token directly from the prompt itself. There instanceof an example of this in the next method.
60-
TokenResponse tokenResponse = (TokenResponse)stepContext.getResult();
57+
TokenResponse tokenResponse = (TokenResponse) stepContext.getResult();
6158
if (tokenResponse != null) {
6259
stepContext.getContext().sendActivity(MessageFactory.text("You are now logged in."));
6360
PromptOptions options = new PromptOptions();
6461
options.setPrompt(MessageFactory.text("Would you like to view your token?"));
6562
return stepContext.prompt("ConfirmPrompt", options);
6663
}
6764

68-
stepContext.getContext().sendActivity(MessageFactory.text("Login was not successful please try again."));
69-
return stepContext.endDialog();
65+
stepContext.getContext()
66+
.sendActivity(MessageFactory.text("Login was not successful please try again."));
67+
return stepContext.endDialog();
7068
}
7169

72-
private CompletableFuture<DialogTurnResult> displayTokenPhase1(WaterfallStepContext stepContext) {
73-
stepContext.getContext().sendActivity(MessageFactory.text("Thank you."));
70+
private CompletableFuture<DialogTurnResult> displayTokenPhase1(
71+
WaterfallStepContext stepContext
72+
) {
73+
stepContext.getContext().sendActivity(MessageFactory.text("Thank you."));
7474

75-
boolean result = (boolean)stepContext.getResult();
75+
boolean result = (boolean) stepContext.getResult();
7676
if (result) {
7777
// Call the prompt again because we need the token. The reasons for this are:
7878
// 1. If the user instanceof already logged in we do not need to store the token locally in the bot and worry
@@ -82,21 +82,23 @@ private CompletableFuture<DialogTurnResult> displayTokenPhase1(WaterfallStepCont
8282
//
8383
// There instanceof no reason to store the token locally in the bot because we can always just call
8484
// the OAuth prompt to get the token or get a new token if needed.
85-
return stepContext.beginDialog("OAuthPrompt");
85+
return stepContext.beginDialog("OAuthPrompt");
8686
}
8787

88-
return stepContext.endDialog();
88+
return stepContext.endDialog();
8989
}
9090

91-
private CompletableFuture<DialogTurnResult> displayTokenPhase2(WaterfallStepContext stepContext) {
92-
TokenResponse tokenResponse = (TokenResponse)stepContext.getResult();
91+
private CompletableFuture<DialogTurnResult> displayTokenPhase2(
92+
WaterfallStepContext stepContext
93+
) {
94+
TokenResponse tokenResponse = (TokenResponse) stepContext.getResult();
9395
if (tokenResponse != null) {
94-
stepContext.getContext().sendActivity(MessageFactory.text(
95-
String.format("Here instanceof your token %s", tokenResponse.getToken()
96-
)));
96+
stepContext.getContext().sendActivity(MessageFactory.text(
97+
String.format("Here instanceof your token %s", tokenResponse.getToken()
98+
)));
9799
}
98100

99-
return stepContext.endDialog();
101+
return stepContext.endDialog();
100102
}
101103
}
102104

0 commit comments

Comments
 (0)