/// <summary> /// we call for every turn while the topic is still active /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task <bool> ContinueTopic(AlarmBotContext context) { // for messages if (context.Activity.Type == ActivityTypes.Message) { switch (context.RecognizedIntents.TopIntent?.Name) { case "showAlarms": // allow show alarm to interrupt, but it's one turn, so we show the data without changing the topic await new ShowAlarmsTopic().StartTopic(context); return(await this.PromptForMissingData(context)); case "help": // show contextual help await AddAlarmResponses.ReplyWithHelp(context, this.Alarm); return(await this.PromptForMissingData(context)); case "cancel": // prompt to cancel await AddAlarmResponses.ReplyWithCancelPrompt(context, this.Alarm); this.TopicState = TopicStates.CancelConfirmation; return(true); default: return(await ProcessTopicState(context)); } } return(true); }
/// <summary> /// Shared method to get missing information /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task <bool> PromptForMissingData(ITurnContext context) { // If we don't have a title (or if its too long), prompt the user to get it. if (String.IsNullOrWhiteSpace(this.Alarm.Title)) { this.TopicState = TopicStates.TitlePrompt; await AddAlarmResponses.ReplyWithTitlePrompt(context, this.Alarm); return(true); } // if title exists but is not valid, then provide feedback and prompt again else if (this.Alarm.Title.Length < 1 || this.Alarm.Title.Length > 100) { this.Alarm.Title = null; this.TopicState = TopicStates.TitlePrompt; await AddAlarmResponses.ReplyWithTitleValidationPrompt(context, this.Alarm); return(true); } // If we don't have a time, prompt the user to get it. if (this.Alarm.Time == null) { this.TopicState = TopicStates.TimePrompt; await AddAlarmResponses.ReplyWithTimePromptFuture(context, this.Alarm); return(true); } // ask for confirmation that we want to add it await AddAlarmResponses.ReplyWithAddConfirmation(context, this.Alarm); this.TopicState = TopicStates.AddConfirmation; return(true); }
private async Task <bool> ProcessTopicState(AlarmBotContext context) { string utterance = (context.Activity.AsMessageActivity().Text ?? "").Trim(); // we ar eusing TopicState to remember what we last asked switch (this.TopicState) { case TopicStates.TitlePrompt: this.Alarm.Title = utterance; return(await this.PromptForMissingData(context)); case TopicStates.TimePrompt: // take first one in the future if a valid time has been parsed var times = context.GetDateTimes(); if (times.Any(t => t > DateTimeOffset.Now)) { this.Alarm.Time = times.FirstOrDefault(t => t > DateTimeOffset.Now); } return(await this.PromptForMissingData(context)); case TopicStates.CancelConfirmation: switch (context.RecognizedIntents.TopIntent?.Name) { case "confirmYes": await AddAlarmResponses.ReplyWithTopicCanceled(context); // End current topic return(false); case "confirmNo": // Re-prompt user for current field. await AddAlarmResponses.ReplyWithTopicCanceled(context); return(await this.PromptForMissingData(context)); default: // prompt again to confirm the cancelation await AddAlarmResponses.ReplyWithCancelReprompt(context, this.Alarm); return(true); } case TopicStates.AddConfirmation: switch (context.RecognizedIntents.TopIntent?.Name) { case "confirmYes": if (context.UserState.Alarms == null) { context.UserState.Alarms = new List <Alarm>(); } context.UserState.Alarms.Add(this.Alarm); await AddAlarmResponses.ReplyWithAddedAlarm(context, this.Alarm); // end topic return(false); case "confirmNo": await AddAlarmResponses.ReplyWithTopicCanceled(context); // End current topic return(false); default: return(await this.PromptForMissingData(context)); } default: return(await this.PromptForMissingData(context)); } }
private async Task <bool> ProcessTopicState(IBotContext context) { string utterance = (context.Request.AsMessageActivity().Text ?? "").Trim(); // we ar eusing TopicState to remember what we last asked switch (this.TopicState) { case TopicStates.TitlePrompt: this.Alarm.Title = utterance; return(await this.PromptForMissingData(context)); case TopicStates.TimePrompt: // take first one in the future this.Alarm.Time = ((BotContext)context).GetDateTimes().Where(t => t > DateTimeOffset.Now).FirstOrDefault(); return(await this.PromptForMissingData(context)); case TopicStates.CancelConfirmation: switch (context.TopIntent.Name) { case "confirmYes": AddAlarmResponses.ReplyWithTopicCanceled(context); // End current topic return(false); case "confirmNo": // Re-prompt user for current field. AddAlarmResponses.ReplyWithTopicCanceled(context); return(await this.PromptForMissingData(context)); default: // prompt again to confirm the cancelation AddAlarmResponses.ReplyWithCancelReprompt(context, this.Alarm); return(true); } case TopicStates.AddConfirmation: switch (context.TopIntent?.Name) { case "confirmYes": var alarms = (List <Alarm>)context.State.UserProperties[UserProperties.ALARMS]; if (alarms == null) { alarms = new List <Alarm>(); context.State.UserProperties[UserProperties.ALARMS] = alarms; } alarms.Add(this.Alarm); AddAlarmResponses.ReplyWithAddedAlarm(context, this.Alarm); // end topic return(false); case "confirmNo": AddAlarmResponses.ReplyWithTopicCanceled(context); // End current topic return(false); default: return(await this.PromptForMissingData(context)); } default: return(await this.PromptForMissingData(context)); } }