PVA + Bot Framework Composer to display dynamic choice options
In this first answer, I will provide an example on how to present a chatbot user with dynamic choice options that are retrieved from an external data source. I use Dataverse in this example, but using SharePoint would be similar, as we retrieve the data with a Power Automate cloud flow and pass the data back to Power Virtual Agents.
The first thing you'll want to do is retrieve the data from your external source.
In my scenario, I want to display a list of stores that are available for a given city.
So, I create a new topic called "Store Locations", give it a few trigger phrases, and add a Question node to ask the user "What is your city"?
I use Power Virtual Agents' default "City" entity to automatically extract the city from a user utterance.
I also save the response as the "UserCity" variable and update the variable to make it global, so I change its scope to "Bot (any topic can access)". This is important for the rest of my demonstration because I will also pass that value to a Bot Framework Composer dialog.
![HenryJammes_1-1671133550534.png HenryJammes_1-1671133550534.png](/_api/msdyn_richtextfiles%28B6C01A32-4A41-48E3-9689-46EDFD9254A8%29/msdyn_imageblob/$value?size=full)
I then create a new cloud flow to look for the list of stores for a given city.
![HenryJammes_2-1671133764441.png HenryJammes_2-1671133764441.png](/_api/msdyn_richtextfiles%28E5EE8477-F660-4695-8547-3847255B03C7%29/msdyn_imageblob/$value?size=full)
In Power Automate:
- I add "User City" as a text input for my flow.
- I add 1 integer variable 'Number Of Accounts" and 1 array variable "List Of Accounts Options"
- I use the "List Rows" from the Dataverse connector to retrieve a list of Accounts with their name, address first line, and I filter the list to only retrieve active accounts that are located in the city the user provided (UserCity).
![HenryJammes_3-1671134122587.png HenryJammes_3-1671134122587.png](/_api/msdyn_richtextfiles%2861B18521-0441-4716-8899-88623AEBE887%29/msdyn_imageblob/$value?size=full)
Still in Power Automate:
- I add an "Append to array variable" for my "List Of Accounts Options" variable. In "Value" I use the dynamic content "Account Name". This also immediately and automatically creates an "Apply to Each" loop.
- I add another "Append to array variable" for my "Number Of Accounts" variable. In "Value" I simply enter 1.
- Finally, in the "Return value(s) to Power Virual Agents" step, I add 2 outputs: "Number Of Accounts" and "List Of Accounts Options". For "Number Of Accounts" I can map it to corresponding variable, but for "List Of Accounts Options" I first need to transform it to text format (as it is an array). To that end, I go the "Expression" tab, select "string" and go back to the "Dynamic Content" tab to select "List Of Accounts Options" and click OK.
![HenryJammes_4-1671134884366.png HenryJammes_4-1671134884366.png](/_api/msdyn_richtextfiles%2862CEBE7D-D0D7-4CB0-8BC7-11CA69568911%29/msdyn_imageblob/$value?size=full)
Back in Power Virtual Agents:
- I can see my Power Automate flows has the new 2 outputs created as variables.
- I make the "ListOfAccoutsOptions" a Bot-scoped variable
- I use the "NumberOfAccounts" variable in a Condition node so that I can have different user experienced if the city returns 0 results.
- I save my topic
![HenryJammes_5-1671135068284.png HenryJammes_5-1671135068284.png](/_api/msdyn_richtextfiles%2868E8EEC2-5362-40AC-8838-348257F32D85%29/msdyn_imageblob/$value?size=full)
I'm now going to move over to the Bot Framework Composer.
- From the list of topics, I click on "Open in Bot Framework Composer".
![HenryJammes_6-1671135297639.png HenryJammes_6-1671135297639.png](/_api/msdyn_richtextfiles%2899D7DC92-60EF-483A-9397-008D1C7235E3%29/msdyn_imageblob/$value?size=full)
In Bot Framework Composer:
- If I haven't done so already, I install it, authenticate and create a local folder for my changes
- I add a new dialog for my bot that I name "ListOfAccountsOptions".
![HenryJammes_7-1671135404564.png HenryJammes_7-1671135404564.png](/_api/msdyn_richtextfiles%289C1A6D18-470B-4C72-8BD5-53B958266FA7%29/msdyn_imageblob/$value?size=full)
- In order for my dialog to return values to Power Virtual Agents, I add an Output: UserSelectedStore and it's a string.
![HenryJammes_8-1671135514509.png HenryJammes_8-1671135514509.png](/_api/msdyn_richtextfiles%285B3C4881-45B1-4F96-B431-2A88FE74F4C6%29/msdyn_imageblob/$value?size=full)
- Under "BeginDialog", I add a new "Ask a question" node of type "Multi-choice".
- In the "Prompt with multi-choice" I add "What is your favorite store in ${virtualagent.UserCity}?" as an alternative under "Text".
- ${virtualagent.UserCity} will automatically use the City that the user has provided in Power Virtual Agents.
![HenryJammes_9-1671136326523.png HenryJammes_9-1671136326523.png](/_api/msdyn_richtextfiles%287A821A39-43BE-4146-A484-BD99C5282F98%29/msdyn_imageblob/$value?size=full)
- In "User input (Choice)", I set the "Properpty" to "dialog.result.UserSelectedStore". That way, I will be able to use the selected option in Power Virtual Agents, in a new variable.
- I choose "value" as the "Ouput format"
![HenryJammes_0-1671199261377.png HenryJammes_0-1671199261377.png](/_api/msdyn_richtextfiles%28B525FD40-E021-4DAD-9752-59CFDC947B46%29/msdyn_imageblob/$value?size=full)
- Still in "User input (Choice)", I scroll down to "List style" and select "suggestedAction" (note: for use in Teams, choose "heroCard" instead.
- For "Array of choices", instead of simple choices, I choose "= Write an expression".
- I select the data variable returned from my Power Automate cloud flow using the "=${virtualagent.ListOfAccountsOptions}"
![HenryJammes_11-1671136683659.png HenryJammes_11-1671136683659.png](/_api/msdyn_richtextfiles%282F873808-BE54-4B2E-80CF-F82506EC098B%29/msdyn_imageblob/$value?size=full)
- Once done, I go to the "Publish" tab, select my bot, and click "Publish selected bots"
![HenryJammes_12-1671136795330.png HenryJammes_12-1671136795330.png](/_api/msdyn_richtextfiles%28F5DE56C5-E0D5-4DA8-B3E0-3C948AB0A286%29/msdyn_imageblob/$value?size=full)
I return to my Power Virtual Agents "Store Locations" topic, and under "All other conditions", I add:
- A message saying "OK, I have found {x] NumberOfAccounts location(s) in {x}bot.UserCity." using variables.
- A "Redirect to another topic" node to select the "ListOfAccountsOptions" Bot Framework dialog.
- A message saying "OK, you selected {x} UserSelectedStore - excellence choice!" using the new variable.
![HenryJammes_13-1671137115556.png HenryJammes_13-1671137115556.png](/_api/msdyn_richtextfiles%2882FC7B5D-04C1-40D9-AD17-7237C11B8861%29/msdyn_imageblob/$value?size=full)
And that's it.
The list of options presented to the user is dynamically generated from a Power Automate cloud flow:
![HenryJammes_14-1671137358046.png HenryJammes_14-1671137358046.png](/_api/msdyn_richtextfiles%289DA153C2-3C89-4083-83A8-D07356F82378%29/msdyn_imageblob/$value?size=full)