How to identify unique users with Diagflow

让人想犯罪 __ 提交于 2019-12-01 05:56:55

You have a couple of options, depending on your exact needs.

Simple: userStorage

Google provides a userStorage object which is persisted across conversations when it can identify a user. This lets you store your own identifier when you need to track when a user returns.

The easiest way to do this is to check the userStorage object for the identifier when your webhook is called. If it doesn't exist, create one using something like a v4 UUID and save it in the userStorage object.

If you are using the actions-on-google library, the code might look something like this:

let userId;
// if a value for userID exists un user storage, it's a returning user so we can
// just read the value and use it. If a value for userId does not exist in user storage,
// it's a new user, so we need to generate a new ID and save it in user storage.
if (userId in conv.user.storage) {
  userId = conv.user.storage.userId;
} else {
  // Uses the "uuid" package. You can get this with "npm install --save uuid"
  var uuid = require('uuid/v4');
  userId = uuid();
  conv.user.storage.userId = userId
}

If you are using the dialogflow library, you can use the above, but you'll need this line first:

let conv = agent.conv();

If you're using the multivocal library, it does all of the above for you and will provide a UserID in the environment under the path User/Id.

If you're handling the JSON directly, and you are using the Dialogflow v2 protocol, you can get the userStorage object by examining originalDetectIntentRequest.payload.user.userStorage in the JSON request object. You'll set the payload.google.userStorage object in the JSON response. The code is similar to the above and might look something like this:

let userStorage = body.originalDetectIntentRequest.payload.user.userStorage || {};
let userId;
// if a value for userID exists un user storage, it's a returning user so we can
// just read the value and use it. If a value for userId does not exist in user storage,
// it's a new user, so we need to generate a new ID and save it in user storage.
if (userId in userStorage) {
  userId = userStorage.userId;
} else {
  // Uses the "uuid" package. You can get this with "npm install --save uuid"
  var uuid = require('uuid/v4');
  userId = uuid();
  userStorage.userId = userId
}

// ... Do stuff with the userID

// Make sure you include the userStorage as part of the response
var responseBody = {
  payload: {
    google: {
      userStorage: JSON.stringify(userStorage),
      // ...
    }
  }
};

Note the first line of the code - if userStorage doesn't exist, use an empty object. It won't exist until you send a response that includes storing something in it for the first time, which will happen in the last few lines of this code.

Advanced: Account Linking

You can request users to sign in to your Action using Google Sign In. This can be done just using voice for the simplest of cases and would only interrupt the flow the first time.

After this, your Action is given a JWT which contains their Google ID which you can use as their identifier.

If you're using the actions-on-google library, you can get the ID from the decoded JWT with a line such as:

const userId = conv.user.profile.payload.sub;

In the multivocal library, the ID from the decoded JWT is available in the environment under the path User/Profile/sub

Deprecated: Anonymous User ID

You'll see some answers here on StackOverflow that reference an Anonymous User ID. Google has deprecated this identifier, which was not always a reliable way to verify returning users, and will be removing it 1 Jun 2019.

This code is currently still being sent, but this will be removed starting 1 Jun 2019.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!