/// <summary> /// Ask if confirm to delete the To Do task sub step. /// </summary> /// <param name="sc">current step context.</param> /// <param name="cancellationToken">cancellation token.</param> /// <returns>Task completion.</returns> public async Task <DialogTurnResult> AskDeletionConfirmation(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken)) { try { var state = await this.accessors.ToDoSkillState.GetAsync(sc.Context); if (state.MarkOrDeleteAllTasksFlag) { var prompt = sc.Context.Activity.CreateReply(ToDoBotResponses.AskDeletionAllConfirmation); return(await sc.PromptAsync(Action.Prompt, new PromptOptions() { Prompt = prompt })); } else { var toDoTask = state.ToDoTaskAllActivities[state.ToDoTaskIndexes[0]].Topic; var token = new StringDictionary() { { "toDoTask", toDoTask } }; var response = ToDoHelper.GenerateResponseWithTokens(ToDoBotResponses.AskDeletionConfirmation, token); var prompt = sc.Context.Activity.CreateReply(response); prompt.Speak = response; return(await sc.PromptAsync(Action.Prompt, new PromptOptions() { Prompt = prompt })); } } catch (Exception ex) { await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ex.Message)); await this.accessors.ToDoSkillState.SetAsync(sc.Context, new ToDoSkillState()); return(await sc.CancelAllDialogsAsync()); } }
/// <summary> /// Delete To Do task step. /// </summary> /// <param name="sc">current step context.</param> /// <param name="cancellationToken">cancellation token.</param> /// <returns>Task completion.</returns> public async Task <DialogTurnResult> DeleteToDoTask(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken)) { try { var state = await this.accessors.ToDoSkillState.GetAsync(sc.Context); if (state.DeleteTaskConfirmation) { if (string.IsNullOrEmpty(state.OneNotePageId)) { await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ToDoBotResponses.SettingUpOneNoteMessage)); } var service = await this.toDoService.Init(state.MsGraphToken, state.OneNotePageId); var page = await service.GetDefaultToDoPage(); string taskTopicToBeDeleted = null; if (state.MarkOrDeleteAllTasksFlag) { await service.DeleteAllToDos(state.ToDoTaskAllActivities, page.ContentUrl); state.ToDoTaskAllActivities = new List <ToDoTaskActivityModel>(); state.ToDoTaskActivities = new List <ToDoTaskActivityModel>(); state.ShowToDoPageIndex = 0; state.ToDoTaskIndexes = new List <int>(); } else { taskTopicToBeDeleted = state.ToDoTaskAllActivities[state.ToDoTaskIndexes[0]].Topic; var tasksToBeDeleted = new List <ToDoTaskActivityModel>(); state.ToDoTaskIndexes.ForEach(i => tasksToBeDeleted.Add(state.ToDoTaskAllActivities[i])); await service.DeleteToDos(tasksToBeDeleted, page.ContentUrl); var todosAndPageIdTuple = await service.GetMyToDoList(); state.OneNotePageId = todosAndPageIdTuple.Item2; state.ToDoTaskAllActivities = todosAndPageIdTuple.Item1; var allTasksCount = state.ToDoTaskAllActivities.Count; var currentTaskIndex = state.ShowToDoPageIndex * state.PageSize; while (currentTaskIndex >= allTasksCount && currentTaskIndex >= state.PageSize) { currentTaskIndex -= state.PageSize; state.ShowToDoPageIndex--; } state.ToDoTaskActivities = state.ToDoTaskAllActivities.GetRange(currentTaskIndex, Math.Min(state.PageSize, allTasksCount - currentTaskIndex)); } if (state.MarkOrDeleteAllTasksFlag) { await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ToDoBotResponses.AfterAllTasksDeleted)); } else { if (state.ToDoTaskActivities.Count > 0) { var deletedToDoListAttachment = ToDoHelper.ToAdaptiveCardAttachmentForOtherFlows( state.ToDoTaskActivities, state.ToDoTaskAllActivities.Count, taskTopicToBeDeleted, ToDoBotResponses.AfterTaskDeleted, ToDoBotResponses.ShowToDoTasks); var deletedToDoListReply = sc.Context.Activity.CreateReply(); deletedToDoListReply.Attachments.Add(deletedToDoListAttachment); await sc.Context.SendActivityAsync(deletedToDoListReply); } else { var token1 = new StringDictionary() { { "taskContent", taskTopicToBeDeleted } }; var response1 = ToDoHelper.GenerateResponseWithTokens(ToDoBotResponses.AfterTaskDeleted, token1); var token2 = new StringDictionary() { { "taskCount", "0" } }; var response2 = ToDoHelper.GenerateResponseWithTokens(ToDoBotResponses.ShowToDoTasks, token2); var response = response1 + " " + response2.Remove(response2.Length - 1) + "."; var botResponse = sc.Context.Activity.CreateReply(response); botResponse.Speak = response; await sc.Context.SendActivityAsync(botResponse); } } } else { await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ToDoBotResponses.AnythingElseCanDo)); } return(await sc.EndDialogAsync(true)); } catch (Exception ex) { await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ex.Message)); await this.accessors.ToDoSkillState.SetAsync(sc.Context, new ToDoSkillState()); return(await sc.CancelAllDialogsAsync()); } }