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

Is it possible to return an Activity from a LuisDialog?

Open fercom opened this issue 10 years ago • 5 comments

I understand that the "original" activity is needed to create a response with attachments from inside a Dialog like this:

public async Task ActivityReceivedAsync(IDialogContext context, IAwaitable argument) { IActivity activity = await argument; var message = activity as Activity; IMessageActivity replyToConversation = message.CreateReply("Welcome. Hi"); replyToConversation.Recipient = message.From; replyToConversation.Attachments.Add( new Attachment() { ContentUrl = "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png", ContentType = "image/png", Name = "Bender_Rodriguez.png" } ); replyToConversation.AttachmentLayout = AttachmentLayoutTypes.List; await context.PostAsync(replyToConversation); context.Wait(MessageReceived); }

But, when I try to pass the activity as an argument to a LuisDialog to use it for the construction of the reply It always fails because the Activity Class is not marked as Serializable, it seems that in a LuisDialog you can only post text responses, my bot needs to respond with Attachments.

Thank you

fercom avatar Aug 25 '16 20:08 fercom

You can use context.MakeMessage to get access to an activity for sending attachments.

If you want to pass activity to a child dialog, consider https://github.com/Microsoft/BotBuilder/issues/1082.

Also in the newest release, you can access the original incoming message from the intent handlers:

https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Samples/AlarmBot/Dialogs/AlarmLuisDialog.cs#L108

willportnoy avatar Aug 25 '16 21:08 willportnoy

Ok, now I tried with this code: var msj = context.MakeMessage(); msj.Text = "hello"; msj.Attachments.Add( new Attachment() { ContentUrl = "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png", ContentType = "image/png", Name = "Bender_Rodriguez.png" } ); msj.AttachmentLayout = AttachmentLayoutTypes.List; await context.PostAsync(msj); context.Wait(MessageReceived);

It sends the response to the emulator but it also throws a SerializationException for the LuisResult Class, it says that it is not marked as serializable image

fercom avatar Aug 26 '16 02:08 fercom

It seems likely that you're storing the LuisResult as a class member within your dialog. Do you need it preserved while waiting for user input? If not, mark it with the [NonSerialized] attribute.

willportnoy avatar Aug 26 '16 02:08 willportnoy

You are absolutely right, I had exactly that and I wasn't even using it. After I removed the unused field it works. Thanks!

fercom avatar Aug 26 '16 02:08 fercom

[LuisIntent("YOUR_INTENT")] public async Task METHOD(IDialogContext context, LuisResult result) { Activity response = ((Activity)context.Activity).CreateReply(); HeroCard card = new HeroCard(){ /* YOUR CARD CONTENT */}; response.Attachments.Add(card.ToAttachment()); await context.PostAsync(response); }

I hope this helps!

ThiagoGodinhoPaschoalin avatar Mar 02 '18 01:03 ThiagoGodinhoPaschoalin