L
L
Lynatik0012020-08-04 21:43:19
Node.js
Lynatik001, 2020-08-04 21:43:19

How to pass a variable from one scene to another?

How can I transfer a variable from one scene to another? I have a transition that happens when you click on the Keyboard button - the one that prints the text in the chat when you click it. Duck that there won't be any kind of callback?
There is a vario to lay down in a session in 1 scene and take it in the second. But I think there is a better way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2020-08-04
@IgorPI

Session and its abstract concept.
I recently wrote a universal bot, although I had no experience in building bots.
And so, just there there is a session.
The session allows you to change the behavior of the dialogue as the play progresses.
Let me give you an example of my bike.
Session Interface

export interface SessionInterface {
    bot: BotInterface;
    state: SessionStateInterface;
    initialState: SessionStateInterface;
    SID: string;
    isNew: boolean;
    inDialog: boolean;
    user: UserInterface;
    send(message: any, options?: any): Promise<any>;
    resetState(): void;
    setState(state: SessionStateInterface): void;
}

Session state interface
export interface SessionStateInterface {
    [key: string]: any;
}

And this is the implementation of the described interfaces
interface SessionConstructorPropsInterface {
    bot: BotInterface;
    initialState?: SessionStateInterface;
}

export class Session implements SessionInterface {
    bot: BotInterface;
    user: UserInterface;
    state: SessionStateInterface;
    initialState: SessionStateInterface;
    SID: string;
    isNew: boolean;
    inDialog: boolean;
    constructor({  bot, initialState }: SessionConstructorPropsInterface) {
        this.isNew = true;
        this.bot = bot;
        this.initialState = initialState || {};
        this.state = Object.assign({}, initialState);
    }

    send(message: any, options: any): Promise<void> {
        this.isNew = false;
        return this.bot.adapter.send(message, options);
    }

    resetState(): void {
        this.state = Object.assign({}, this.initialState);
    }

    setState(state: SessionStateInterface): void {
        Object.keys(state).forEach((key) => {
            this.state[key] = state[key];
        });
    }
}

For example, in the dialog | dialog handler
main.use(/\/login/i, async ({message, session}: BotContextInterface, next) => {

  // Здесь я сохраняю состояние
  session.setState({
    processLogin: true,
    processLoginStep: 'login'
  })

  // Если мне нужно получить состояние
  console.log(session.state.processLogin)

});

The above code allows you to store the state of the bot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question