如何在新版Dialogflow V2 API中使用userStorage?
Dialogflow API V2: Working with User Storage (Compared to V1)
First, let's recap the simple approach you already know from V1 since that's your baseline:
- Setting a stored value:
app.userStorage.favoriteColor = 'blue'; - Retrieving it later:
let myFavoriteColor = app.userStorage.favoriteColor;
Now for V2, the logic is almost identical—you just use the conv object instead of app. Here's how to replicate that functionality step by step:
Storing Data in V2
In your intent handler, use conv.user.storage to save custom values. For example, storing a favorite color would look like this:
app.intent('set_favorite_color', (conv) => { // Assume we've pulled the color from user input here const userColor = 'blue'; conv.user.storage.favoriteColor = userColor; conv.close(`Got it! I'll remember your favorite color is ${userColor}.`); });
Retrieving Stored Data in V2
In any subsequent session or intent, you can fetch that saved value using the same conv.user.storage object:
app.intent('get_favorite_color', (conv) => { const savedColor = conv.user.storage.favoriteColor; if (savedColor) { conv.close(`Your favorite color is ${savedColor}, right?`); } else { conv.ask(`I don't remember your favorite color yet—what is it?`); } });
Quick Key Notes
- Just like V1,
conv.user.storagepersists across sessions for the same user, so your data stays intact between interactions. - The structure is nearly a 1:1 swap—V1 uses
app.userStorage, V2 usesconv.user.storage(notice the extra dot betweenuserandstorage). - That weather report example you saw with
conv.user.storage.zipfollows this exact pattern—you can store any custom property you need here, not just zip codes or colors.
内容的提问来源于stack exchange,提问作者Geoff
相关产品推荐
相关产品推荐

