コード例 #1
0
ファイル: ShowToDoItemDialog.cs プロジェクト: premsonbaby/AI
        public async Task <DialogTurnResult> ShowToDoTasks(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                state.ListType = state.ListType ?? ListType.ToDo.ToString();
                if (!state.ListTypeIds.ContainsKey(state.ListType))
                {
                    await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ToDoSharedResponses.SettingUpOneNoteMessage));
                }

                var topIntent = state.LuisResult?.TopIntent().intent;
                if (topIntent == ToDo.Intent.ShowToDo)
                {
                    var service = await ServiceManager.InitAsync(state.MsGraphToken, state.ListTypeIds);

                    state.AllTasks = await service.GetTasksAsync(state.ListType);
                }

                var allTasksCount    = state.AllTasks.Count;
                var currentTaskIndex = state.ShowTaskPageIndex * state.PageSize;
                state.Tasks = state.AllTasks.GetRange(currentTaskIndex, Math.Min(state.PageSize, allTasksCount - currentTaskIndex));
                var generalTopIntent = state.GeneralLuisResult?.TopIntent().intent;
                if (state.Tasks.Count <= 0)
                {
                    return(await sc.NextAsync());
                }
                else
                {
                    Attachment toDoListAttachment = null;
                    if (topIntent == ToDo.Intent.ShowToDo)
                    {
                        toDoListAttachment = ToAdaptiveCardAttachmentForShowToDos(
                            state.Tasks,
                            state.AllTasks.Count,
                            ToDoSharedResponses.ShowToDoTasks,
                            ShowToDoResponses.ReadToDoTasks);
                    }
                    else if (generalTopIntent == General.Intent.Next)
                    {
                        toDoListAttachment = ToAdaptiveCardAttachmentForShowToDos(
                            state.Tasks,
                            state.AllTasks.Count,
                            ShowToDoResponses.ShowNextToDoTasks,
                            null);
                    }
                    else if (generalTopIntent == General.Intent.Previous)
                    {
                        toDoListAttachment = ToAdaptiveCardAttachmentForShowToDos(
                            state.Tasks,
                            state.AllTasks.Count,
                            ShowToDoResponses.ShowPreviousToDoTasks,
                            null);
                    }

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

                    if (topIntent == ToDo.Intent.ShowToDo && allTasksCount > (state.ShowTaskPageIndex + 1) * state.PageSize)
                    {
                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ShowToDoResponses.ShowingMoreTasks));
                    }

                    return(await sc.EndDialogAsync(true));
                }
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }
コード例 #2
0
        public async Task <DialogTurnResult> DeleteToDoTask(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var state = await Accessor.GetAsync(sc.Context);

                if (state.DeleteTaskConfirmation)
                {
                    if (!state.ListTypeIds.ContainsKey(state.ListType))
                    {
                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(ToDoSharedResponses.SettingUpOneNoteMessage));
                    }

                    var service = await ServiceManager.InitAsync(state.MsGraphToken, state.ListTypeIds);

                    string taskTopicToBeDeleted = null;
                    if (state.MarkOrDeleteAllTasksFlag)
                    {
                        await service.DeleteTasksAsync(state.ListType, state.AllTasks);

                        state.AllTasks          = new List <TaskItem>();
                        state.Tasks             = new List <TaskItem>();
                        state.ShowTaskPageIndex = 0;
                        state.TaskIndexes       = new List <int>();
                    }
                    else
                    {
                        taskTopicToBeDeleted = state.AllTasks[state.TaskIndexes[0]].Topic;
                        var tasksToBeDeleted = new List <TaskItem>();
                        state.TaskIndexes.ForEach(i => tasksToBeDeleted.Add(state.AllTasks[i]));
                        await service.DeleteTasksAsync(state.ListType, tasksToBeDeleted);

                        state.AllTasks = await service.GetTasksAsync(state.ListType);

                        var allTasksCount    = state.AllTasks.Count;
                        var currentTaskIndex = state.ShowTaskPageIndex * state.PageSize;
                        while (currentTaskIndex >= allTasksCount && currentTaskIndex >= state.PageSize)
                        {
                            currentTaskIndex -= state.PageSize;
                            state.ShowTaskPageIndex--;
                        }

                        state.Tasks = state.AllTasks.GetRange(currentTaskIndex, Math.Min(state.PageSize, allTasksCount - currentTaskIndex));
                    }

                    if (state.MarkOrDeleteAllTasksFlag)
                    {
                        await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(DeleteToDoResponses.AfterAllTasksDeleted));
                    }
                    else
                    {
                        if (state.Tasks.Count > 0)
                        {
                            var deletedToDoListAttachment = ToAdaptiveCardAttachmentForOtherFlows(
                                state.Tasks,
                                state.AllTasks.Count,
                                taskTopicToBeDeleted,
                                DeleteToDoResponses.AfterTaskDeleted,
                                ToDoSharedResponses.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 = GenerateResponseWithTokens(DeleteToDoResponses.AfterTaskDeleted, token1);
                            var token2    = new StringDictionary()
                            {
                                { "taskCount", "0" }
                            };
                            var response2   = GenerateResponseWithTokens(ToDoSharedResponses.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(ToDoSharedResponses.ActionEnded));
                }

                return(await sc.EndDialogAsync(true));
            }
            catch
            {
                await HandleDialogExceptions(sc);

                throw;
            }
        }