grammyjs / menu

Interactive menus for grammY.
https://grammy.dev/plugins/menu
MIT License
31 stars 11 forks source link

feat: Static menus #2

Open KnorpelSenf opened 3 years ago

KnorpelSenf commented 3 years ago

Menus cannot be sent via bot.api because they require an installed API transformer. This transformer is only installed during bot.use(menu).

One way would be to directly provide the API transformer to be installed on bot.api, but this does not follow the intuitive pattern of the rest of the plugin.

Instead, it would be cool if we can have a statically rendered version of the menu (a menu range) that can be passed to the constructor of the menu. Whenever the menu is sent (the internal Proxy is accessed), this static menu is rendered and sent. It obviously may not depend on a context object, so we should throw an error if someone tries to use dynamic strings/dynamic ranges.

const menu = new Menu('id', {
  static: new Menu.Range().text('go', ctx => ctx.reply('whee'))
})
sartoshi-foot-dao commented 2 years ago

Depositing the phrases I queried the issues of this repo so that they may be indexed for others:

this.id bot.use bot.api

Context

Question

Good evening all, a question about Menus and ctx:

Suppose I wanted to use grammY bot in an ancillary webhook that does not receive updates from Telegram but payloads from another system e.g. a scheduled message.

This message will contain a Menu, which depends on ctx to work; directly using Menu with bot.api.sendMessage(1234567, "good evening", {reply_markup: Menu} throws error Cannot send menu '${this.id}'! Did you forget to use bot.use() for it?. Actually, bot.use(Menu) is called before any other middleware.

I am assuming that if I had access to a ctx, presumably via listener, Menu would work. If so, what listener could be triggered by a non-telegram-update payload?

Has anyone any experience with sending menus in such a manner?

Edit: Menu is an instance of the Menu class (from plugin)

Answer

@KnorpelSenf: You are correct that menus are generated from context objects. There's an idea to create static menus (https://github.com/grammyjs/menu/issues/2) but so far there's been very little demand for this. Until then, there's a less convenient solution:

What you should do instead is to use bot.api to send a message with reply markup, using the built-in keyboard plugin of grammY. As soon as a button is pressed, you can switch over to the menu plugin and let it handle the rest.