54 lines
No EOL
2 KiB
JavaScript
54 lines
No EOL
2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.UserState = void 0;
|
|
const botState_1 = require("./botState");
|
|
const NO_KEY = 'UserState: overridden getStorageKey method did not return a key.';
|
|
/**
|
|
* Reads and writes user state for your bot to storage.
|
|
*
|
|
* @remarks
|
|
* Each user your bot communicates with will have its own isolated storage object that can be used
|
|
* to persist information about the user across all of the conversation you have with that user.
|
|
*
|
|
* ```JavaScript
|
|
* const { UserState, MemoryStorage } = require('botbuilder');
|
|
*
|
|
* const userState = new UserState(new MemoryStorage());
|
|
* ```
|
|
*/
|
|
class UserState extends botState_1.BotState {
|
|
/**
|
|
* Creates a new UserState instance.
|
|
*
|
|
* @param storage Storage provider to persist user state to.
|
|
* @param namespace (Optional) namespace to append to storage keys. Defaults to an empty string.
|
|
*/
|
|
constructor(storage, namespace = '') {
|
|
super(storage, (context) => {
|
|
// Calculate storage key
|
|
const key = this.getStorageKey(context);
|
|
return key ? Promise.resolve(key) : Promise.reject(new Error(NO_KEY));
|
|
});
|
|
this.namespace = namespace;
|
|
}
|
|
/**
|
|
* Returns the storage key for the current user state.
|
|
*
|
|
* @param context Context for current turn of conversation with the user.
|
|
* @returns The storage key for the current user state.
|
|
*/
|
|
getStorageKey(context) {
|
|
const activity = context.activity;
|
|
const channelId = activity.channelId;
|
|
const userId = activity && activity.from && activity.from.id ? activity.from.id : undefined;
|
|
if (!channelId) {
|
|
throw new Error('missing activity.channelId');
|
|
}
|
|
if (!userId) {
|
|
throw new Error('missing activity.from.id');
|
|
}
|
|
return `${channelId}/users/${userId}/${this.namespace}`;
|
|
}
|
|
}
|
|
exports.UserState = UserState;
|
|
//# sourceMappingURL=userState.js.map
|