public override Task OnReceiveActivity(IBotContext context) { if ((context.Request.Type == ActivityTypes.Message) && (context.Request.AsMessageActivity().Text.Length > 0)) { var message = context.Request.AsMessageActivity(); // If the user wants to change the topic of conversation... if (message.Text.ToLowerInvariant() == "add alarm") { // Set the active topic and let the active topic handle this turn. this.SetActiveTopic(ADD_ALARM_TOPIC) .OnReceiveActivity(context); return(Task.CompletedTask); } if (message.Text.ToLowerInvariant() == "delete alarm") { this.SetActiveTopic(DELETE_ALARM_TOPIC) .OnReceiveActivity(context); return(Task.CompletedTask); } if (message.Text.ToLowerInvariant() == "show alarms") { this.ClearActiveTopic(); AlarmsView.ShowAlarms(context, context.State.UserProperties[USER_STATE_ALARMS]); return(Task.CompletedTask); } if (message.Text.ToLowerInvariant() == "help") { this.ClearActiveTopic(); this.ShowHelp(context); return(Task.CompletedTask); } // If there is an active topic, let it handle this turn until it completes. if (HasActiveTopic) { ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } ShowDefaultMessage(context); } return(Task.CompletedTask); }
public override Task OnReceiveActivity(IBotContext context) { if (HasActiveTopic) { ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } // If there are no alarms to delete... if (this.State.Alarms.Count == 0) { context.SendActivity("There are no alarms to delete."); return(Task.CompletedTask); } if (this.State.AlarmIndex == null) { // If there is only one alarm to delete, use that index. No need to prompt. if (this.State.Alarms.Count == 1) { AlarmsView.ShowAlarms(context, this.State.Alarms); this.State.AlarmIndex = 0; } else { this.SetActiveTopic(WHICH_ALARM_PROMPT) .OnReceiveActivity(context); return(Task.CompletedTask); } } this.State.Alarm.Title = this.State.Alarms[(int)this.State.AlarmIndex].Title; if (this.State.DeleteConfirmed == null) { this.SetActiveTopic(CONFIRM_DELETE_PROMPT) .OnReceiveActivity(context); return(Task.CompletedTask); } this.OnSuccess(context, new DeleteAlarmTopicValue { Alarm = this.State.Alarm, AlarmIndex = (int)this.State.AlarmIndex, DeleteConfirmed = (bool)this.State.DeleteConfirmed }); return(Task.CompletedTask); }
public override Task OnTurn(ITurnContext turnContext) { if ((turnContext.Activity.Type == ActivityTypes.Message) && (turnContext.Activity.Text.Length > 0)) { var message = turnContext.Activity; // If the user wants to change the topic of conversation... if (message.Text.ToLowerInvariant() == "add alarm") { // Set the active topic and let the active topic handle this turn. return(SetActiveTopic(ADD_ALARM_TOPIC) .OnTurn(turnContext)); } if (message.Text.ToLowerInvariant() == "delete alarm") { return(SetActiveTopic(DELETE_ALARM_TOPIC, turnContext.GetUserState <BotUserState>().Alarms) .OnTurn(turnContext)); } if (message.Text.ToLowerInvariant() == "show alarms") { ClearActiveTopic(); AlarmsView.ShowAlarms(turnContext, turnContext.GetUserState <BotUserState>().Alarms); return(Task.CompletedTask); } if (message.Text.ToLowerInvariant() == "help") { ClearActiveTopic(); return(ShowHelp(turnContext)); } // If there is an active topic, let it handle this turn until it completes. if (HasActiveTopic) { return(ActiveTopic.OnTurn(turnContext)); } return(ShowDefaultMessage(turnContext)); } return(Task.CompletedTask); }
public DeleteAlarmTopic(List <Alarm> alarms) : base() { if (alarms != null) { this._state.Alarms = alarms; } this.SubTopics.Add(WHICH_ALARM_PROMPT, (object[] args) => { var whichAlarmPrompt = new Prompt <int>(); whichAlarmPrompt.Set .OnPrompt((context, lastTurnReason) => { if ((lastTurnReason != null) && (lastTurnReason == "indexnotfound")) { context.SendActivity($"Sorry, I coulnd't find an alarm named '{ context.Request.AsMessageActivity().Text }'.", "Let's try again."); } AlarmsView.ShowAlarms(context, this._state.Alarms); context.SendActivity("Which alarm would you like to delete?"); }) .Validator(new AlarmIndexValidator(this._state.Alarms)) .MaxTurns(2) .OnSuccess((context, index) => { this.ClearActiveTopic(); this.State.AlarmIndex = index; this.OnReceiveActivity(context); }) .OnFailure((context, reason) => { this.ClearActiveTopic(); if ((reason != null) && (reason == "toomanyattempts")) { context.SendActivity("I'm sorry I'm having issues understanding you."); } this.OnFailure(context, reason); }); return(whichAlarmPrompt); }); this.SubTopics.Add(CONFIRM_DELETE_PROMPT, (object[] args) => { var confirmDeletePrompt = new Prompt <bool>(); confirmDeletePrompt.Set .OnPrompt((context, lastTurnReason) => { if ((lastTurnReason != null) & (lastTurnReason == "notyesorno")) { context.SendActivity("Sorry, I was expecting 'yes' or 'no'.", "Let's try again."); } context.SendActivity($"Are you sure you want to delete alarm '{ this.State.Alarm.Title }' ('yes' or 'no')?`"); }) .Validator(new YesOrNoValidator()) .MaxTurns(2) .OnSuccess((context, value) => { this.ClearActiveTopic(); this.State.DeleteConfirmed = value; this.OnReceiveActivity(context); }) .OnFailure((context, reason) => { this.ClearActiveTopic(); if ((reason != null) && (reason == "toomanyattempts")) { context.SendActivity("I'm sorry I'm having issues understanding you."); } this.OnFailure(context, reason); }); return(confirmDeletePrompt); }); }