How to extract the user id from the request to Google webhook server

I am implementing an Action for Google assistant. The server fulfilling the request needs to be able to identify the user behind the request.

Part of the JSON in the request is shown below. The information inside the user object doesn't really identify the user....

      "user": {
        "lastSeen": "2019-07-29T09:07:59Z",
        "locale": "en-US",
        "userVerificationStatus": "VERIFIED"
      }

Upvotes: 0

Views: 273

Answers (1)

Ravi
Ravi

Reputation: 35589

As you can see in official document, you will not be able to get anonymous user-id from 29th July, 2019.

enter image description here

Now onwards if you need anything related to user profile, you have to use Google Sign-In for Assitstant.

Reference code from the official document:

// WITH ACCOUNT LINKING (Google Sign-In for the Assistant)
const data = "Something you want to save";
const userId = conv.user.profile.payload.sub;
saveDataToDB(userId, data);

However you can generate random number for user and store it in session for that user. Check demo code

// WITH WEBHOOK-GENERATED IDS
const data = "Something you want to save";
let userId;
// if a value for userID exists in 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 {
  // generateUUID is your function to generate ids.
  userId = generateUUID();
  conv.user.storage.userId = userId
}
saveDataToDB(userId, data);

Upvotes: 1

Related Questions