Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions samples/13.core-bot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
<artifactId>bot-dialogs</artifactId>
<version>4.6.0-preview9</version>
</dependency>
<dependency>
<groupId>com.microsoft.bot</groupId>
<artifactId>bot-ai-qna</artifactId>
<version>4.6.0-preview9</version>
</dependency>
<dependency>
<groupId>com.microsoft.bot</groupId>
<artifactId>bot-ai-luis-v3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import com.microsoft.bot.builder.Bot;
import com.microsoft.bot.builder.ConversationState;
import com.microsoft.bot.builder.Storage;
import com.microsoft.bot.builder.UserState;
import com.microsoft.bot.dialogs.Dialog;
import com.microsoft.bot.integration.AdapterWithErrorHandler;
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
import com.microsoft.bot.integration.Configuration;
Expand Down Expand Up @@ -36,8 +34,10 @@
* override methods in order to provide custom implementations.
*/
public class Application extends BotDependencyConfiguration {

/**
* The start method.
*
* @param args The args.
*/
public static void main(String[] args) {
Expand All @@ -48,56 +48,21 @@ public static void main(String[] args) {
* Returns the Bot for this application.
*
* <p>
* The @Component annotation could be used on the Bot class instead of this method
* with the @Bean annotation.
* The @Component annotation could be used on the Bot class instead of this method with the
* @Bean annotation.
* </p>
*
* @return The Bot implementation for this application.
*/
@Bean
public Bot getBot(
Configuration configuration,
UserState userState,
ConversationState conversationState
) {
Storage storage = this.getStorage();
UserState userState = this.getUserState(storage);
ConversationState conversationState = this.getConversationState(storage);
Dialog rootDialog = this.getRootDialog();
return new DialogAndWelcomeBot(conversationState, userState, rootDialog);
}

/**
* Returns a FlightBookingRecognizer object.
* @return The FlightBookingRecognizer.
*/
@Bean
public FlightBookingRecognizer getFlightBookingRecognizer() {
Configuration configuration = this.getConfiguration();
return new FlightBookingRecognizer(configuration);
}

/**
* Returns a BookingDialog object.
* @return The BookingDialog.
*/
@Bean
public BookingDialog getBookingDialog() {
return new BookingDialog();
}

/**
* Returns the starting Dialog for this application.
*
* <p>
* The @Component annotation could be used on the Dialog class instead of this method
* with the @Bean annotation.
* </p>
*
* @return The Dialog implementation for this application.
*/
@Bean
public Dialog getRootDialog() {
FlightBookingRecognizer flightBookingRecognizer = this.getFlightBookingRecognizer();
BookingDialog bookingDialog = this.getBookingDialog();
return new MainDialog(flightBookingRecognizer, bookingDialog);
FlightBookingRecognizer recognizer = new FlightBookingRecognizer(configuration);
MainDialog dialog = new MainDialog(recognizer, new BookingDialog());
return new DialogAndWelcomeBot<>(conversationState, userState, dialog);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
* The model class to retrieve the information of the booking.
*/
public class BookingDetails {

private String destination;
private String origin;
private String travelDate;

/**
* Gets the destination of the booking.
*
* @return The destination.
*/
public String getDestination() {
Expand All @@ -22,6 +24,7 @@ public String getDestination() {

/**
* Sets the destination of the booking.
*
* @param withDestination The new destination.
*/
public void setDestination(String withDestination) {
Expand All @@ -30,6 +33,7 @@ public void setDestination(String withDestination) {

/**
* Gets the origin of the booking.
*
* @return The origin.
*/
public String getOrigin() {
Expand All @@ -38,6 +42,7 @@ public String getOrigin() {

/**
* Sets the origin of the booking.
*
* @param withOrigin The new origin.
*/
public void setOrigin(String withOrigin) {
Expand All @@ -46,6 +51,7 @@ public void setOrigin(String withOrigin) {

/**
* Gets the travel date of the booking.
*
* @return The travel date.
*/
public String getTravelDate() {
Expand All @@ -54,6 +60,7 @@ public String getTravelDate() {

/**
* Sets the travel date of the booking.
*
* @param withTravelDate The new travel date.
*/
public void setTravelDate(String withTravelDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* The class containing the booking dialogs.
*/
public class BookingDialog extends CancelAndHelpDialog {

private final String destinationStepMsgText = "Where would you like to travel to?";
private final String originStepMsgText = "Where are you traveling from?";

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

if (bookingDetails.getDestination().isEmpty()) {
Activity promptMessage =
MessageFactory.text(destinationStepMsgText, destinationStepMsgText, InputHints.EXPECTING_INPUT);
MessageFactory.text(destinationStepMsgText, destinationStepMsgText,
InputHints.EXPECTING_INPUT
);
PromptOptions promptOptions = new PromptOptions();
promptOptions.setPrompt(promptMessage);
return stepContext.prompt("TextPrompt", promptOptions);
Expand All @@ -71,7 +74,8 @@ private CompletableFuture<DialogTurnResult> originStep(WaterfallStepContext step

if (bookingDetails.getOrigin().isEmpty()) {
Activity promptMessage =
MessageFactory.text(originStepMsgText, originStepMsgText, InputHints.EXPECTING_INPUT);
MessageFactory
.text(originStepMsgText, originStepMsgText, InputHints.EXPECTING_INPUT);
PromptOptions promptOptions = new PromptOptions();
promptOptions.setPrompt(promptMessage);
return stepContext.prompt("TextPrompt", promptOptions);
Expand Down Expand Up @@ -100,9 +104,13 @@ private CompletableFuture<DialogTurnResult> confirmStep(WaterfallStepContext ste
bookingDetails.setTravelDate(stepContext.getResult().toString());

String messageText =
String.format("Please confirm, I have you traveling to: %s from: %s on: %s. Is this correct?",
bookingDetails.getDestination(), bookingDetails.getOrigin(), bookingDetails.getTravelDate());
Activity promptMessage = MessageFactory.text(messageText, messageText, InputHints.EXPECTING_INPUT);
String.format(
"Please confirm, I have you traveling to: %s from: %s on: %s. Is this correct?",
bookingDetails.getDestination(), bookingDetails.getOrigin(),
bookingDetails.getTravelDate()
);
Activity promptMessage = MessageFactory
.text(messageText, messageText, InputHints.EXPECTING_INPUT);

PromptOptions promptOptions = new PromptOptions();
promptOptions.setPrompt(promptMessage);
Expand All @@ -114,7 +122,6 @@ private CompletableFuture<DialogTurnResult> confirmStep(WaterfallStepContext ste
private CompletableFuture<DialogTurnResult> finalStep(WaterfallStepContext stepContext) {
if ((Boolean) stepContext.getResult()) {
BookingDetails bookingDetails = (BookingDetails) stepContext.getOptions();

return stepContext.endDialog(bookingDetails);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@
* The class in charge of the dialog interruptions.
*/
public class CancelAndHelpDialog extends ComponentDialog {

private final String helpMsgText = "Show help here";
private final String cancelMsgText = "Cancelling...";

/**
* The constructor of the CancelAndHelpDialog class.
*
* @param id The dialog's Id.
*/
public CancelAndHelpDialog(String id) {
super(id);
}

/**
* Called when the dialog is _continued_, where it is the active dialog and the
* user replies with a new activity.
* Called when the dialog is _continued_, where it is the active dialog and the user replies
* with a new activity.
*
* @param innerDc innerDc The inner {@link DialogContext} for the current turn of conversation.
* @return A {@link CompletableFuture} representing the asynchronous operation.
* If the task is successful, the result indicates whether the dialog is
* still active after the turn has been processed by the dialog. The
* result may also contain a return value.
* @return A {@link CompletableFuture} representing the asynchronous operation. If the task is
* successful, the result indicates whether the dialog is still active after the turn has been
* processed by the dialog. The result may also contain a return value.
*/
@Override
protected CompletableFuture<DialogTurnResult> onContinueDialog(DialogContext innerDc) {
Expand All @@ -55,16 +57,19 @@ private CompletableFuture<DialogTurnResult> interrupt(DialogContext innerDc) {
switch (text) {
case "help":
case "?":
Activity helpMessage = MessageFactory.text(helpMsgText, helpMsgText, InputHints.EXPECTING_INPUT);
Activity helpMessage = MessageFactory
.text(helpMsgText, helpMsgText, InputHints.EXPECTING_INPUT);
return innerDc.getContext().sendActivity(helpMessage)
.thenCompose(sendResult ->
CompletableFuture.completedFuture(new DialogTurnResult(DialogTurnStatus.WAITING)));
CompletableFuture
.completedFuture(new DialogTurnResult(DialogTurnStatus.WAITING)));
case "cancel":
case "quit":
Activity cancelMessage = MessageFactory
.text(cancelMsgText, cancelMsgText, InputHints.IGNORING_INPUT);
return innerDc.getContext()
.sendActivity(cancelMessage).thenCompose(sendResult -> innerDc.cancelAllDialogs());
.sendActivity(cancelMessage)
.thenCompose(sendResult -> innerDc.cancelAllDialogs());
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ private CompletableFuture<DialogTurnResult> initialStep(WaterfallStepContext ste
return stepContext.prompt("DateTimePrompt", promptOptions);
}

DateTimeResolution dateTimeResolution = new DateTimeResolution() {
{
setTimex(timex);
}
};
List<DateTimeResolution> dateTimeResolutions = new ArrayList<DateTimeResolution>() {
{
add(dateTimeResolution);
}
};
DateTimeResolution dateTimeResolution = new DateTimeResolution() {{
setTimex(timex);
}};
List<DateTimeResolution> dateTimeResolutions = new ArrayList<DateTimeResolution>() {{
add(dateTimeResolution);
}};
return stepContext.next(dateTimeResolutions);
}

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

private static CompletableFuture<Boolean> dateTimePromptValidator(PromptValidatorContext<List<DateTimeResolution>>
promptContext) {
private static CompletableFuture<Boolean> dateTimePromptValidator(
PromptValidatorContext<List<DateTimeResolution>> promptContext
) {
if (promptContext.getRecognized().getSucceeded()) {
// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the
// Time part. TIMEX is a format that represents DateTime expressions that include some ambiguity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,52 @@

/**
* The class containing the welcome dialog.
*
* @param <T> is a Dialog.
*/
public class DialogAndWelcomeBot<T extends Dialog> extends DialogBot {

/**
* Creates a DialogBot.
*
* @param withConversationState ConversationState to use in the bot
* @param withUserState UserState to use
* @param withDialog Param inheriting from Dialog class
*/
public DialogAndWelcomeBot(ConversationState withConversationState, UserState withUserState, T withDialog) {
public DialogAndWelcomeBot(
ConversationState withConversationState, UserState withUserState, T withDialog
) {
super(withConversationState, withUserState, withDialog);
}

/**
* When the {@link #onConversationUpdateActivity(TurnContext)} method receives a
* conversation update activity that indicates one or more users other than the
* bot are joining the conversation, it calls this method.
* @param membersAdded A list of all the members added to the conversation,
* as described by the conversation update activity
* @param turnContext The context object for this turn.
* When the {@link #onConversationUpdateActivity(TurnContext)} method receives a conversation
* update activity that indicates one or more users other than the bot are joining the
* conversation, it calls this method.
*
* @param membersAdded A list of all the members added to the conversation, as described by the
* conversation update activity
* @param turnContext The context object for this turn.
* @return A task that represents the work queued to execute.
*/
@Override
protected CompletableFuture<Void> onMembersAdded(List<ChannelAccount> membersAdded, TurnContext turnContext) {
protected CompletableFuture<Void> onMembersAdded(
List<ChannelAccount> membersAdded, TurnContext turnContext
) {
return turnContext.getActivity().getMembersAdded().stream()
.filter(member -> !StringUtils
.equals(member.getId(), turnContext.getActivity().getRecipient().getId()))
.map(channel -> {
// Greet anyone that was not the target (recipient) of this message.
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
Attachment welcomeCard = createAdaptiveCardAttachment();
Activity response = MessageFactory.attachment(welcomeCard, null, "Welcome to Bot Framework!", null);
Activity response = MessageFactory
.attachment(welcomeCard, null, "Welcome to Bot Framework!", null);

return turnContext.sendActivity(response).thenApply(sendResult -> {
return Dialog.run(getDialog(), turnContext, getConversationState().createProperty("DialogState"));
return Dialog.run(getDialog(), turnContext,
getConversationState().createProperty("DialogState")
);
});
})
.collect(CompletableFutures.toFutureList())
Expand All @@ -67,14 +78,17 @@ protected CompletableFuture<Void> onMembersAdded(List<ChannelAccount> membersAdd

// Load attachment from embedded resource.
private Attachment createAdaptiveCardAttachment() {
try (InputStream inputStream = Thread.currentThread().
getContextClassLoader().getResourceAsStream("cards/welcomeCard.json")) {
String adaptiveCardJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8.toString());
try (
InputStream inputStream = Thread.currentThread().
getContextClassLoader().getResourceAsStream("cards/welcomeCard.json")
) {
String adaptiveCardJson = IOUtils
.toString(inputStream, StandardCharsets.UTF_8.toString());

return new Attachment() {{
setContentType("application/vnd.microsoft.card.adaptive");
setContent(Serialization.jsonToTree(adaptiveCardJson));
}};
return new Attachment() {{
setContentType("application/vnd.microsoft.card.adaptive");
setContent(Serialization.jsonToTree(adaptiveCardJson));
}};

} catch (IOException e) {
e.printStackTrace();
Expand Down
Loading