使用npm programm调用dialogflow api失败

时间:2017-12-22 12:52:27

标签: node.js npm gcloud gcp dialogflow

我尝试使用Dialogflow API运行一个创建特定意图的脚本。该API已经启用了几周。

我调用脚本后收到以下错误: 错误:Dialogflow API尚未在项目可用的auth-library中使用,或者在禁用之前。访问https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=usable-auth-library启用它,然后重试。如果您最近启用了此API,请等待几分钟,以便将操作传播到我们的系统并重试。     at /Users/$USER"/Desktop/node_modules/grpc/src/client.js:554:15   代码:7,   元数据:    元数据{      _internal_repr:       {'google.rpc.help-bin':[数组],         'grpc-status-details-bin':[数组],         'grpc-server-stats-bin':[Array]}}}

这是我的剧本。

  // 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 个答案:

答案 0 :(得分:3)

错误根本不明确,这意味着没有凭据,您无权访问远程资源。以下是适用于我的快速解决方案:

  1. 转到https://console.cloud.google.com/apis/credentials/serviceaccountkey
  2. 下载json auth文件(例如foobar-123.json
  3. 添加environement变量:

    导出GOOGLE_APPLICATION_CREDENTIALS =“/ home / me / secure / foobar-123.json”

  4. 完整的教程&文档:https://cloud.google.com/docs/authentication/getting-started

    我报告了此问题:https://github.com/dialogflow/dialogflow-nodejs-client-v2/issues/28