RegEx entity in Power Virtual Agents

Microsoft has introduced a new feature called RegEx entities in Power Virtual Agents. This blog shows how you can use RegEx entities to validate input of a user who interacts with a Power Virtual Agent.

Regular Expressions

Regular Expressions have always been a great way of validating field values. It is a sequence of characters which defines a search pattern. And now it is possible to use your own regular expressions in Power Virtual Agents: Regex (regular expression) entity support in Power Virtual Agents.

UK Postcode example

To explore this feature I am going to use a challenging example, UK Postcodes. There has been a lot of debate about this one, but for demo purposes I am using the expression below:

Kudos to @RyanMaclean365 for the UK Postcode regex on his site and some nice other examples in his blog, Ten RegEx Expression to use in Forms Pro

GetAddressIO

Before you start with the Power Automate flow and the Power Virtual Agent setup you need to make sure you request a 30-day trial API key at GetAddressIO. Obviously there are also other services out there. For me this was just the easiest one to setup for a demo.

The flow steps

getaddressio_flowsteps

1. Create a flow with a When Power Virtual Agents calls a flow trigger action. Add a text Postcode input field and a House number input field to that action.
pva_findlocation_start

2. Add three Initialize variable actions. Below is a table with the name, type and value for each of the variables.

NameTypeValue
ApiKeyStringYour key from the trial
HouseString@{triggerBody()[‘number’]}
PostcodeString@{triggerBody()[‘text’]}

threevariables

3. Add a HTTP action. Use the GET method to interact with the Find Method of the Get Address IO API. Use the URI from below.

addressio_getaddress_http

4. Add a Return value(s) to Power Virtual Agents action. Use the following expression.
In this expression I am retrieving the 10th item of the result set. It probably makes more sense to retrieve the first item. In that case replace [9] by the index reference [0].

pva_findlocation_end

The bot steps

findlocation_topic

1. Create a new blank topic.

topicaccounts

2. Give it a name, in this case FindLocation. Add some trigger phrases. Via these you can interact with your topic. Save the topic.

3. Click Entities on the left menu. Click New Entity. Select Regular expression (Regex) option in the dialog.

createregexentity_pva

4. Add a name, description and a pattern for your new RegEx entity. I used UK Postcode and the pattern below. Save the entity.

pattern_regex

5. Go to the Authoring canvas of your topic. Add the first question, What is your postcode?. You now should be able to select your new RegEx UK Postcode entity from the list for this question.

whatisyourpostcodequestion

6. Add another question, What is your house number? This time use the Number type for the question.

whatisyourhousenumber

7. Add a Call an action. Connect it to your earlier created flow. Make sure you map the variables of the questions to the input fields of the flow.

callanaction_getaddressio

8. Add a message action. Use the AddressFound variable in the body of the message to show the address which the bot has found to the user.

showmessage_addressfound

Testing the final result

Test the bot with some data. As you can see it will respond with Sorry, I didn’t understand if it isn’t a postcode which matches our Regular Expression.

ukpostcode_bot

Happy testing!

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.