Search code examples
node.jsbotframework

Bot Framework v4 Node: How to use userState in a base class


For a multi language bot running on Messenger I need to store the current language of the user in botState. This works fine for most components:

In Index.js the userState instance is created and passed to the main dialog

const userState = new UserState(memoryStorage);
const dialog = new RequestProcessing(userState);
const bot = new DialogBot(conversationState, userState, dialog);

From within the main dialog, I am able to create accessors and pass userState to my componentdialogs

    constructor(userState) {
        super(LEAD_CAPTURE_DIALOG);
        this.userState = userState;
        this.userProfileAccessor = userState.createProperty(USER_PROFILE);
        this.addDialog(new LeadCaptureDialog(userState));

Using userState in my dialogcomponents works perfectly like this.

However: I am also using a base class called CancelAndHelpDialog (based on this sample ) to handle interrupts like cancel and help in my componentdialogs.

I don't know how to pass userState to this base class.

Any guidance is much appreciated


Solution

  • It looks like you already know how to make the user state a parameter of your main dialog's constructor. You can do the same in your cancel and help dialog's constructor, and pass the user state to it when you call super.

    https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/super