Create an Interactive Power Virtual Agent

close up photo of toy bot

Making your Power Virtual Agent more interactive is achievable using Power Automate. When the bot asks the end user a question, it can accept input (entities), pass these to Power Automate and then in return tailor a response, based on the outcome of your flow. In order to demonstrate this, I built a parcel history bot that would query the DHL Tracking API and return to the user the current status and event history for a valid Tracking ID. This method can be re-applied to Microsoft Lists, Dataverse, Connectors and any other 3rd party API. You can watch a demo and see how it was built in more detail below.

Setting up the Power Virtual Agent

You can build a Power Virtual Agents rapidly and deploy via Teams within minutes. It’s possible to build a PVA in Teams under existing Microsoft 365 licensing but it’s worth noting that if you want your bot to go external or beyond the standard connectors in Power Automate, as I do in my demo, you will need to license your organisation with a PVA Subscription.

When you first commission your bot, you have a fully working demo that you could make live and publish immediately. It comes with 4 sample topics to help you understand how the trigger phrases enable your bot to recognise a topic path and how the logic of the auhoring canvas plays out.

Parcel Tracking Bot Topic and Trigger Phrases

Above I established a new Topic on Parcel Tracking and setup 4 phrases around the topic of parcel tracking and DHL. Establishing the process in the authoring canvas needs an understanding of basic logic, but the nodes available to you are quite limited in that you can:

  1. Ask a question
  2. Add a condition
  3. Call an action (i.e. a call to Power Automate)
  4. Show a Message
  5. Or – Redirect to another topic

The key concepts of the authoring canvas are well described here.

The Process Map

My parcel topic will be triggered using the keywords and in response, will welcome the user and ask them for a tracking reference. It then saves the supplied reference into a variable which is then supplied to Power Automate as an Input Variable, in this case I called it trackingref. The flow is created as an action and requires the PVA trigger, with corresponding input variable, the actions in between, and the output. In my case I have two variables as Markdown Tables, the current parcel status and the event history of the Parcel.

Once my Flow has run, the bot will return the two tables back to the user and if no parcel is found, will ask the user via a condition block, if they want to search again. This is done by checking the status variable being equal to “No Match Found” which is logic I built into my flow should no parcel match be found. The PVA is then able to loop back up to the top of the process and ask once more for a valid tracking reference. If a valid parcel is found or the end user doesn’t want to search again, the conversation is ended with a survey.

Chat bot welcome
Calling a flow as an action and returning the response
a condition to check if the parcel was found
Searching again or ending the conversation

DHL Parcel API

In order for the Flow to get the tracking info of the DHL parcel, you will need a developers account which can be setup in 5 minutes. The documentation for the API I use in my demo is available here and simply requires an API key (which they provide when you register an application) and a parcel reference. You will also need premium licensing for a 3rd party API or connector but if you are keen to explore what PVA and Power Automate could do for you, you could consider setting up a Microsoft List and build a flow to retrieve data based on a filter of the get items action

Power Automate

If you want to try this solution out for yourself, the flow, in the form of a Scope, is available to download here. You can create your chat bot and paste the scope into your PVA Flow. I built my initial Flow in the Maker Portal, using a scope, and that allowed me to test the specifics of retrieving data and returning it to a compose action, without having to run my bot. By building the solution in a scope, I could easily copy it into PVA once I was ready.

My flow begins with the PVA trigger and trackingref input, which allows me to accept the input from the end user conversation. I then have a compose with my API key from DHL, a call to the HTTP action with the tracking reference as part of the URL and a header for the API Key. Then I have a condition which is setup to run on success and failure (as the HTTP call will fail if no parcel is found) and it checks to see if the shipment array is null.

If the condition is evaluated true, I simply have a compose with a string in it. This allows me to play out the rest of the flow using the full width of the editor. I do not need to put the rest of my flow in the yes container. In the no, I return the default values back to PVA of “no match found!” and a request to check that the reference was valid. The flow then terminates as succesful and no further actions will be run.

Getting the Status object and Events array involved exploring the flow history for the HTTP action, and potentially you could return more than one parcel if they share the same reference. If you do go down the route of creating a real DHL chat bot, make sure you fully explore the API docs. You will see below I have formed the expressions required to get this data and create my first status table using Markdown.

Two ways of achieving the same thing

Next I really just wanted to demonstrate how you can achieve the same output more efficiently using a select action. Below I am taking the array of parcel event data and repurposing it as an array of object strings.

Sample event array

The apply to each is the more conventional method where we take the array as input, create a string in a compose, and then bring those strings back together to form an array using outputs (Pieters Method). Using select and concat, I can build exactly the same output but in a single api call. This has huge efficiency benefit when processing large arrays.

I then use a compose with a return line to join the array of data to form the table data formatted in Markdown.

The final piece of the flow is the return action, where the output from the flow is returned back to PVA and then presented to the user via the message action. If we have found a parcel, we have two neatly formatted tables, if no parcel is found, we return an appropriate message and ask them if they want to search again.

I created this solution to demonstrate how to take your Power Virtual Agent to the next level. It’s very simple to have a conversation with a chat bot and setup multiple topics to signpost a user but the real benefit of PVA is the dynamic data. How many days holiday do I have? Can I book a day off? Who is in the office today? All of these questions could be answered by the chat bot using Power Automate and a data source via standard connectors to Microsoft 365 or Premium with an external API.

What have you used Power Virtual Agents for?

Share