Exemplo n.º 1
0
        private async Task DigestActionInput(DialogContext dc, FindArticlesInput request)
        {
            var userState = await _userStateAccessor.GetAsync(dc.Context, () => new NewsSkillUserState());

            userState.Market = request.Market;
            var convState = await _stateAccessor.GetAsync(dc.Context, () => new NewsSkillState());

            var newsLuis = new NewsLuis()
            {
                Entities = new NewsLuis._Entities()
            };

            if (request.Query != null)
            {
                newsLuis.Entities.topic = new string[] { request.Query };
            }

            if (request.Site != null)
            {
                newsLuis.Entities.site = new string[] { request.Site };
            }

            convState.LuisResult = newsLuis;
        }
Exemplo n.º 2
0
        // Handles routing to additional dialogs logic.
        private async Task <DialogTurnResult> RouteStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var a     = stepContext.Context.Activity;
            var state = await _stateAccessor.GetAsync(stepContext.Context, () => new NewsSkillState(), cancellationToken : cancellationToken);

            state.IsAction = false;

            if (a.Type == ActivityTypes.Message && !string.IsNullOrEmpty(a.Text))
            {
                var result = stepContext.Context.TurnState.Get <NewsLuis>(StateProperties.SkillLuisResult);
                state.LuisResult = result;

                var intent = result?.TopIntent().intent;

                // switch on general intents
                switch (intent)
                {
                case NewsLuis.Intent.TrendingArticles:
                {
                    // send articles in response
                    return(await stepContext.BeginDialogAsync(nameof(TrendingArticlesDialog), cancellationToken : cancellationToken));
                }

                case NewsLuis.Intent.SetFavoriteTopics:
                case NewsLuis.Intent.ShowFavoriteTopics:
                {
                    // send favorite news categories
                    return(await stepContext.BeginDialogAsync(nameof(FavoriteTopicsDialog), cancellationToken : cancellationToken));
                }

                case NewsLuis.Intent.FindArticles:
                {
                    // send greeting response
                    return(await stepContext.BeginDialogAsync(nameof(FindArticlesDialog), cancellationToken : cancellationToken));
                }

                case NewsLuis.Intent.None:
                {
                    // No intent was identified, send confused message
                    await stepContext.Context.SendActivityAsync(_templateManager.GenerateActivityForLocale(MainStrings.CONFUSED), cancellationToken);

                    break;
                }

                default:
                {
                    // intent was identified but not yet implemented
                    await stepContext.Context.SendActivityAsync(_templateManager.GenerateActivityForLocale(MainStrings.FeatureNotAvailable), cancellationToken);

                    break;
                }
                }
            }
            else if (a.Type == ActivityTypes.Event)
            {
                // Handle skill actions here
                var eventActivity = a.AsEventActivity();

                if (!string.IsNullOrEmpty(eventActivity.Name))
                {
                    switch (eventActivity.Name)
                    {
                    // Each Action in the Manifest will have an associated Name which will be on incoming Event activities
                    case Events.GetTrendingArticlesEvent:
                    {
                        TrendingArticlesInput actionData = null;

                        var eventValue = a.Value as JObject;
                        if (eventValue != null)
                        {
                            actionData = eventValue.ToObject <TrendingArticlesInput>();
                            await DigestActionInput(stepContext, actionData);
                        }

                        state.IsAction = true;
                        return(await stepContext.BeginDialogAsync(nameof(TrendingArticlesDialog)));
                    }

                    case Events.GetFavoriteTopicsEvent:
                    {
                        FavoriteTopicsInput actionData = null;

                        var eventValue = a.Value as JObject;
                        if (eventValue != null)
                        {
                            actionData = eventValue.ToObject <FavoriteTopicsInput>();
                            await DigestActionInput(stepContext, actionData);
                        }

                        state.IsAction = true;
                        return(await stepContext.BeginDialogAsync(nameof(FavoriteTopicsDialog)));
                    }

                    case Events.FindArticlesEvent:
                    {
                        FindArticlesInput actionData = null;

                        var eventValue = a.Value as JObject;
                        if (eventValue != null)
                        {
                            actionData = eventValue.ToObject <FindArticlesInput>();
                            await DigestActionInput(stepContext, actionData);
                        }

                        state.IsAction = true;
                        return(await stepContext.BeginDialogAsync(nameof(FindArticlesDialog)));
                    }

                    default:

                        // todo: move the response to lg
                        await stepContext.Context.SendActivityAsync(new Activity(type : ActivityTypes.Trace, text : $"Unknown Event '{eventActivity.Name ?? "undefined"}' was received but not processed."));

                        break;
                    }
                }
            }

            // If activity was unhandled, flow should continue to next step
            return(await stepContext.NextAsync());
        }