Пример #1
0
        /// <summary>
        /// Mark To Do task completed step.
        /// </summary>
        /// <param name="sc">current step context.</param>
        /// <param name="cancellationToken">cancellation token.</param>
        /// <returns>Task completion.</returns>
        public async Task <DialogTurnResult> MarkToDoTaskCompleted(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await this.accessors.ToDoSkillState.GetAsync(sc.Context);

                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();

                BotResponse botResponse;
                string      taskToBeMarked = null;
                if (state.MarkOrDeleteAllTasksFlag)
                {
                    await service.MarkAllToDoItemsCompleted(state.ToDoTaskAllActivities, page.ContentUrl);

                    botResponse = ToDoBotResponses.AfterAllToDoTasksCompleted;
                }
                else
                {
                    await service.MarkToDoItemCompleted(state.ToDoTaskActivities[state.ToDoTaskIndex], page.ContentUrl);

                    botResponse    = ToDoBotResponses.AfterToDoTaskCompleted;
                    taskToBeMarked = state.ToDoTaskActivities[state.ToDoTaskIndex].Topic;
                }

                var todosAndPageIdTuple = await service.GetMyToDoList();

                state.OneNotePageId         = todosAndPageIdTuple.Item2;
                state.ToDoTaskAllActivities = todosAndPageIdTuple.Item1;
                var allTasksCount    = state.ToDoTaskAllActivities.Count;
                var currentTaskIndex = state.ShowToDoPageIndex * state.PageSize;
                state.ToDoTaskActivities = state.ToDoTaskAllActivities.GetRange(currentTaskIndex, Math.Min(state.PageSize, allTasksCount - currentTaskIndex));
                var markToDoAttachment = ToDoHelper.ToAdaptiveCardAttachmentForOtherFlows(
                    state.ToDoTaskActivities,
                    state.ToDoTaskAllActivities.Count,
                    taskToBeMarked,
                    botResponse,
                    ToDoBotResponses.ShowToDoTasks);
                var markToDoReply = sc.Context.Activity.CreateReply();
                markToDoReply.Attachments.Add(markToDoAttachment);
                await sc.Context.SendActivityAsync(markToDoReply);

                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());
            }
        }
Пример #2
0
        /// <summary>
        /// Add 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> AddToDoTask(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await this.accessors.ToDoSkillState.GetAsync(sc.Context);

                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();

                state.OneNotePageId = page.Id;
                await service.AddToDoToOneNote(state.ToDoTaskContent, page.ContentUrl);

                var todosAndPageIdTuple = await service.GetMyToDoList();

                state.OneNotePageId         = todosAndPageIdTuple.Item2;
                state.ToDoTaskAllActivities = todosAndPageIdTuple.Item1;
                state.ShowToDoPageIndex     = 0;
                var rangeCount = Math.Min(state.PageSize, state.ToDoTaskAllActivities.Count);
                state.ToDoTaskActivities = state.ToDoTaskAllActivities.GetRange(0, rangeCount);
                var toDoListAttachment = ToDoHelper.ToAdaptiveCardAttachmentForOtherFlows(
                    state.ToDoTaskActivities,
                    state.ToDoTaskAllActivities.Count,
                    state.ToDoTaskContent,
                    ToDoBotResponses.AfterToDoTaskAdded,
                    ToDoBotResponses.ShowToDoTasks);

                var toDoListReply = sc.Context.Activity.CreateReply();
                toDoListReply.Attachments.Add(toDoListAttachment);
                await sc.Context.SendActivityAsync(toDoListReply);

                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());
            }
        }
Пример #3
0
        /// <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());
            }
        }