Curiosity
Curiosity

Reputation: 1931

Bot Framework (.Net) - Hero Card button click - IAwaitable with a custom type

I have a class (let's say XYZ) and I'm passing a list of type XYZ to a Hero Card as buttons, so that when user clicks on a button, it returns the clicked item's value. I have a method called getResult which expects a value of type XYZ.

private async Task getResult(IDialogContext context, IAwaitable<XYZ> result){}

The above method is executed fromcontext.Wait<XYZ>(getResult);

But when I click on a button, an error occurs (InvalidTypeException: invalid type: expected Microsoft.MscaBot.Data.XYZ, have Activity).

How can I avoid this error and receive the item clicked by the user?

Update: (Following is what my code looks like)

[Serializable]
public class XYZ
{
    public override string ToString()
    {
        return name;
    }
    public string name { get; set; }
    public Nullable<decimal> price { get; set; }
}

Other class:

[Serializable]
public class TestDialog : IDialog<object>
{
    private static List<XYZ> listOfXYZ = new List<XYZ>() { new XYZ() { name = "MyName1", price = 20.23 }, new XYZ() { name = "MyName2", price = 50.63 } };

    public async Task StartAsync(IDialogContext context)
    {
        await ShowOptions (context);
    }

    private async Task ShowOptions(IDialogContext context)
    {
        var replyToConversation = context.MakeMessage();
        replyToConversation.Type = "message";


        List<CardAction> cardButtons = new List<CardAction>();
        foreach (var item in listOfXYZ)
        {
            CardAction CardButton = new CardAction()
            {
               Type = ActionTypes.ImBack,
               Title = item.ToString(),
               Value = item
            };
            cardButtons.Add(CardButton);
        }

        HeroCard heroCard = new HeroCard()
        {
            Text = "Please select one of the options below",
            Buttons = cardButtons,
        };
        Attachment myAttachment = heroCard.ToAttachment();
        replyToConversation.Attachments.Add(myAttachment);
        replyToConversation.AttachmentLayout = "list";
        await context.PostAsync(replyToConversation);
        context.Wait(getResult);
    }

    private async Task getResult(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
         // Some logic here
    }
}

Upvotes: 0

Views: 1133

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

The CardAction of the HeroCard (or any other card) will end up posting an IMessageActivity to the bot. The Text of that activity will be the Value you set in the CardAction.

What you are trying to do is not possible. You need to await an IMessageActivity. If you are trying to send a complex object as the value, make sure the object it's serializable otherwise it won't work either.

Upvotes: 1

Related Questions