grammy-inline-menu icon indicating copy to clipboard operation
grammy-inline-menu copied to clipboard

How to pass parameter to `replyToContext`

Open mikenlanggio opened this issue 1 year ago • 3 comments

Describe your goal

When the user type /start, I want to open my menu like this:
mainMenuMiddleware.replyToContext(context);

But I want to pass some parameters like this:
mainMenuMiddleware.replyToContext(context, parameterString);
And somehow, in my main menu can catch these string

Is it possible?

P/s: I know about context but I don't want to use context in this case

mikenlanggio avatar Jul 28 '24 07:07 mikenlanggio

The second argument of replyToContext is the menu path to be opened which isnt what you need. You can access stuff on the context from within the menu. So either get the information you need there or put the information into the context:

bot.command('start', async ctx => {
  ctx.state.foo = 'bar';
  await mainMenuMiddleware.replyToContext(ctx);
});

Does this help?

EdJoPaTo avatar Jul 28 '24 15:07 EdJoPaTo

The second argument of replyToContext is the menu path to be opened which isnt what you need. You can access stuff on the context from within the menu. So either get the information you need there or put the information into the context:

bot.command('start', async ctx => {
  ctx.state.foo = 'bar';
  await mainMenuMiddleware.replyToContext(ctx);
});

Does this help?

Humm. What different with context.session? Thank you

mikenlanggio avatar Jul 29 '24 14:07 mikenlanggio

The grammY docs names it ctx.config. You can basically choose your own names.

The session stays over multiple requests while this state, config or however its called is kept for this exact request (update) and won't be kept or stored somewhere. That way you can enrich the context of an individual call based on what you encounter and other middlewares after that can use that information then.

EdJoPaTo avatar Jul 29 '24 20:07 EdJoPaTo

I'll just close this due to inactivity. If you have any questions feel free to ask ahead!

EdJoPaTo avatar Oct 31 '24 17:10 EdJoPaTo