Dennis van den Berg Dennis van den Berg - 3 months ago 22
C# Question

Access original (user) message text in a LuisIntent method (MBF version 3)

I'm trying to access the original message in a

LuisIntent
method in a
LuisModel
class I am using in a Microsoft Bot Framework (version 3) C# project. However, I can't seem to figure it out myself, nor do any suggestions made in a related StackOverflow question work (because this concerns MBF version 1).

I tried commenting on a related issue on GitHub, without success.

Does anyone know how to get the user message that fired the method?

Answer

You can have a property to store the origin activity and assign it in MessageReceived

public class YourDialog : LuisDialog<string>
{    
    [NonSerialized]
    private IMessageActivity _originActivity;

    internal YourDialog()
    {
    }

    [LuisIntent("IntentionConstant.Empty")]
    public async Task HandleLuisResult(IDialogContext context, LuisResult result)
    {
        try
        {
            // you can access _originActivity here

        }
        catch (Exception ex) when(ex is ApplicationException)
        {
            throw;
        }
        catch (Exception ex) when (ex is TaskCanceledException)
        {
        }
    }

    protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
    {
        _originActivity = await item;
        await base.MessageReceived(context, item);
    }
}