UNPKG

2.15 kBPlain TextView Raw
1/**
2 * @module botkit
3 */
4/**
5 * Copyright (c) Microsoft Corporation. All rights reserved.
6 * Licensed under the MIT License.
7 */
8import { Activity, ConversationState, TurnContext } from 'botbuilder';
9
10/**
11 * A customized version of [ConversationState](https://docs.microsoft.com/en-us/javascript/api/botbuilder-core/conversationstate?view=botbuilder-ts-latest) that overide the [getStorageKey](#getStorageKey) method to create a more complex key value.
12 * This allows Botkit to automatically track conversation state in scenarios where multiple users are present in a single channel,
13 * or when threads or sub-channels parent channel that would normally collide based on the information defined in the conversation address field.
14 * Note: This is used automatically inside Botkit and developers should not need to directly interact with it.
15 * @ignore
16 */
17export class BotkitConversationState extends ConversationState {
18 public getStorageKey(context: TurnContext): string | undefined {
19 const activity: Activity = context.activity;
20 const channelId: string = activity.channelId;
21 if (!activity.conversation || !activity.conversation.id) {
22 throw new Error('missing activity.conversation');
23 }
24
25 // create a combo key by sorting all the fields in the conversation address and combining them all
26 // mix in user id as well, because conversations are between the bot and a single user
27 const conversationId: string = Object.keys(activity.conversation).filter((key) => { return key !== 'properties'; }).sort().map((key) => activity.conversation[key]).filter((val) => val !== '' && val !== null && typeof val !== 'undefined').join('-') + '-' + activity.from.id;
28
29 if (!channelId) {
30 throw new Error('missing activity.channelId');
31 }
32
33 if (!conversationId) {
34 throw new Error('missing activity.conversation.id');
35 }
36
37 // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
38 // @ts-ignore namespace is technically private
39 return `${ channelId }/conversations/${ conversationId }/${ this.namespace }`;
40 }
41}