How do I get user entitlements to persist across c

2019-08-18 00:56发布

问题:

I am trying to see if a user has purchased a product by checking conv.user.entitlements but except for immediately after purchase, this is empty.

This value seemed to be persisting between conversations previously, now it is always empty. I am able to see the entitlement using conv.user.entitlements immediately after purchase, but then it is gone.

 if (arg.purchaseStatus === 'PURCHASE_STATUS_OK') {
 console.log('purchase2 ', conv.user.entitlements);
//this works as expected showing the entitlement

In the logs I see: purchase2 [ { entitlements: [ [Object] ], packageName: 'com.chad.myfirstapp' } ]

But when I try to log the same in the next conversation, I get:

purchase2 [ ]

回答1:

While the conv.user object is capable of storing data across conversations, there are both technical and legal limitations to keep in mind, documented here.

When the Assistant can match an identity to the user, the contents of user storage never expires, and only the user or the Action itself can clear it.

When the Assistant can't match an identity to the user, the content of user storage is cleared at the end of the conversation. Examples of cases where the Assistant can't match an identity to the user are:

  • Voice match is set up and there is no match.
  • The user disabled personal data.

In addition to the conv.user object, you can also store data in your preferred database. This Github sample demonstrates how to connect Dialogflow to a Firebase Firestore database. You can find the write function here:

  function writeToDb (agent) {

    // Get parameter from Dialogflow with the string to add to the database
    const databaseEntry = agent.parameters.databaseEntry;

    // Get the database collection 'dialogflow' and document 'agent' and store
    // the document  {entry: "<value of database entry>"} in the 'agent' document
    const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
    return db.runTransaction(t => {
      t.set(dialogflowAgentRef, {entry: databaseEntry});
      return Promise.resolve('Write complete');
    }).then(doc => {
      agent.add(`Wrote "${databaseEntry}" to the Firestore database.`);
    }).catch(err => {
      console.log(`Error writing to Firestore: ${err}`);
      agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
    });
  }


回答2:

If you just want to see if a user has purchased an item in the past, use the conv.user.storage to store either a boolean whether the user has purchased something in the past:

conv.user.storage.hasPurchasedItem = true

Or perhaps you can make an array of purchases in conv.user.storage and check its length:

conv.user.storage.purchases = conv.user.storage.purchases || [] conv.user.storage.purchases.push({item: bananas, cost: 0.99})