Esempio n. 1
0
        private async Task <bool> EvaluateExpressionInput(ITurnContext turnContext, RequestState requestState)
        {
            var               res     = false;
            var               text    = turnContext.Activity.Text;
            var               curLink = requestState.CurrentLink;
            ActionLink        actionResult;
            ExpEvalResultType op = curLink.NeuralExp.Next(text, out actionResult);

            switch (op)
            {
            case ExpEvalResultType.Skipped:
            case ExpEvalResultType.False:
            case ExpEvalResultType.True:
            {
                if (op == ExpEvalResultType.Skipped)
                {
                    await SendNotes(curLink, turnContext);
                }
                switch (actionResult.Type)
                {
                case LinkType.NeuralLink:
                {
                    var link = await DbLinkCollection.FindOneById(actionResult.LinkId);

                    if (link != null)
                    {
                        curLink = link;
                        requestState.StepForward(curLink);
                        await SendResponseForCurrentNode(turnContext, requestState);
                    }
                    else
                    {
                        //Invalid expression evaluation link id.
                        await SendReply(turnContext, $"Invalid node information : '{actionResult.LinkId}',please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions());
                    }
                }
                break;

                case LinkType.ActionLink:
                {
                    var action = await DbActionCollection.FindOneById(actionResult.LinkId);

                    await turnContext.SendActivityAsync(action.BuildActionRespose(turnContext));

                    requestState.CurrentState = ChatStateType.RecordFeedback;
                }
                break;

                case LinkType.NeuralResource:
                {
                    var resource = await DbResourceCollection.FindOneById(actionResult.LinkId);

                    await turnContext.SendActivityAsync(resource.BuildResourceResponse(turnContext));

                    requestState.CurrentState = ChatStateType.RecordFeedback;
                }
                break;

                default:
                    break;
                }
                if (requestState.CurrentState == ChatStateType.RecordFeedback)
                {
                    await Task.Delay(1000).ContinueWith(async ct =>
                        {
                            await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions());
                        });
                }
                res = true;
            }
            break;

            case ExpEvalResultType.Exception:
            case ExpEvalResultType.Invalid:
            {
                await SendReply(turnContext, "Invalid input, please try again!", SuggestionExtension.GetCommonSuggestionActions("Exit:exit"));

                requestState.CurrentState = ChatStateType.InvalidInput;
            }
            break;

            case ExpEvalResultType.Empty:    //TODO
            {
                await SendReply(turnContext, $"Evaluated empty expression of node : '{curLink._id}',please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions());
            }
            break;

            default:

                break;
            }
            return(res);
        }
Esempio n. 2
0
        private async Task <ResponseType> TakeAction(ITurnContext turnContext, RequestState requestState)
        {
            var ret     = ResponseType.Continue;
            var text    = turnContext.Activity.Text;
            var curLink = requestState.CurrentLink;

            switch (requestState.CurrentState)
            {
            case ChatStateType.Start:
            {
                await SendResponseForCurrentNode(turnContext, requestState);
            }
            break;

            case ChatStateType.AdvanceChat:
            {
                if (string.IsNullOrWhiteSpace(text))
                {
                    await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.InvalidInput), SuggestionExtension.GetCommonSuggestionActions());
                }
                else
                {
                    string nextNodeId = null;
                    try
                    {
                        nextNodeId = requestState.PredictNode(text);
                        var nextLink = await DbLinkCollection.FindOneById(nextNodeId);

                        if (nextLink != null)
                        {
                            requestState.StepForward(nextLink);
                            await SendResponseForCurrentNode(turnContext, requestState);
                        }
                        else
                        {
                            await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.NoMatchFound), SuggestionExtension.GetCommonSuggestionActions());
                        }
                    }
                    catch (Exception e)
                    {
                        await SendReply(turnContext, $"ML prediction returned an error: '{e.Message}'. please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions("Exit:exit"));
                    }
                }
            }
            break;

            case ChatStateType.PickNode:
            {
                var nextLink = await DbLinkCollection.FindOneById(text);

                if (nextLink == null)
                {
                    await turnContext.SendActivityAsync(StringsProvider.TryGet(BotResourceKeyConstants.InvalidInput));
                    await SendResponseForCurrentNode(turnContext, requestState);
                }
                else
                {
                    curLink = nextLink;
                    requestState.StepForward(curLink);
                    await SendResponseForCurrentNode(turnContext, requestState);
                }
            }
            break;

            case ChatStateType.RecordFeedback:
            {
                switch (text.ToLower())
                {
                case "yes":
                {
                    var             childLink  = curLink;
                    NeuralLinkModel parentLink = null;
                    while (requestState.LinkHistory.TryPop(out parentLink))
                    {
                        await DbLinkCollection.UpdateNeuralRankById(parentLink._id, childLink._id);

                        childLink = parentLink;
                    }
                    ret = ResponseType.End;
                }
                break;

                case "no":
                    ret = ResponseType.End;
                    break;

                default:
                    await turnContext.SendActivityAsync(StringsProvider.TryGet(BotResourceKeyConstants.InvalidInput));
                    await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions());

                    break;
                }
                if (ret == ResponseType.End)
                {
                    await turnContext.SendActivityAsync(StringsProvider.TryGet(BotResourceKeyConstants.ThankYouFeedback));
                }
            }
            break;

            case ChatStateType.InvalidInput:
            case ChatStateType.ExpInput:
            {
                await EvaluateExpressionInput(turnContext, requestState);
            }
            break;

            default:
                break;
            }
            return(ret);
        }
        private async Task <bool> EvaluateExpressionInput(ITurnContext turnContext, RequestState requestState)
        {
            var               res     = false;
            var               text    = turnContext.Activity.Text;
            var               curLink = requestState.CurrentLink;
            ActionLink        actionResult;
            ExpEvalResultType op = curLink.NeuralExp.Next(text, out actionResult);

            switch (op)
            {
            case ExpEvalResultType.Skipped:
            case ExpEvalResultType.False:
            case ExpEvalResultType.True:
            {
                switch (actionResult.Type)
                {
                case LinkType.NeuralLink:
                {
                    var link = await DbLinkCollection.FindOneById(actionResult.LinkId);

                    if (link != null)
                    {
                        curLink = link;
                        requestState.StepForward(curLink);
                        await SendResponseForCurrentNode(turnContext, requestState);
                    }
                    else
                    {
                        //Invalid expression evaluation link id.
                    }
                }
                break;

                case LinkType.ActionLink:
                {
                    var action = await DbActionCollection.FindOneById(actionResult.LinkId);

                    await turnContext.SendActivityAsync(action.BuildActionRespose(turnContext));

                    await Task.Delay(1000).ContinueWith(async ct =>
                            {
                                await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions());
                            });

                    requestState.CurrentState = ChatStateType.RecordFeedback;
                }
                break;

                case LinkType.NeuralResource:
                    break;

                default:
                    break;
                }
            }
                res = true;
                break;

            case ExpEvalResultType.Exception:
            case ExpEvalResultType.Invalid:
            {
                await SendReply(turnContext, "Invalid input, Try again!", SuggestionExtension.GetCommonSuggestionActions());

                requestState.CurrentState = ChatStateType.InvalidInput;
            }
            break;

            case ExpEvalResultType.Empty:    //TODO
            default:

                break;
            }
            return(res);
        }