59 lines
No EOL
2.5 KiB
JavaScript
59 lines
No EOL
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PrivateConversationState = void 0;
|
|
const botState_1 = require("./botState");
|
|
const NO_KEY = 'PrivateConversationState: overridden getStorageKey method did not return a key.';
|
|
/**
|
|
* Reads and writes PrivateConversation state for your bot to storage.
|
|
*
|
|
* @remarks
|
|
* Each PrivateConversation your bot has with a user or group will have its own isolated storage object
|
|
* that can be used to persist PrivateConversation tracking information between turns of the PrivateConversation.
|
|
* This state information can be reset at any point by calling [clear()](#clear).
|
|
*
|
|
* ```JavaScript
|
|
* const { PrivateConversationState, MemoryStorage } = require('botbuilder');
|
|
*
|
|
* const PrivateConversationState = new PrivateConversationState(new MemoryStorage());
|
|
* ```
|
|
*/
|
|
class PrivateConversationState extends botState_1.BotState {
|
|
/**
|
|
* Creates a new PrivateConversationState instance.
|
|
*
|
|
* @param storage Storage provider to persist PrivateConversation 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 PrivateConversation state.
|
|
*
|
|
* @param context Context for current turn of PrivateConversation with the user.
|
|
* @returns The storage key for the current PrivateConversation state.
|
|
*/
|
|
getStorageKey(context) {
|
|
const activity = context.activity;
|
|
const channelId = activity.channelId;
|
|
const conversationId = activity && activity.conversation && activity.conversation.id ? activity.conversation.id : undefined;
|
|
const userId = activity && activity.from && activity.from.id ? activity.from.id : undefined;
|
|
if (!channelId) {
|
|
throw new Error('missing activity.channelId');
|
|
}
|
|
if (!conversationId) {
|
|
throw new Error('missing activity.conversation.id');
|
|
}
|
|
if (!userId) {
|
|
throw new Error('missing activity.from.id');
|
|
}
|
|
return `${channelId}/conversations/${conversationId}/users/${userId}/${this.namespace}`;
|
|
}
|
|
}
|
|
exports.PrivateConversationState = PrivateConversationState;
|
|
//# sourceMappingURL=privateConversationState.js.map
|