user7885981
user7885981

Reputation:

How can I create an entity specific to a user?

I'm creating an action for Google Assistant with Dialogflow and actions-on-google-nodejs that accesses the GitKraken Glo API to add cards to people's boards. I'm authenticating my users with Account Linking. I want my users to be able to say things like Add a card to [board name] or Add a card. If a board name isn't given I want the action to prompt the user for it. How can I create a session entity that get's all the board names for the logged in user?

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.

Upvotes: 2

Views: 1077

Answers (2)

Prisoner
Prisoner

Reputation: 50721

There are a few things you'll need to do first to use a Session Entity:

  • The Entity Type needs to already exist. Session Entities update existing ones. The easiest way to do this is to create the Entity you want in the Dialogflow UI. It doesn't need to have any Entities in it, but having one as a default can be useful.
  • You need a Service Account for your project in Google Cloud that will do the update, and a secret key for this account.
  • Your life will be a lot easier if you use a library, such as the dialogflow-nodejs library.

In general, your code needs to do the following, typically when the user first starts the session (ie - in your Welcome Intent Handler):

  • Get the list of boards
  • Update the Session Entity Type, creating an Entity for each board. Doing this update involves:

The README for the library includes links to sample code about how to do this using the nodejs library. Code that I have that does this work has a function like this:

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

Upvotes: 2

user2226755
user2226755

Reputation: 13183

conv.user.email

You can use conv.user object :

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

Also with conv id is unique id for the current conversation.

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

Upvotes: 0

Related Questions