cancel
Showing results for 
Search instead for 
Did you mean: 
Expiscornovus

RegEx entity in Power Virtual Agents

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:

 

 

^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$

 

 

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.

 

Also be aware that this is using a premium connector in the Power Automate flow, which might require an additional license.

 

The flow steps

 

getaddressio_flowsteps.png

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.png

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

 

Name Type Value
ApiKey String Your key from the trial
House String @{triggerBody()['number']}
Postcode String @{triggerBody()['text']}

 

threevariables.png

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.

 

 

https://api.getAddress.io/find/@{variables('PostCode')}/@{variables('House')}?api-key=@{variables('ApiKey')}&expand=true

 

addressio_getaddress_http.png


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].

 

 

concat(outputs('HTTP_-_Find')['body']['addresses'][9]['line_1'], ', ',\r\noutputs('HTTP_-_Find')['body']['addresses'][9]['line_2'], ', ',\r\noutputs('HTTP_-_Find')['body']['addresses'][9]['town_or_city'])

 

 

pva_findlocation_end.png

 

 

The bot steps

findlocation_topic.png

1. Create a new blank topic.

 

createtopic.png

 

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.jpg

 

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

 

 

^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$

 

 

pattern_regex.png

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.png

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

whatisyourhousenumber.png

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.png

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.png


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.png

 

Happy testing!

 

Cross Posted from:
https://www.expiscornovus.com/2021/09/26/regex-entity-in-power-virtual-agents/