Search code examples
node.jsdialogflow-esactions-on-googlegoogle-assistant-sdk

How can I create an entity specific to a user?


I'm creating an action for Google Assistant with Dialogflow and actions-on-google-nodejs that accesses the GitKraken Glo API to add cards to people's boards. I'm authenticating my users with Account Linking. I want my users to be able to say things like Add a card to [board name] or Add a card. If a board name isn't given I want the action to prompt the user for it. How can I create a session entity that get's all the board names for the logged in user?

Sorry if this doesn't make much sense, I'm pretty new to Actions on Google and Dialogflow. Feel free to ask questions for clarity.


Solution

  • There are a few things you'll need to do first to use a Session Entity:

    • The Entity Type needs to already exist. Session Entities update existing ones. The easiest way to do this is to create the Entity you want in the Dialogflow UI. It doesn't need to have any Entities in it, but having one as a default can be useful.
    • You need a Service Account for your project in Google Cloud that will do the update, and a secret key for this account.
    • Your life will be a lot easier if you use a library, such as the dialogflow-nodejs library.

    In general, your code needs to do the following, typically when the user first starts the session (ie - in your Welcome Intent Handler):

    • Get the list of boards
    • Update the Session Entity Type, creating an Entity for each board. Doing this update involves:

    The README for the library includes links to sample code about how to do this using the nodejs library. Code that I have that does this work has a function like this:

    function setSessionEntity( env, entityType ){
      const config = envToConfig( env );
      const client = new dialogflow.SessionEntityTypesClient( config );
    
      let parent = env.dialogflow.parent;
      if( entityType.displayName && !entityType.name ){
        entityType.name = `${parent}/entityTypes/${entityType.displayName}`;
      }
      if( !entityType.entityOverrideMode ){
        entityType.entityOverrideMode = 'ENTITY_OVERRIDE_MODE_OVERRIDE';
      }
    
      const request = {
        parent: parent,
        sessionEntityType: entityType
      };
      return client.createSessionEntityType( request );
    }