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.

alt_text

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.

alt_text

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





Kicking off I/O ‘18 with new & improved features

May 8, 2018

Today, we’re kicking off our 2nd year at Google I/O with lots of feature releases to help you build conversational experiences faster and smarter. See them at this Thursday’s I/O talk (which will be livestreamed) and come chat with us at the Google Assistant Sandbox if you’re attending the festivities!

Get started faster with new onboarding resources

New to Dialogflow? Watch our 3-part ‘Basics of Dialogflow’ video series to learn core Dialogflow concepts such as intents, entities, contexts and fulfillment.

And for those eager to start building, quickly import one of nine fully functional Dialogflow samples, complete with fulfillment code. Click on the ‘Add to Dialogflow’ button below to try out Temperature Trivia, which demonstrates how easy it is to build an Action for the Google Assistant using Dialogflow:

Temperature Trivia
Temperature Trivia

Debug using more insights and diagnostics

As developers ourselves, we know how important it is to detect errors and to receive specific and actionable help towards resolving them. The history tool has been redesigned to cleanly display conversations between your users and your agent, and flag places where your agent was unable to match an intent. It also links to diagnostics via our new Google Stackdriver Logging integration so you can easily diagnose and quickly fix issues.

history UI

We’ve also expanded the diagnostic information shown in the test console, so you can see the raw request being sent to your fulfillment webhook and the response that your webhook sends back.

diagnostics UI

Improve NLU quality by training with negative examples

We’ve heard your feedback! Developers have been asking for our natural language understanding to ignore certain phrases that mistrigger unwanted intents. You can now improve your agent’s precision by adding negative examples as training phrases for fallback intents.

negative examples
By providing the negative example 'Buy bus ticket to San Francisco' in the Default Fallback Intent, a purchase intent will not be matched to this inquiry if the agent only sells flight tickets.

Build to more Google Assistant surfaces and environments

As mentioned on the Google Developers blog, Actions will soon be available on new Smart Displays. You can start building for these visually rich surfaces in your Dialogflow console. We’ve launched support for tables, and will be adding more multimodal options in the upcoming months.

Google Assistant Table Card UI

Also announced is the ability to test your Action with groups of users before launching publicly. See how these new release environments in the Actions Console work with your Dialogflow agents. (For all other platforms, you can also try the beta release of Dialogflow versions and environments.)

Try these out

Go ahead and give these features a try! Drop by our developer community to share your feedback, subscribe to future news & updates, and follow us on Twitter. To see what others have built, check out our case studies with KLM Royal Dutch Airlines, Domino’s and Ticketmaster.

Posted by Artem Goncharuk, Engineering Lead





Dialogflow API V2 and Enterprise Edition are now generally available

April 17, 2018

Back in November 2017, we introduced two beta releases:

  1. Dialogflow API V2: the next iteration of our developer API
  2. Dialogflow Enterprise Edition: our new enterprise offering

Starting today, both releases are out of beta and generally available. If you’re currently using API V1, take note of important migration information.

Dialogflow API V2

Our API V2 now serves as the default API for all new Dialogflow agents, and all new feature upgrades will only be released for API V2. We built API V2 on Google infrastructure and incorporated the best of Google’s machine learning. Some of API V2’s benefits include Google Cloud Speech-to-Text integration, agent management, and gRPC support.

To assist with your API V2 development, we’ve updated our 7 client libraries, including Node.js, Java, and Python. We’ve also published a fulfillment library for Node.js that simplifies building webhooks, along with JSON request and response samples for use in any environment.

Dialogflow Enterprise Edition

Dialogflow Enterprise Edition, which is built on API V2, comes with Google Cloud Platform’s support and Service Level Agreement (SLA). For users that need enterprise support, check out our Enterprise blog post for more details.

Migration from API V1 to V2

We recommend that all existing agents and existing code be migrated from Dialogflow API V1 to V2, which is our most stable release. This V1 → V2 migration guide can be used as a reference. If you’re using the Actions on Google client library, migrate to version 2, which is compatible with Dialogflow API V2 and offers many additional improvements.

Stay updated with our news

In addition to today’s announcements, we’re planning a lot of new features for I/O 2018. Make sure to subscribe to our blog and follow us on Twitter, Facebook, and Google+ to stay informed!

Posted by Daniel Imrie-Situnayake, Developer Relations and Mary Chen, Product Marketing





Introducing Session Flow for visualizing conversations in Analytics

March 20, 2018

Nine months ago, we introduced the Analytics dashboard in Dialogflow to help you monitor your agent’s performance around session traffic and intent usage. Today, we’re launching a new feature in Analytics called Session Flow, an interactive visualization that maps the most common user journeys of your agent across all platforms.

session flow screenshot

The new Session Flow report can help you answer questions to improve the user experience and increase overall usage of your agent, such as:

  • Which journeys are most and least common?
  • When do exits occur across user journeys?
  • What do transitions across intents look like in conversations?

Best Buy Canada uses Session Flow to decrease its Dialogflow agent’s exit rate by 10%

Best Buy Canada uses Session Flow to better understand how users of its order-status bot journey through different intents, which intents are most popular, and where exits occur. The company also relies on this feature when running experiments to analyze how changes to its bot would affect the overall user experience. Recently, Best Buy Canada found that the exit rate for its search intent decreased by 10% after updating its fallback intent to better handle failed product searches.

Try it out

Head over to your Dialogflow console to try out Session Flow. We hope that the new feature helps you improve the user experience and increase overall usage of your agent. We’re continually working with Chatbase, the cloud service for more easily analyzing and optimizing bots, on expanding the Analytics dashboard to help you monitor and improve agent performance.

Refer to the docs for more information about interpreting Analytics and as always, let us know in our help forum if you have any questions or feedback.

Posted by Justin Kestelyn, Chatbase PMM





Introducing Dialogflow's Node.js Fulfillment Library Beta

March 15, 2018

Fulfillment is a powerful way to connect Dialogflow’s natural language capabilities with your own backend, APIs, and databases to create contextual, personalized and actionable conversational experiences for your users. Dialogflow developers are using fulfillment to allow their users to order items, retrieve user-specific information such as emails, and control devices like a photobooth. Today, we’re simplifying Node.js fulfillment development with the beta release of the Dialogflow Fulfillment Library.

The new fulfillment library works seamlessly with text, card, image and suggestion responses across v1 and v2 agents, 8 chat and voice platforms, as well as Dialogflow’s own simulator. The library also supports custom payloads, which are platform-specific responses, for all 14 Dialogflow-supported platforms. It includes an integration with the new Actions on Google client library to easily create responses for the Google Assistant. Read on to see how you can use the Dialogflow Fulfillment Library to build agents across platforms and languages, and integrate with platforms like the Google Assistant.

Cross-platform responses

The fulfillment library supports text, card, image, and suggestion chip responses for Dialogflow’s simulator and these 8 platforms: the Google Assistant, Facebook, Slack, Telegram, Kik, Skype, Line, and Viber. See how to add text, card, and suggestions with the code below and what the responses look like on the Google Assistant, Dialogflow’s simulator, Slack, and Facebook Messenger:

const functions = require('firebase-functions');
const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const agent = new WebhookClient({ req, res });
  
  function intentHandler(agent) {
    agent.add('This message is from Dialogflow\'s Cloud Functions for Firebase editor!');
    agent.add(new Card({
        title: 'Title: this is a card title',
        imageUrl: 'https://developers.google.com/actions/assistant.png',
        text: 'This is the body text of a card.  You can even use line\n  breaks and emoji! 💁',
        buttonText: 'This is a button',
        buttonUrl: 'https://assistant.google.com/'
      })
    );
    agent.add(new Suggestion('Quick Reply'));
    agent.add(new Suggestion('Suggestion'));
  }

  agent.handleRequest(intentHandler);
});

Copy and paste the above code into Dialogflow’s inline editor to try it now or check out the full quick start sample here.

Google Assistant Dialogflow's simulator
alt_text alt_text
Slack Facebook Messenger
alt_text alt_text

See the reference documentation for other rich response types, such as images.

Multilingual responses

You can also build multilingual and locale fulfillment using the locale attribute. See how to say hello in French or English depending on the language of the request with the code below:

const functions = require('firebase-functions');
const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const agent = new WebhookClient({ req, res });
  function welcome(agent) {   // English handler function & intent map
    agent.add('Welcome to my agent!');
  } 
  function bienvenue(agent) {  // French handler function & intent map
    agent.add('Bienvenue à mon agent!');
  }
  
  if (agent.locale === 'en') {
    agent.handleRequest(welcome);
  } else if (agent.locale === 'fr') {
    agent.handleRequest(bienvenue);
  }
});

Copy and paste the above code into Dialogflow’s inline editor to try it now or check out the full multilingual and locale sample here.

Custom payloads and the Google Assistant Integration

The fulfillment library supports custom payload responses, which are platform-specific features such as authentication and transactions used by Dialogflow-supported platforms. These payloads will be sent to the target platform in place of other messages defined in the library. The example below shows how to add custom JSON payloads for the Google Assistant and Slack:

agent.add(new Payload(agent.ACTIONS_ON_GOOGLE, {/*your Google payload here*/});
agent.add(new Payload(agent.SLACK, {/*your Slack payload here*/});

Dialogflow’s fulfillment library also has an integration with the Google Assistant using Actions on Google’s v2 alpha client library. This integration enables creation of custom Actions on Google payloads through the Actions on Google v2 client library. Here is an example of how to ask for the user’s location through the Google Assistant:

const functions = require('firebase-functions');
const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');
const {Permission} = require('actions-on-google');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const agent = new WebhookClient({ req, res });

  function intentHandler(agent) {
    let conv = agent.conv();
    conv.ask(new Permission({
      context: 'To give results in your area',
      permissions: 'DEVICE_PRECISE_LOCATION',
    }))
    agent.add(conv);
  }

  agent.handleRequest(intentHandler);
});

Copy and paste the above code into Dialogflow’s inline editor to try it now or check out the full Actions on Google sample here.

Next steps

Check out the Dialogflow fulfillment library on Github and npm, along with our quick start sample and getting started guide to get up and running today. Send us feedback, feature requests or bugs by opening an issue on GitHub.

We’ll be discussing the library, your feedback and feature requests on Dialogflow’s Google+ community. We look forward to hearing from you and seeing what you build with the Dialogflow fulfillment library!

Posted by Matt Carroll, Dialogflow Developer Relations





Subscribe Via Email

Enter your email address:

Delivered by FeedBurner

About

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

Resources

Follow us on social