botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] Call Specific LUIS Intent from Message Controller

Open SatyaPavanKumar opened this issue 8 years ago • 1 comments

I am beginner to chat Bot applications. Below is my requirement .

I have more than 10 intents in chatbot application . whenever chat bot launched, I will check user registration status (Registered/Unregistered) from database in background. If user is UNREGISTERED, i will have to send response to bot with prompting 2 buttons with message like below .

Hey, You are not registered , Would you like to register ? ( prompt with YES or NO Buttons)

If user status is REGISTERED, i should call 1 LUIS intent directly from messagecontroller.cs file else I should call another LUIS intent based on user prompt from above message.

Note: LUIS class file is in dialogs folder of application.

My Queries: I can get to know user status in message controller itself

  1. How can I prompt message with 2 buttons in messagecontroller.cs file if user is unregistered?

  2. How can i call exactly specific LUIS intent if user status is registered from messagecontroller.cs file ?

Please help me with code and suggestions if i can acheive in any other way?

SatyaPavanKumar avatar Feb 21 '18 06:02 SatyaPavanKumar

It is generally a good practice to have a RootDialog that contains the branching logic, and load that dialog from the MessageController:

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                await Conversation.SendAsync(activity, () => new RootDialog());
            }
            else
            {
                HandleSystemMessage(activity);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

RootDialog:

  [Serializable]
   public class RootDialog : IDialog<object>
   {
       public async Task StartAsync(IDialogContext context)
       {
           context.Wait(this.MessageReceivedAsync);
       }

       private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
       {
           var message = await result;

           if (!context.UserData.ContainsKey("UserRegistered"))
           {
               PromptDialog.Confirm(context, ResumeAfterChoice, "Would you like to register?");                
           }
           else
           {
               await context.PostAsync("Already registered");
               //load another dialog?  

               context.Wait(MessageReceivedAsync);
           }
       }

       private async Task ResumeAfterChoice(IDialogContext context, IAwaitable<bool> result)
       {
           var choice = await result;
           try
           {
               if (choice)
               {
                   await context.PostAsync("user chose to register");
                   //forward to registration dialog
                   await context.Forward(new RegistrationDialog(), ResumeAfterRegistration, context.Activity.AsMessageActivity());
               }
               else
               {
                   await context.PostAsync("user did not choose to register");
                   //now what?
               }
           }
           catch (TooManyAttemptsException ex)
           {
               await context.PostAsync($"Ooops! Too many attemps. Please choose to register!");
               context.Wait(this.MessageReceivedAsync);
           }
       }

       private async Task ResumeAfterRegistration(IDialogContext context, IAwaitable<object> result)
       {
           var response = await result;
           //check result, to determine if user completed registration
       }
   }

EricDahlvang avatar Feb 26 '18 18:02 EricDahlvang