Created
March 2, 2022 23:49
-
-
Save sunnyy02/33d508bb89bdd8f71896f02c3a682691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppointmentBookingDialog : CancelAndHelpDialog | |
{ | |
private const string DoctorStepMsgText = "Who would you like to see?"; | |
public AppointmentBookingDialog() | |
: base(nameof(AppointmentBookingDialog)) | |
{ | |
AddDialog(new TextPrompt(nameof(TextPrompt))); | |
AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt))); | |
AddDialog(new DateResolverDialog()); | |
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[] | |
{ | |
DoctorStepAsync, | |
AppointmentDateStepAsync, | |
ConfirmStepAsync, | |
FinalStepAsync, | |
})); | |
// The initial child Dialog to run. | |
InitialDialogId = nameof(WaterfallDialog); | |
} | |
private async Task<DialogTurnResult>DoctorStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) | |
{ | |
var bookingDetails = (AppointmentDetails)stepContext.Options; | |
if (bookingDetails.Doctor == null) | |
{ | |
var promptMessage = MessageFactory.Text(DoctorStepMsgText, DoctorStepMsgText, InputHints.ExpectingInput); | |
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = promptMessage }, cancellationToken); | |
} | |
return await stepContext.NextAsync(bookingDetails.Doctor, cancellationToken); | |
} | |
private async Task<DialogTurnResult> AppointmentDateStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) | |
{ | |
var bookingDetails = (AppointmentDetails)stepContext.Options; | |
bookingDetails.AppointmentDate = (string)stepContext.Result; | |
if (bookingDetails.AppointmentDate == null || IsAmbiguous(bookingDetails.AppointmentDate)) | |
{ | |
return await stepContext.BeginDialogAsync(nameof(DateResolverDialog), bookingDetails.AppointmentDate, cancellationToken); | |
} | |
return await stepContext.NextAsync(bookingDetails.AppointmentDate, cancellationToken); | |
} | |
private async Task<DialogTurnResult> ConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) | |
{ | |
var bookingDetails = (AppointmentDetails)stepContext.Options; | |
bookingDetails.AppointmentDate = (string)stepContext.Result; | |
var messageText = $"Please confirm, I have you book with Doctor: {bookingDetails.Doctor} on: {bookingDetails.AppointmentDate}. Is this correct?"; | |
var promptMessage = MessageFactory.Text(messageText, messageText, InputHints.ExpectingInput); | |
return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = promptMessage }, cancellationToken); | |
} | |
private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) | |
{ | |
if ((bool)stepContext.Result) | |
{ | |
var bookingDetails = (AppointmentDetails)stepContext.Options; | |
return await stepContext.EndDialogAsync(bookingDetails, cancellationToken); | |
} | |
return await stepContext.EndDialogAsync(null, cancellationToken); | |
} | |
private static bool IsAmbiguous(string timex) | |
{ | |
var timexProperty = new TimexProperty(timex); | |
return !timexProperty.Types.Contains(Constants.TimexTypes.Definite); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment