Search code examples
node.jsdialogflow-esdialogflow-es-fulfillmentdialogflow-cx

Hi is it possible to create an entity in Dialogflow using Node.js?


I'm trying to create an entity on Dialogflow using Node.js. Is it possible? If yes, how should I execute it? Thank you.


Solution

  • Yes it is possible. For reference see EntityTypesClient() for other methods you can use regarding entities.

    Prior to executing the code, make sure you have done the following as mentioned in Dialogflow nodejs quickstart.

    1. Select or create a Cloud Platform project.
    2. Enable billing for your project.
    3. Enable the Dialogflow API API.
    4. Set up authentication with a service account so you can access the API from your local workstation.

    The code example below creates an entity test_sizing with a value that have corresponding synonyms. You can also print values of response if you need information from it.

    'use strict';
    
    const dialogflow = require('@google-cloud/dialogflow');
    
    const entityClient = new dialogflow.EntityTypesClient();
    const agentPath = entityClient.projectAgentPath('your-project-id-here');
    const entityType = {
            displayName: 'test_sizing',
            kind: 'KIND_MAP',
            entities: [
            {value: 'small', synonyms: ['small', 'petit']},
            {value: 'medium', synonyms: ['medium']},
            {value: 'large', synonyms: ['large', 'big']},
          ],
    };
    const request = { parent: agentPath, entityType: entityType };
    const response = entityClient.createEntityType(request);
    

    Dialogflow output:

    enter image description here

    test_sizing entity:

    enter image description here