错误:资源名称' foo'不符合' projects / * / agent'

时间:2017-11-25 16:35:51

标签: node.js dialogflow jovo-framework

我试图在DialogFlow上创建一个新的Entity

const dialogflow = require('dialogflow');

/**
 * trains the NLP to recognize language constructs
 */
export function intTraining() {

  const ENTITY_DEFINITION_BOOLEAN = {
    parent: 'foo',
    entityType: {
      displayName: 'myBoolean',
      kind: 'KIND_MAP',
      autoExpansionMode: 'AUTO_EXPANSION_MODE_UNSPECIFIED',
      entities: [
        {value: 'true',  synonyms: [ 'yes', 'yeah', 'sure', 'okay' ]},
        {value: 'false', synonyms: [ 'no', 'no thanks', 'never' ]}
      ],
    },
  };

  // allocate
  const entityTypesClient = new dialogflow.EntityTypesClient();
  // declare promises
  const promises = [];
  // allocate entities
  prepareEntity(entityTypesClient, promises, ENTITY_DEFINITION_BOOLEAN);
  // execute state initialization
  Promise.all(promises);
}

/** Buffers an Entity onto the Promise Queue. */
function prepareEntity(entityTypesClient, promises, definition) {
  // boolean entity
  promises.push(entityTypesClient
    .createEntityType(definition)
    .then(responses => { })
    .catch(err => { console.error('', err) })
  );
}

但是,当我执行此代码时,收到以下错误:

Error: Resource name 'foo' does not match 'projects/*/agent'.

我已经使用gcloud auth application-default login在我的计算机上创建API访问凭据,并将foo配置为当前选定的项目,但这没有帮助。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要在创建实体请求中包含Dialogflow代理的完整路径,因为您的帐户可能有权访问多个代理。 Dialogflow v2 Node.js库(您似乎正在使用)有一个帮助方法,可以使用代理的项目ID(可以在your Dialogflow agent's settings中找到)为您构建代理路径。以下是code from Dialogflow's v2 Node.js samples的修改摘录,其中显示了如何构造和创建创建实体类型请求:

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

// Instantiates clients
const entityTypesClient = new dialogflow.EntityTypesClient();
const intentsClient = new dialogflow.IntentsClient();

// The path to the agent the created entity type belongs to.
const agentPath = intentsClient.projectAgentPath(projectId);

// Create an entity type named "size", with possible values of small, medium
// and large and some synonyms.
const sizeRequest = {
  parent: agentPath,
  entityType: {
    displayName: 'size',
    entities: [
      {value: 'small', synonyms: ['small', 'petit']},
      {value: 'medium', synonyms: ['medium']},
      {value: 'large', synonyms: ['large', 'big']},
    ],
  },
};

entityTypesClient.createEntityType(sizeRequest)
  .then(responses => {
    console.log('Created size entity type:');
    logEntityType(responses[0]);
  })
  .catch(err => {
    console.error('Failed to create size entity type:', err);
  })
);

答案 1 :(得分:-2)

您需要提供项目代理的精确路径。您应该使用:{/ p>,而不是在parent : foo的分配中使用ENTITY_DEFINITION_BOOLEAN

parent: 'projects/foo/agent'

这与DialogFlow预期的路径结构相匹配。