Expressions in Adaptive cards
Bot Framework Composer can be useful to create custom adaptive cards for your Power Virtual Agents bot. In this article I am showing you how you can use expressions for certain requirements.
Adaptive (card) expressions within Bot Framework Composer pic.twitter.com/YRB7mas6nR
— Dennis (@expiscornovus) March 25, 2022
Inspiration
This question from NewUser22:
Wanted to check on how do we do conditional formatting for color on adaptive cards for bot composer? Seems like bot composer only supports version 1.3..
Power Users Community thread: Conditional formatting for adaptive cards.
Bot Framework Composer
You can extend your bot by developing custom dialogs with Bot Framework Composer and then adding them to your Power Virtual Agents bot. There are four examples on the Microsoft Documentation site to get you started with Bot Framework Composer as a product.
The example I am showing today is based on Example 1 – Show an Adaptive Card in Power Virtual Agents. Definitely try out that sample first before you check out and try my code.
Adaptive expressions
Since this is all based on Bot Framework I assumed Microsoft probably had some kind of expression language, functions and documentation to summarize it all. Luckily, I found such an article, Adaptive expressions prebuilt functions.
For this example we want to retrieve what the current month it is. When the month is equal to January the color of the text should be green (Good). If it is another month it should be red (Attention).
For this specific use case I am using an adaptive expression with the if and equals functions.
Topic with Bot variable
Start by creating a standard topic within your Power Virtual Agent.
1. In Power Virtual Agents create a New Topic. Select From Blank.
2. Add a trigger phrase. I used the keyword ‘Formatting’.
3. Add an Action. Create a new Power Automate flow like below. This flow has an expression which retrieves the current month.
4. In the same action store the result of the flow in a variable, make sure that it is a bot variable.
Because it is a bot variable we can easily reference it within Bot Framework Composer by using the virtualagent scope.
Advanced Topic with Bot Response
The second part is to create an advanced topic via Bot Framework Composer. This will be used to send an adaptive card with some conditional formatting to the user.
1. In Power Virtual Agents create a New Topic. Select Open in Bot Framework Composer.
2. After the Bot Framework Composer application has opened navigate to the Create Tab, Add a Dialog. Provide a Name and click Ok.
3. In the Bot Responses Tab, select your newly created Dialog section, select Show Code and add the code from below
4. In the Create Tab, select Begin Dialog
5. Add a Send a response action. Add the code below to it
6. Publish your work.
Linking the topics together
The last thing we should do is to redirect the standard topic to our advanced topic.
1. Open your standard topic
2. Add Redirect to another topic action. Make sure you select your advanced topic
3. Publish your Power Virtual Agent
That should be it for the setup.
Publish your PVA to whatever channel you want and start testing. I have used it on the Demo website and Microsoft Teams channel btw.
Happy testing!