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

Commit 2e577c2

Browse files
authored
Core Bot formatting (#1045)
* Core Bot formatting * Simplified Core Bot Appliation DI
1 parent 3b23c78 commit 2e577c2

File tree

10 files changed

+199
-161
lines changed

10 files changed

+199
-161
lines changed

samples/13.core-bot/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@
8989
<artifactId>bot-dialogs</artifactId>
9090
<version>4.6.0-preview9</version>
9191
</dependency>
92-
<dependency>
93-
<groupId>com.microsoft.bot</groupId>
94-
<artifactId>bot-ai-qna</artifactId>
95-
<version>4.6.0-preview9</version>
96-
</dependency>
9792
<dependency>
9893
<groupId>com.microsoft.bot</groupId>
9994
<artifactId>bot-ai-luis-v3</artifactId>

samples/13.core-bot/src/main/java/com/microsoft/bot/sample/core/Application.java

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
import com.microsoft.bot.builder.Bot;
77
import com.microsoft.bot.builder.ConversationState;
8-
import com.microsoft.bot.builder.Storage;
98
import com.microsoft.bot.builder.UserState;
10-
import com.microsoft.bot.dialogs.Dialog;
119
import com.microsoft.bot.integration.AdapterWithErrorHandler;
1210
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
1311
import com.microsoft.bot.integration.Configuration;
@@ -36,8 +34,10 @@
3634
* override methods in order to provide custom implementations.
3735
*/
3836
public class Application extends BotDependencyConfiguration {
37+
3938
/**
4039
* The start method.
40+
*
4141
* @param args The args.
4242
*/
4343
public static void main(String[] args) {
@@ -48,56 +48,21 @@ public static void main(String[] args) {
4848
* Returns the Bot for this application.
4949
*
5050
* <p>
51-
* The @Component annotation could be used on the Bot class instead of this method
52-
* with the @Bean annotation.
51+
* The @Component annotation could be used on the Bot class instead of this method with the
52+
* @Bean annotation.
5353
* </p>
5454
*
5555
* @return The Bot implementation for this application.
5656
*/
5757
@Bean
5858
public Bot getBot(
59+
Configuration configuration,
60+
UserState userState,
61+
ConversationState conversationState
5962
) {
60-
Storage storage = this.getStorage();
61-
UserState userState = this.getUserState(storage);
62-
ConversationState conversationState = this.getConversationState(storage);
63-
Dialog rootDialog = this.getRootDialog();
64-
return new DialogAndWelcomeBot(conversationState, userState, rootDialog);
65-
}
66-
67-
/**
68-
* Returns a FlightBookingRecognizer object.
69-
* @return The FlightBookingRecognizer.
70-
*/
71-
@Bean
72-
public FlightBookingRecognizer getFlightBookingRecognizer() {
73-
Configuration configuration = this.getConfiguration();
74-
return new FlightBookingRecognizer(configuration);
75-
}
76-
77-
/**
78-
* Returns a BookingDialog object.
79-
* @return The BookingDialog.
80-
*/
81-
@Bean
82-
public BookingDialog getBookingDialog() {
83-
return new BookingDialog();
84-
}
85-
86-
/**
87-
* Returns the starting Dialog for this application.
88-
*
89-
* <p>
90-
* The @Component annotation could be used on the Dialog class instead of this method
91-
* with the @Bean annotation.
92-
* </p>
93-
*
94-
* @return The Dialog implementation for this application.
95-
*/
96-
@Bean
97-
public Dialog getRootDialog() {
98-
FlightBookingRecognizer flightBookingRecognizer = this.getFlightBookingRecognizer();
99-
BookingDialog bookingDialog = this.getBookingDialog();
100-
return new MainDialog(flightBookingRecognizer, bookingDialog);
63+
FlightBookingRecognizer recognizer = new FlightBookingRecognizer(configuration);
64+
MainDialog dialog = new MainDialog(recognizer, new BookingDialog());
65+
return new DialogAndWelcomeBot<>(conversationState, userState, dialog);
10166
}
10267

10368
/**

samples/13.core-bot/src/main/java/com/microsoft/bot/sample/core/BookingDetails.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
* The model class to retrieve the information of the booking.
88
*/
99
public class BookingDetails {
10+
1011
private String destination;
1112
private String origin;
1213
private String travelDate;
1314

1415
/**
1516
* Gets the destination of the booking.
17+
*
1618
* @return The destination.
1719
*/
1820
public String getDestination() {
@@ -22,6 +24,7 @@ public String getDestination() {
2224

2325
/**
2426
* Sets the destination of the booking.
27+
*
2528
* @param withDestination The new destination.
2629
*/
2730
public void setDestination(String withDestination) {
@@ -30,6 +33,7 @@ public void setDestination(String withDestination) {
3033

3134
/**
3235
* Gets the origin of the booking.
36+
*
3337
* @return The origin.
3438
*/
3539
public String getOrigin() {
@@ -38,6 +42,7 @@ public String getOrigin() {
3842

3943
/**
4044
* Sets the origin of the booking.
45+
*
4146
* @param withOrigin The new origin.
4247
*/
4348
public void setOrigin(String withOrigin) {
@@ -46,6 +51,7 @@ public void setOrigin(String withOrigin) {
4651

4752
/**
4853
* Gets the travel date of the booking.
54+
*
4955
* @return The travel date.
5056
*/
5157
public String getTravelDate() {
@@ -54,6 +60,7 @@ public String getTravelDate() {
5460

5561
/**
5662
* Sets the travel date of the booking.
63+
*
5764
* @param withTravelDate The new travel date.
5865
*/
5966
public void setTravelDate(String withTravelDate) {

samples/13.core-bot/src/main/java/com/microsoft/bot/sample/core/BookingDialog.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* The class containing the booking dialogs.
2424
*/
2525
public class BookingDialog extends CancelAndHelpDialog {
26+
2627
private final String destinationStepMsgText = "Where would you like to travel to?";
2728
private final String originStepMsgText = "Where are you traveling from?";
2829

@@ -54,7 +55,9 @@ private CompletableFuture<DialogTurnResult> destinationStep(WaterfallStepContext
5455

5556
if (bookingDetails.getDestination().isEmpty()) {
5657
Activity promptMessage =
57-
MessageFactory.text(destinationStepMsgText, destinationStepMsgText, InputHints.EXPECTING_INPUT);
58+
MessageFactory.text(destinationStepMsgText, destinationStepMsgText,
59+
InputHints.EXPECTING_INPUT
60+
);
5861
PromptOptions promptOptions = new PromptOptions();
5962
promptOptions.setPrompt(promptMessage);
6063
return stepContext.prompt("TextPrompt", promptOptions);
@@ -71,7 +74,8 @@ private CompletableFuture<DialogTurnResult> originStep(WaterfallStepContext step
7174

7275
if (bookingDetails.getOrigin().isEmpty()) {
7376
Activity promptMessage =
74-
MessageFactory.text(originStepMsgText, originStepMsgText, InputHints.EXPECTING_INPUT);
77+
MessageFactory
78+
.text(originStepMsgText, originStepMsgText, InputHints.EXPECTING_INPUT);
7579
PromptOptions promptOptions = new PromptOptions();
7680
promptOptions.setPrompt(promptMessage);
7781
return stepContext.prompt("TextPrompt", promptOptions);
@@ -100,9 +104,13 @@ private CompletableFuture<DialogTurnResult> confirmStep(WaterfallStepContext ste
100104
bookingDetails.setTravelDate(stepContext.getResult().toString());
101105

102106
String messageText =
103-
String.format("Please confirm, I have you traveling to: %s from: %s on: %s. Is this correct?",
104-
bookingDetails.getDestination(), bookingDetails.getOrigin(), bookingDetails.getTravelDate());
105-
Activity promptMessage = MessageFactory.text(messageText, messageText, InputHints.EXPECTING_INPUT);
107+
String.format(
108+
"Please confirm, I have you traveling to: %s from: %s on: %s. Is this correct?",
109+
bookingDetails.getDestination(), bookingDetails.getOrigin(),
110+
bookingDetails.getTravelDate()
111+
);
112+
Activity promptMessage = MessageFactory
113+
.text(messageText, messageText, InputHints.EXPECTING_INPUT);
106114

107115
PromptOptions promptOptions = new PromptOptions();
108116
promptOptions.setPrompt(promptMessage);
@@ -114,7 +122,6 @@ private CompletableFuture<DialogTurnResult> confirmStep(WaterfallStepContext ste
114122
private CompletableFuture<DialogTurnResult> finalStep(WaterfallStepContext stepContext) {
115123
if ((Boolean) stepContext.getResult()) {
116124
BookingDetails bookingDetails = (BookingDetails) stepContext.getOptions();
117-
118125
return stepContext.endDialog(bookingDetails);
119126
}
120127

samples/13.core-bot/src/main/java/com/microsoft/bot/sample/core/CancelAndHelpDialog.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,27 @@
1818
* The class in charge of the dialog interruptions.
1919
*/
2020
public class CancelAndHelpDialog extends ComponentDialog {
21+
2122
private final String helpMsgText = "Show help here";
2223
private final String cancelMsgText = "Cancelling...";
2324

2425
/**
2526
* The constructor of the CancelAndHelpDialog class.
27+
*
2628
* @param id The dialog's Id.
2729
*/
2830
public CancelAndHelpDialog(String id) {
2931
super(id);
3032
}
3133

3234
/**
33-
* Called when the dialog is _continued_, where it is the active dialog and the
34-
* user replies with a new activity.
35+
* Called when the dialog is _continued_, where it is the active dialog and the user replies
36+
* with a new activity.
37+
*
3538
* @param innerDc innerDc The inner {@link DialogContext} for the current turn of conversation.
36-
* @return A {@link CompletableFuture} representing the asynchronous operation.
37-
* If the task is successful, the result indicates whether the dialog is
38-
* still active after the turn has been processed by the dialog. The
39-
* result may also contain a return value.
39+
* @return A {@link CompletableFuture} representing the asynchronous operation. If the task is
40+
* successful, the result indicates whether the dialog is still active after the turn has been
41+
* processed by the dialog. The result may also contain a return value.
4042
*/
4143
@Override
4244
protected CompletableFuture<DialogTurnResult> onContinueDialog(DialogContext innerDc) {
@@ -55,16 +57,19 @@ private CompletableFuture<DialogTurnResult> interrupt(DialogContext innerDc) {
5557
switch (text) {
5658
case "help":
5759
case "?":
58-
Activity helpMessage = MessageFactory.text(helpMsgText, helpMsgText, InputHints.EXPECTING_INPUT);
60+
Activity helpMessage = MessageFactory
61+
.text(helpMsgText, helpMsgText, InputHints.EXPECTING_INPUT);
5962
return innerDc.getContext().sendActivity(helpMessage)
6063
.thenCompose(sendResult ->
61-
CompletableFuture.completedFuture(new DialogTurnResult(DialogTurnStatus.WAITING)));
64+
CompletableFuture
65+
.completedFuture(new DialogTurnResult(DialogTurnStatus.WAITING)));
6266
case "cancel":
6367
case "quit":
6468
Activity cancelMessage = MessageFactory
6569
.text(cancelMsgText, cancelMsgText, InputHints.IGNORING_INPUT);
6670
return innerDc.getContext()
67-
.sendActivity(cancelMessage).thenCompose(sendResult -> innerDc.cancelAllDialogs());
71+
.sendActivity(cancelMessage)
72+
.thenCompose(sendResult -> innerDc.cancelAllDialogs());
6873
default:
6974
break;
7075
}

samples/13.core-bot/src/main/java/com/microsoft/bot/sample/core/DateResolverDialog.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,12 @@ private CompletableFuture<DialogTurnResult> initialStep(WaterfallStepContext ste
7575
return stepContext.prompt("DateTimePrompt", promptOptions);
7676
}
7777

78-
DateTimeResolution dateTimeResolution = new DateTimeResolution() {
79-
{
80-
setTimex(timex);
81-
}
82-
};
83-
List<DateTimeResolution> dateTimeResolutions = new ArrayList<DateTimeResolution>() {
84-
{
85-
add(dateTimeResolution);
86-
}
87-
};
78+
DateTimeResolution dateTimeResolution = new DateTimeResolution() {{
79+
setTimex(timex);
80+
}};
81+
List<DateTimeResolution> dateTimeResolutions = new ArrayList<DateTimeResolution>() {{
82+
add(dateTimeResolution);
83+
}};
8884
return stepContext.next(dateTimeResolutions);
8985
}
9086

@@ -93,8 +89,9 @@ private CompletableFuture<DialogTurnResult> finalStep(WaterfallStepContext stepC
9389
return stepContext.endDialog(timex);
9490
}
9591

96-
private static CompletableFuture<Boolean> dateTimePromptValidator(PromptValidatorContext<List<DateTimeResolution>>
97-
promptContext) {
92+
private static CompletableFuture<Boolean> dateTimePromptValidator(
93+
PromptValidatorContext<List<DateTimeResolution>> promptContext
94+
) {
9895
if (promptContext.getRecognized().getSucceeded()) {
9996
// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the
10097
// Time part. TIMEX is a format that represents DateTime expressions that include some ambiguity.

samples/13.core-bot/src/main/java/com/microsoft/bot/sample/core/DialogAndWelcomeBot.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,52 @@
2424

2525
/**
2626
* The class containing the welcome dialog.
27+
*
2728
* @param <T> is a Dialog.
2829
*/
2930
public class DialogAndWelcomeBot<T extends Dialog> extends DialogBot {
31+
3032
/**
3133
* Creates a DialogBot.
34+
*
3235
* @param withConversationState ConversationState to use in the bot
3336
* @param withUserState UserState to use
3437
* @param withDialog Param inheriting from Dialog class
3538
*/
36-
public DialogAndWelcomeBot(ConversationState withConversationState, UserState withUserState, T withDialog) {
39+
public DialogAndWelcomeBot(
40+
ConversationState withConversationState, UserState withUserState, T withDialog
41+
) {
3742
super(withConversationState, withUserState, withDialog);
3843
}
3944

4045
/**
41-
* When the {@link #onConversationUpdateActivity(TurnContext)} method receives a
42-
* conversation update activity that indicates one or more users other than the
43-
* bot are joining the conversation, it calls this method.
44-
* @param membersAdded A list of all the members added to the conversation,
45-
* as described by the conversation update activity
46-
* @param turnContext The context object for this turn.
46+
* When the {@link #onConversationUpdateActivity(TurnContext)} method receives a conversation
47+
* update activity that indicates one or more users other than the bot are joining the
48+
* conversation, it calls this method.
49+
*
50+
* @param membersAdded A list of all the members added to the conversation, as described by the
51+
* conversation update activity
52+
* @param turnContext The context object for this turn.
4753
* @return A task that represents the work queued to execute.
4854
*/
4955
@Override
50-
protected CompletableFuture<Void> onMembersAdded(List<ChannelAccount> membersAdded, TurnContext turnContext) {
56+
protected CompletableFuture<Void> onMembersAdded(
57+
List<ChannelAccount> membersAdded, TurnContext turnContext
58+
) {
5159
return turnContext.getActivity().getMembersAdded().stream()
5260
.filter(member -> !StringUtils
5361
.equals(member.getId(), turnContext.getActivity().getRecipient().getId()))
5462
.map(channel -> {
5563
// Greet anyone that was not the target (recipient) of this message.
5664
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
5765
Attachment welcomeCard = createAdaptiveCardAttachment();
58-
Activity response = MessageFactory.attachment(welcomeCard, null, "Welcome to Bot Framework!", null);
66+
Activity response = MessageFactory
67+
.attachment(welcomeCard, null, "Welcome to Bot Framework!", null);
5968

6069
return turnContext.sendActivity(response).thenApply(sendResult -> {
61-
return Dialog.run(getDialog(), turnContext, getConversationState().createProperty("DialogState"));
70+
return Dialog.run(getDialog(), turnContext,
71+
getConversationState().createProperty("DialogState")
72+
);
6273
});
6374
})
6475
.collect(CompletableFutures.toFutureList())
@@ -67,14 +78,17 @@ protected CompletableFuture<Void> onMembersAdded(List<ChannelAccount> membersAdd
6778

6879
// Load attachment from embedded resource.
6980
private Attachment createAdaptiveCardAttachment() {
70-
try (InputStream inputStream = Thread.currentThread().
71-
getContextClassLoader().getResourceAsStream("cards/welcomeCard.json")) {
72-
String adaptiveCardJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8.toString());
81+
try (
82+
InputStream inputStream = Thread.currentThread().
83+
getContextClassLoader().getResourceAsStream("cards/welcomeCard.json")
84+
) {
85+
String adaptiveCardJson = IOUtils
86+
.toString(inputStream, StandardCharsets.UTF_8.toString());
7387

74-
return new Attachment() {{
75-
setContentType("application/vnd.microsoft.card.adaptive");
76-
setContent(Serialization.jsonToTree(adaptiveCardJson));
77-
}};
88+
return new Attachment() {{
89+
setContentType("application/vnd.microsoft.card.adaptive");
90+
setContent(Serialization.jsonToTree(adaptiveCardJson));
91+
}};
7892

7993
} catch (IOException e) {
8094
e.printStackTrace();

0 commit comments

Comments
 (0)