Exemple #1
0
        public async Task OnTurn(ITurnContext context)
        {
            //TODO: is this the right way to handle cards?
            string  utterance = context.Activity.Text;
            JObject cardData  = (JObject)context.Activity.Value;

            if (cardData != null && cardData.Property("intent") != null)
            {
                utterance = cardData["utterance"].ToString();
            }
            System.Threading.CancellationToken ct;

            CafeBotUserState userState         = context.GetUserState <CafeBotUserState>();
            CafeBotConvState conversationState = context.GetConversationState <CafeBotConvState>();

            switch (context.Activity.Type)
            {
            case ActivityTypes.ConversationUpdate:
                var newUserName = context.Activity.MembersAdded[0].Name;
                if (!string.IsNullOrWhiteSpace(newUserName) && newUserName != "Bot")
                {
                    await context.SendActivity($"Hello {newUserName}! I'm the Cafe bot!");

                    // remember the user's name
                    userState.name = newUserName;

                    userState.sendCards = true;

                    await context.SendActivity("I can help you find contoso cafe locations, book a table and answer questions about Contoso cafe!");

                    // send a welcome card
                    if (userState.sendCards)
                    {
                        await context.SendActivity(CreateCardResponse(context.Activity, createWelcomeCardAttachment()));
                    }
                }
                break;

            case ActivityTypes.Message:
                // create dialogContext
                DialogContext dc = _dialogs.CreateContext(context, conversationState);


                if (utterance == "start over")
                {
                    //restart the conversation
                    await context.SendActivity("Sure.. Let's start over");

                    dc.EndAll();
                }
                else
                {
                    // continue with any active dialogs
                    await dc.Continue();
                }

                if (!context.Responded)
                {
                    // call LUIS and get results
                    LuisRecognizer lRecognizer = createLUISRecognizer();
                    cafeLUISModel  lResult     = await lRecognizer.Recognize <cafeLUISModel>(utterance, ct);

                    Dictionary <string, object> lD = new Dictionary <string, object>();
                    if (lResult != null)
                    {
                        lD.Add("luisResult", lResult);
                    }

                    // top level dispatch
                    switch (lResult.TopIntent().intent)
                    {
                    case cafeLUISModel.Intent.Greeting:
                        await context.SendActivity("Hello, I'm the contoso cafe bot. How can I help you?");

                        if (userState.sendCards)
                        {
                            await context.SendActivity(CreateCardResponse(context.Activity, createWelcomeCardAttachment()));
                        }
                        break;

                    case cafeLUISModel.Intent.Book_Table:
                        await dc.Begin("BookTable", lD);

                        break;

                    case cafeLUISModel.Intent.Who_are_you_intent:
                        await dc.Begin("WhoAreYou");

                        break;

                    case cafeLUISModel.Intent.None:
                    default:
                        await getQnAResult(context);

                        break;
                    }
                }
                break;
            }
        }
Exemple #2
0
        public async Task OnTurn(ITurnContext context)
        {
            //TODO: is this the right way to handle cards?
            string  utterance = context.Activity.Text;
            JObject cardData  = (JObject)context.Activity.Value;

            if (cardData != null && cardData.Property("intent") != null)
            {
                utterance = cardData["utterance"].ToString();
            }

            var userState         = context.GetUserState <CafeBotUserState>();
            var conversationState = context.GetConversationState <CafeBotConvState>();

            switch (context.Activity.Type)
            {
            case ActivityTypes.ConversationUpdate:
                var newUserName = context.Activity.MembersAdded[0].Name;
                if (!string.IsNullOrWhiteSpace(newUserName) && newUserName != "Bot" && string.IsNullOrEmpty(userState.name))
                {
                    await context.SendActivity($"Hello {newUserName}! I'm the Cafe bot!");

                    // remember the user's name
                    userState.name = newUserName;

                    //userState.sendCards = true;

                    await context.SendActivity("I can help you find contoso cafe locations, book a table and answer questions about Contoso cafe!");

                    // send a welcome card
                    if (userState.sendCards)
                    {
                        await context.SendActivity(CreateCardResponse(context.Activity, createWelcomeCardAttachment()));
                    }
                }
                break;

            case ActivityTypes.Message:

                // create dialogContext
                var dc = _dialogs.CreateContext(context, conversationState);
                // continue with any active dialogs
                await dc.Continue();

                if (!context.Responded)
                {
                    // call LUIS and get results
                    LuisRecognizerOptions lOptions = new LuisRecognizerOptions()
                    {
                        Verbose = true
                    };
                    LuisModel lModel = new LuisModel(
                        "edaadd9b-b632-4733-a25c-5b67271035dd",
                        "be30825b782843dcbbe520ac5338f567",
                        new System.Uri("https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/"), Microsoft.Cognitive.LUIS.LuisApiVersion.V2);
                    LuisRecognizer lRecognizer = new LuisRecognizer(lModel, lOptions);
                    System.Threading.CancellationToken ct;
                    cafeLUISModel lResult = await lRecognizer.Recognize <cafeLUISModel>(context.Activity.Text, ct);

                    // top level dispatch

                    switch (lResult.TopIntent().intent)
                    {
                    case cafeLUISModel.Intent.Greeting:
                        //case "hi":
                        await context.SendActivity("Hello, I'm the contoso cafe bot. How can I help you?");

                        if (userState.sendCards)
                        {
                            await context.SendActivity(CreateCardResponse(context.Activity, createWelcomeCardAttachment()));
                        }
                        break;

                    case cafeLUISModel.Intent.Book_Table:
                        // case "book table":
                        await dc.Begin("BookTable");

                        break;

                    case cafeLUISModel.Intent.Who_are_you_intent:
                        // case "who are you?":
                        await dc.Begin("WhoAreYou");

                        break;

                    case cafeLUISModel.Intent.None:
                    default:
                        var qEndpoint = new QnAMakerEndpoint()
                        {
                            Host            = "https://contosocafeqnamaker.azurewebsites.net/qnamaker",
                            EndpointKey     = "09e2d55b-a44c-41b6-a08a-76a7df9ddffe",
                            KnowledgeBaseId = "b5534d70-bded-45e1-998a-5945174d4ff3"
                        };
                        var qOptions = new QnAMakerOptions()
                        {
                            ScoreThreshold = 0.4F,
                            Top            = 1
                        };
                        var           qnamaker = new QnAMaker(qEndpoint, qOptions);
                        QueryResult[] qResult  = await qnamaker.GetAnswers(context.Activity.Text);

                        if (qResult.Length == 0)
                        {
                            await context.SendActivity("Sorry, I do not understand.");

                            await context.SendActivity("You can say hi or book table or find locations");
                        }
                        else
                        {
                            await context.SendActivity(qResult[0].Answer);
                        }

                        //await context.SendActivity("Sorry, I do not understand.");
                        //await context.SendActivity("You can say hi or book table or find locations");
                        break;
                    }
                }
                break;
            }
        }