Validate entities using regular expressions in fulfillment

August 23, 2018

Entities are powerful tools used for extracting parameter values from natural language inputs. Dialogflow developers use entities to retrieve important information from users, such as dates & times for appointments, locations for shipping orders, and numbers. Dialogflow already contains built-in system entities for common concepts like dates, times, numbers and more. In this post, we’ll show you how to use regular expressions to validate custom entities with fulfillment.

As an example, let’s imagine an organization has alphanumeric Employee IDs that are always six characters in length, and that the letters and numbers can appear in any position within the IDs. Here are the steps to validate if the Employee ID provided by the user complies with the format:

Let’s begin by creating an intent and adding 15-20 different Training Phrases to it. Try to add as many of the most common examples your employees might use to communicate their ID. Since we are looking for validating specific keywords associated with an entity, such words/phrases need to be identified in the user query and mapped with an entity.

In this case, even though we know the length of the ID and what it should comprise of, there is no specific format in which the letters and numbers should appear in it. Since the keyword does not comply with any predefined entity format (and it’s not possible to create a custom developer entity for this), we recommend using @sys.any as a wildcard entity here. To do that, highlight the keyword in the training phrases and choose @sys.any from the list of entities.

Since we suggest using @sys.any for this, please consider a few points in mind to improve matching accuracy:

  1. A little higher number of examples than the generally recommended number (10-15) should be provided in the intent.
  2. Avoid mapping the entire training phrase to @sys.any
  3. If possible, prompt users for confirmation before fulfilling the request.
  4. You could also consider restricting this intent with an input context, if possible.

For instance, in the screenshot below, an intent “Get Employee ID” is created and the Employee ID is annotated in the training phrases to @sys.any.


Now that we have extracted the required keyword from the user query, we need to check if it is in the expected format or not. This check would be done in the fulfillment code by comparing this keyword with the desired regex pattern. We would, therefore, need to enable webhook fulfillment for this intent. To do this, click Fulfillment at the bottom of the page to reveal the options. Choose Enable webhook call for this intent and click the SAVE button as shown in the screenshot below.


The next step involves writing the fulfillment code for the validateEmployeeID intent. This would further involve two sub-steps:

  1. Map the intent to a function: This will let the fulfillment know which block of code to trigger. You can create a map (like intentMap in the code snippet below) to associate an action handler with the name of an intent. This would indicate to the fulfillment what steps should be taken to complete the user’s request when it matches a particular intent. In our example, we have created an action handler ‘validateEmployeeID’ for the intent ‘Get Employee ID’.

    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('Get Employee ID', validateEmployeeID);
  2. Implement logic in the action handler to validate the length and format of the ID: In order to validate if the ID is in the correct format or not, we would need to ensure that the ID provided in the user’s request is passed onto the fulfillment. This is done with the help of parameters. Parameters are elements generally used to connect words in a user’s response, to entities. Please note that we have created one such parameter ‘employeeID’ already and its value can be extracted in the fulfillment code from the JSON request body (field name: queryResult.parameters). Once we have the Employee ID extracted in the fulfillment, we try to match it with a regex pattern specified in the fulfillment code for the required format.

Please NOTE that you would need to modify the regex pattern based on your specific use-case.

The code snippet that implements the above step looks like this:

function validateEmployeeID (agent) {
    // get the employee ID parameter from the request received from Dialogflow
    let employeeID = agent.parameters.employeeID;
    let pattern = /[^a-zA-Z0-9]/;
    if (employeeID.length !== 6) { 
        agent.add(`The length of the Employee ID should be six characters.Please enter the correct ID.`); 
    } else if (employeeID.match(pattern) !== null) { 
        agent.add(`Employee ID should have only letters or numbers. Please enter the correct ID.`); 

You can find the complete sample code for this functionality on Github. Click on the button below to try out the sample now!

Drop by Dialogflow’s Google+ community to share your feedback and subscribe to our blog for more such tutorials. We look forward to hearing from you!

Posted by Surbhee Sehgal, Partner Technology Manager, Dialogflow

Subscribe Via Email

Enter your email address:

Delivered by FeedBurner


Welcome to the Dialogflow blog. Subscribe to get product updates, best practices, and tutorials directly in your inbox.


Follow us on social