예제 #1
0
        /// <summary>
        /// Called when the default topic is started
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <bool> StartTopic(ITurnContext context)
        {
            switch (context.Activity.Type)
            {
            case ActivityTypes.ConversationUpdate:
            {
                // greet when added to conversation
                var activity = context.Activity.AsConversationUpdateActivity();
                if (activity.MembersAdded.Any(m => m.Id == activity.Recipient.Id))
                {
                    await RootResponses.ReplyWithGreeting(context);

                    await RootResponses.ReplyWithHelp(context);

                    this.Greeted = true;
                }
            }
            break;

            case ActivityTypes.Message:
                // greet on first message if we haven't already
                if (!Greeted)
                {
                    await RootResponses.ReplyWithGreeting(context);

                    this.Greeted = true;
                }
                return(await this.ContinueTopic(context));
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Every Conversation turn for our bot calls this method.
        /// </summary>
        /// <param name="context">The current turn context.</param>
        public async Task OnTurn(ITurnContext context)
        {
            if (context.Activity.Type is ActivityTypes.Message)
            {
                // Get the user and conversation state from the turn context.
                var state = UserState <UserData> .Get(context);

                var conversationInfo = ConversationState <ConversationInfo> .Get(context);

                // Establish dialog state from the conversation state.
                var dc = _dialogs.CreateContext(context, conversationInfo);

                // Continue any current dialog.
                await dc.Continue();

                // Every turn sends a response, so if no response was sent,
                // then there no dialog is currently active.
                if (!context.Responded)
                {
                    // Greet them if we haven't already
                    if (state.Greeted == "not greeted")
                    {
                        await RootResponses.ReplyWithGreeting(context);

                        await RootResponses.ReplyWithHelp(context);

                        state.Greeted = "greeted";
                    }
                    else
                    {
                        await dc.Begin(RootDialog);
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Continue the topic, method which is routed to while this topic is active
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <bool> ContinueTopic(ITurnContext context)
        {
            var conversation = ConversationState <ConversationData> .Get(context);

            var recognizedIntents = context.Services.Get <IRecognizedIntents>();

            switch (context.Activity.Type)
            {
            case ActivityTypes.Message:
                switch (recognizedIntents.TopIntent?.Name)
                {
                case "search":
                    // switch to search topic
                    conversation.ActiveTopic = new SearchTopic();
                    return(await conversation.ActiveTopic.StartTopic(context));

                case "share":
                    // show that you're sharing
                    await RootResponses.ReplyWithShareConfirmation(context);

                    return(true);

                case "order":
                    // show that you're ordering
                    await RootResponses.ReplyWithOrderConfirmation(context);

                    return(true);

                case "help":
                    // show help
                    await RootResponses.ReplyWithHelp(context);

                    return(true);

                default:
                    // adding app logic when Regex doesn't find an intent - consult LUIS
                    var result    = context.Services.Get <RecognizerResult>(LuisRecognizerMiddleware.LuisRecognizerResultKey);
                    var topIntent = result?.GetTopScoringIntent();

                    switch ((topIntent != null) ? topIntent.Value.intent : null)
                    {
                    case null:
                        // Add app logic when there is no result.
                        await RootResponses.ReplyWithConfused(context);

                        break;

                    case "None":
                        await RootResponses.ReplyWithConfused(context);

                        await RootResponses.ReplyWithLuisScore(context, topIntent.Value.intent, topIntent.Value.score);

                        break;

                    case "Greeting":
                        await RootResponses.ReplyWithGreeting(context);

                        await RootResponses.ReplyWithHelp(context);

                        await RootResponses.ReplyWithLuisScore(context, topIntent.Value.intent, topIntent.Value.score);

                        break;

                    case "OrderPic":
                        await RootResponses.ReplyWithOrderConfirmation(context);

                        await RootResponses.ReplyWithLuisScore(context, topIntent.Value.intent, topIntent.Value.score);

                        break;

                    case "SharePic":
                        await RootResponses.ReplyWithShareConfirmation(context);

                        await RootResponses.ReplyWithLuisScore(context, topIntent.Value.intent, topIntent.Value.score);

                        break;

                    case "SearchPics":
                        // Check if LUIS has identified the search term that we should look for.
                        var entity = result?.Entities;
                        var obj    = JObject.Parse(JsonConvert.SerializeObject(entity)).SelectToken("facet");
                        // if no entities are picked up on by LUIS, go through SearchTopic
                        if (obj == null)
                        {
                            conversation.ActiveTopic = new SearchTopic();
                            await RootResponses.ReplyWithLuisScore(context, topIntent.Value.intent, topIntent.Value.score);
                        }
                        // if entities are picked up by LUIS, skip SearchTopic and process the search
                        else
                        {
                            facet = obj.ToString().Replace("\"", "").Trim(']', '[', ' ');
                            await ProceedWithSearchAsync(context, facet);

                            await RootResponses.ReplyWithLuisScore(context, topIntent.Value.intent, topIntent.Value.score);

                            break;
                        }
                        return(await conversation.ActiveTopic.StartTopic(context));

                    default:
                        await RootResponses.ReplyWithConfused(context);

                        break;
                    }
                    return(true);
                }
            }
            return(true);
        }
예제 #4
0
        /// <summary>
        /// Composes a main dialog for our bot.
        /// </summary>
        /// <returns>A new main dialog.</returns>
        private static DialogSet ComposeMainDialog()
        {
            var dialogs = new DialogSet();

            dialogs.Add(RootDialog, new WaterfallStep[]
            {
                // Duplicate the following row if your dialog will have
                // multiple turns. In this case, we just have one
                async(dc, args, next) =>
                {
                    // Get the state of the conversation
                    var conversation = ConversationState <ConversationInfo> .Get(dc.Context);
                    // If Regex picks up on anything, store it
                    var recognizedIntents = dc.Context.Services.Get <IRecognizedIntents>();
                    // Based on the recognized intent, direct the conversation
                    switch (recognizedIntents.TopIntent?.Name)
                    {
                    case "search":
                        // switch to SearchDialog
                        await dc.Begin(SearchDialog.Id);
                        break;

                    case "share":
                        // respond that you're sharing the photo
                        await RootResponses.ReplyWithShareConfirmation(dc.Context);
                        break;

                    case "order":
                        // respond that you're ordering
                        await RootResponses.ReplyWithOrderConfirmation(dc.Context);
                        break;

                    case "help":
                        // show help
                        await RootResponses.ReplyWithHelp(dc.Context);
                        break;

                    default:
                        // adding app logic when Regex doesn't find an intent - consult LUIS
                        var result    = dc.Context.Services.Get <RecognizerResult>(LuisRecognizerMiddleware.LuisRecognizerResultKey);
                        var topIntent = result?.GetTopScoringIntent();

                        switch ((topIntent != null) ? topIntent.Value.intent : null)
                        {
                        case null:
                            // Add app logic when there is no result.
                            await RootResponses.ReplyWithConfused(dc.Context);
                            break;

                        case "None":
                            await RootResponses.ReplyWithConfused(dc.Context);
                            await RootResponses.ReplyWithLuisScore(dc.Context, topIntent.Value.intent, topIntent.Value.score);
                            break;

                        case "Greeting":
                            await RootResponses.ReplyWithGreeting(dc.Context);
                            await RootResponses.ReplyWithHelp(dc.Context);
                            await RootResponses.ReplyWithLuisScore(dc.Context, topIntent.Value.intent, topIntent.Value.score);
                            break;

                        case "OrderPic":
                            await RootResponses.ReplyWithOrderConfirmation(dc.Context);
                            await RootResponses.ReplyWithLuisScore(dc.Context, topIntent.Value.intent, topIntent.Value.score);
                            break;

                        case "SharePic":
                            await RootResponses.ReplyWithShareConfirmation(dc.Context);
                            await RootResponses.ReplyWithLuisScore(dc.Context, topIntent.Value.intent, topIntent.Value.score);
                            break;

                        case "SearchPics":
                            // Check if LUIS has identified the search term that we should look for.
                            var entity = result?.Entities;
                            var obj    = JObject.Parse(JsonConvert.SerializeObject(entity)).SelectToken("facet");
                            // if no entities are picked up on by LUIS, go through SearchDialog
                            if (obj == null)
                            {
                                await dc.Begin(SearchDialog.Id);
                                await RootResponses.ReplyWithLuisScore(dc.Context, topIntent.Value.intent, topIntent.Value.score);
                            }
                            // if entities are picked up by LUIS, skip SearchDialog and process the search
                            else
                            {
                                var facet = obj.ToString().Replace("\"", "").Trim(']', '[', ' ');

                                await RootResponses.ReplyWithLuisScore(dc.Context, topIntent.Value.intent, topIntent.Value.score);
                                await SearchResponses.ReplyWithSearchConfirmation(dc.Context, facet);
                                await StartAsync(dc.Context, facet);
                                break;
                            }
                            break;

                        default:
                            await RootResponses.ReplyWithConfused(dc.Context);
                            break;
                        }
                        break;
                    }
                }
            });

            // Add our child dialogs (in this case just one)
            dialogs.Add(SearchDialog.Id, SearchDialog.Instance);

            return(dialogs);
        }