如何创建特定于用户的实体?

How can I create an entity specific to a user?

我正在使用 Dialogflow 和 actions-on-google-nodejs that accesses the GitKraken Glo API 为 Google Assistant 创建一个操作,以将卡片添加到人们的看板中。我正在使用帐户链接验证我的用户。我希望我的用户能够说 Add a card to [board name]Add a card 之类的话。如果没有给出板名称,我希望操作提示用户输入它。我如何创建一个会话实体来获取登录用户的所有面板名称?

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.

conv.user.email

您可以使用 conv.user 对象 :

const Users = {};

app.intent('Get Signin', (conv, params, signin) => {
  if (signin.status === 'OK') {
    const email = conv.user.email
    Users[email] = { };
    conv.ask(`I got your email as ${email}. What do you want to do next?`)
  } else {
    conv.ask(`I won't be able to save your data, but what do you want to next?`)
  }
})

app.intent('actions.intent.TEXT', (conv, input) => {
  if (signin.status === 'OK') {
    Users[conv.user.email] = {
      lastinput: input
    };
  }
});

conv.id

此外,conv id 是当前对话的唯一 ID。

// Create an app instance
const app = dialogflow()

// Register handlers for Dialogflow intents

const Users = {};

app.intent('Default Welcome Intent', conv => {
   Users[conv.id] = {
       conversationId: conv.id,
       name: '1234'
   };
})

app.intent('actions.intent.TEXT', (conv, input) => {
   Users[conv.id] = {
       lastinput: input
   };
});

app.intent('Goodbye', conv => {
  delete Users[conv.id];
})

要使用会话实体,您首先需要做几件事:

  • 实体类型需要已经存在。会话实体更新现有的。最简单的方法是在 Dialogflow UI 中创建您想要的实体。它不需要包含任何实体,但默认设置一个实体会很有用。
  • 您需要一个 Service Account 用于您在 Google Cloud 中的项目,它将执行更新,以及此帐户的密钥。
  • 如果您使用图书馆,例如 dialogflow-nodejs 图书馆,您的生活会变得 很多

通常,您的代码需要执行以下操作,通常是在用户首次启动会话时(即 - 在您的 Welcome Intent Handler 中):

  • 获取看板列表
  • 更新会话实体类型,为每个板创建一个实体。进行此更新涉及:

该库的自述文件包含指向示例代码的链接,这些示例代码介绍了如何使用 nodejs 库执行此操作。我拥有的执行此工作的代码具有如下功能:

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 );
}