Search code examples
c#botframeworkazure-bot-serviceadaptive-cardsadaptive-dialogs

Can an adaptive card used in an adaptive dialog in a templated fashion?


I would like to return an Adaptive card via SendActivity in an Adaptive Dialog.

The code to do this looks like:

new OnIntent("Help")
{
    Actions = new List<Dialog>()
    {
        new SendActivity("${Help-Root-Dialog()}")
    }
},

But, I'd like to include a parameter in the call to create the adaptive card. Let's say a username (and therefore personalize the message to the user. Is there a way this can be done?


Solution

  • Found a few ways to do this:

    1. Use SetProperty before the card is invoked in the conversation
    new SetProperty()
    {
        Property = "conversation.gameCreateDate",
        Value = DateTime.Now.ToString()
    },
    
    new SendActivity("${PlayGameCard()}"),
    //In .lg file:
    # PlayGameCard
    [Activity
        Attachments = ${json(AdaptiveCard.Definition())}
    ]
    
    //...
    {
        "type": "TextBlock",
        "spacing": "None",
        "text": "Created ${conversation.gameCreateDate}",
        "isSubtle": true,
        "wrap": true
    }
    //...
    
    1. Send a parameter when calling the card, as is done in the AdaptorWithErrorHandler middleware.
    await turnContext.SendActivityAsync(ActivityFactory.FromObject( _templates.Evaluate("SomethingWentWrong", exception)));
    

    I used option 1, but 2 seems to be the way.