2
votes

I tried to run a script which creates a specific intent by using the Dialogflow API. The API is enabled already a couple of weeks.

After I called my script i received the following error: Error: Dialogflow API has not been used in project usable-auth-library before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=usable-auth-library then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. at /Users/$USER"/Desktop/node_modules/grpc/src/client.js:554:15 code: 7, metadata: Metadata { _internal_repr: { 'google.rpc.help-bin': [Array], 'grpc-status-details-bin': [Array], 'grpc-server-stats-bin': [Array] } } }

Here is my script.

  // Imports the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiates clients
const contextsClient = new dialogflow.ContextsClient();
const intentsClient = new dialogflow.IntentsClient();
projectId="1000-1505-ecom-conx-dev"
projectId="conrad-test"
// The path to identify the agent that owns the created intent.
const agentPath = intentsClient.projectAgentPath(projectId);

// Setup intents for ordering a pizza.

// First of all, let's create an intent that triggers pizza order flow.

// Output contexts for ordering pizza. They are used for matching follow-up
// intents. For pizza ordering intents, a "pizza" output context is used for
// hinting the conversation is about pizza ordering, not beer or something
// else. For the first intent, it returns responses asking users to provide
// size information, with a "size" output context for matching the intent
// asking for the size of the pizza.

// Note that session ID is unknown here, using asterisk.
const accountOutputContexts = [
  {
    name: contextsClient.contextPath(
      projectId,
      '*' /* sessionId */,
      'EComerceAgent'
    ),
    lifespanCount: 5,
  },
];

// The result of the matched intent.
const accountResult = {
  action: '',
  parameters: [
    {
      displayName: 'Account for',
      value: '$application',
      entityTypeDisplayName: '@application',
      mandatory: true,
      prompts: [
        'You need appliation access, please describe for which and which permissions do you need?',
        'Would you like access to jirra?',
        'Would you like access to confluence?',
        'Would you like access to AEM?',
      ],
    },
    {
      displayName: 'user',
      value: '$user',
      entityTypeDisplayName: '@user',
      mandatory: true,
      prompts: ['For wich user'],
      isList: true,
    },
    {
      displayName: 'permission',
      value: '$permission',
      // The API provides a built-in entity type @sys.address for addresses.
      entityTypeDisplayName: 'permission',
      mandatory: true,
      prompts: ['Which permission do you need?'],
    },
  ],
  messages: [
    {
      text: {
        text: [
          'No problem. We will create an account on $application for $user with the following permission: $permission'
        ],
      },
    },
    {
      text: {
        text: [
          'Reply "check" to place your order. Reply "cancel" to cancel ' +
            'your order. You can change your delivery address as well.',
        ],
      },
    },
    {
      quickReplies: {
        title:
          'No problem. We will create an account on $application for $user with the following permissions: $permission', 
        quickReplies: ['Create account', 'Cancel']
      },
      platform: 'PLATFORM_UNSPECIFIED',
    },
  ],
  outputContexts: accountOutputContexts,
};

// The phrases for training the linguistic model.
const accountPhrases = [
  {type: 'TYPE_EXAMPLE', parts: [{text: 'Get  account'}]},
  {type: 'TYPE_EXAMPLE', parts: [{text: 'acction'}]},
  {
    type: 'TYPE_EXAMPLE',
    parts: [
      {text: 'Create an account '},
      {text: 'for', entityType: '@application', alias: 'application'},
      {text: ' '},
      {text: 'for ', entityType: '@user', alias: 'user'},
      {text: 'with the followin permissions ', entityType: '@permission', alias: 'permission'},
    ],
  },
  {
    type: 'TYPE_EXAMPLE',
    parts: [
      {text: "I'd like to have access "},
      {text: 'to', entityType: '@application', alias: 'application'},
    ],
  }
];

// The intent to be created.
const accountIntent = {
  displayName: 'Account',
  events: ['create_account'],
  // Webhook is disabled because we are not ready to call the webhook yet.
  webhookState: 'WEBHOOK_STATE_DISABLED',
  trainingPhrases: accountPhrases,
  mlEnabled: true,
  priority: 500000,
  result: accountResult,
};

const accountRequest = {
  parent: agentPath,
  intent: accountIntent,
};

// Create the pizza intent
intentsClient
  .createIntent(accountRequest)
  .then(responses => {
    console.log('Created account intent:');
    logIntent(responses[0]);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });
1

1 Answers

3
votes

The error is not clear at all, it means that your are not authorized to access the remote ressource without credentials. Here is the quick solution that worked for me :

  1. Go to https://console.cloud.google.com/apis/credentials/serviceaccountkey
  2. Download json auth file (eg. foobar-123.json)
  3. Add environement variable :

    export GOOGLE_APPLICATION_CREDENTIALS="/home/me/secure/foobar-123.json"

And the full tutorial & documentation here : https://cloud.google.com/docs/authentication/getting-started


I reported this issue : https://github.com/dialogflow/dialogflow-nodejs-client-v2/issues/28