EdJoPaTo / grammy-inline-menu

Inline Menus for Telegram made simple. Successor of telegraf-inline-menu.
MIT License
354 stars 46 forks source link

How can i use toggle with pagination #152

Closed petroShved closed 3 years ago

petroShved commented 3 years ago

I need to create pagination with some toggle button, for example, events with pagination, and when I can click the toggle button for subscribing for the event. The problem is when I change page, click the toggle button, context.session in toggle button is undefined, and i can't get page number to identify event id

EdJoPaTo commented 3 years ago

Im not sure I understand what you are trying to achive. Many toggle buttons sounds like a good usecase for menu.select with its included pagination support. Check the readme for My choose/select has too many choices. Can I use a pagination?.

Does that already help with your problem? Otherwise can you create a simple example of what does not seem to be working?

petroShved commented 3 years ago

image So as you can see i have here pagination(text+button(toggle)+button(url)). What I want to achieve is toggle status separate for page.

events.toggle('Subscribe', 'toggle me', {
  set: (ctx, newState) => {
    const indexItem = ctx.session && ctx.session.page ? ctx.session.page - 1 : 0;
    items[indexItem].isSubscribed = newState;
    return true;
  },
  isSet: (ctx) => {
    const indexItem = ctx.session && ctx.session.page ? ctx.session.page - 1 : 0;
    return items[indexItem].isSubscribed;
  },
});

but when i click on toggle button on page 2 ctx.session is undefined in toggle (set/isSet functions)

EdJoPaTo commented 3 years ago

the context and therefore the session are independent from the menu templates… The templates are just passing though the context. So whatever passes in the context seems to not work for you.

Is your menuMiddleware added to the bot (bot.use(…)) after the session middleware you are using? Is it working when using menuMiddleware.replyToContext(ctx) but not when pressing a button (= callback_query)?

petroShved commented 3 years ago

yes, so its working for events.url for example and text, but not working for toggle. https://github.com/petroShved/telegraf-calendar/ I Open my repo so you can check maybe

EdJoPaTo commented 3 years ago

when looking on your app.js:

first: your bot token is included there. You should not include secrets in repos. If you set the repo private again this might not be a problem but still make sure to change the token via botfather as anyone with that token can be the bot.

second: take a look on the things related to the bot.

Use telegraf's own in memory session or check out telegraf-session-local for a persistent one.

Another thing I noticed: you are using telegraf 3.38. You should update telegraf to v4.