Search code examples
c#botframeworkazure-language-understanding

Is there any common method which will hit all the intents in the LUIS Dialog?


I would like to know if anyone knows any way to call a custom method all the time whenever any Luis Intent method is invoked. Basically I am trying to add a loader message to user when a Bot is getting data from LuisDialog.


Solution

  • If you have a RootDialog that forwards the message to the LuisDialog, you can show a "loader message" in either the RootDialog or the LuisDialog's StartAsync method. Another option is to override the LuisDialog's MessageReceived, and send the loader message before calling the base.MessageReceived.

    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);
            return Task.CompletedTask;
        }
    
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;
            await context.PostAsync("RootDialog.MessageReceivedAsync");
            await context.Forward(new LuisTestDialog(), AfterLuisDialog, activity);            
        }
    
        private async Task AfterLuisDialog(IDialogContext context, IAwaitable<object> result)
        {
            await context.PostAsync("RootDialog.AfterLuisDialog");            
        }
    }
    
    [LuisModel("xxx", "xxx")]
    [Serializable]
    public class LuisTestDialog : LuisDialog<object>
    {
        public async override Task StartAsync(IDialogContext context)
        {
            await context.PostAsync("LuisTestDialog.StartAsync");
            await base.StartAsync(context);
        }
    
        [LuisIntent("")]
        [LuisIntent("None")]
        public async Task None(IDialogContext context, LuisResult result)
        {
            await context.PostAsync("LuisTestDialog.None");
            context.Done(true);
        }
    
        protected async override Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
        {
            await context.PostAsync("LuisTestDialog.MessageReceived");
            await base.MessageReceived(context, item);
        }
    }
    

    The above will result in the following messages:

    enter image description here