Conversation
KnorpelSenf
left a comment
There was a problem hiding this comment.
This is generally a good approach, but I still have some concerns regarding flexibility.
src/menu.ts
Outdated
|
|
||
| /** | ||
| * Use this method to send menus that have content. | ||
| * | ||
| * @param menu_id The ID of menu to send | ||
| */ | ||
| replyWithMenu: (menu_id: string) => Promise<void> |
There was a problem hiding this comment.
How can a menu be sent to a different chat?
There was a problem hiding this comment.
Like that:
bot.command("start", async (ctx) => {
await ctx.api.sendMessage(
ctx.chatId,
"This text will be edited by menu plugin",
{ reply_markup: home_menu }
);
});This approach looks kinda janky, so I'd introduce a sendMenu method in ctx.api:
Object.assign(ctx.api, {sendMenu: (chat_id: string | number, menu_id: 'string') => ctx.sendMessage(chat_id, 'a', {reply_markup: this.at(menu_id)})})There was a problem hiding this comment.
That's one option.
How about introducing ctx.menu.replyWith("main-menu") instead of polluting ctx directly? We could then also introduce ctx.menu.send or something which would have a chat_id param.
There was a problem hiding this comment.
So I put these methods in MenuControlPanel, but I get an error, because this control panel is available only in menus, not outside of them. So this just produces an error:
bot.command("start", async (ctx) => {
await ctx.menu.replyWith("main");
});And this works, because of the reasons stated above:
const menu = new Menu("main", {
content: { text: (ctx) => `Your ID is ${ctx.chatId}` },
}).text("Hello world", (ctx) => ctx.menu.replyWith("sub"));We could move the control panel into the middleware, but I fear that this will break something. Is there another way?
There was a problem hiding this comment.
Ahhh that's a good point. I guess my suggestion doesn't work well.
We probably want to support sending a menu from arbitrary handlers, not just button handlers. This means we must introduce a new context flavour that people can install for their middleware.
We can then enable ctx.replyWithMenu anywhere in the middleware tree.
KnorpelSenf
left a comment
There was a problem hiding this comment.
#57 (comment) still needs to be done.
- Create a new context flavour that can be used in middleware
- Move replyWithMenu to it
Created a new middleware that is applied globally and is not tied to any menu that makes it possible to send menus. Also introduced formatting options for menus' content and imported api in deps.
|
Yeah i accidentally clicked on that one😅 Anyway, here is my commit. I created a new middleware that contains the functions discussed above and I also introduced some options for menu content. |
This is a bare-bones implementation of content support for menus. Currently it supports only text.
One major change is the addition of
replyWithMenufunction which makes it possible to send a menu without specifying text of message (because the text is already specified inside the menu). Example usage:Example of specifying menu's text:
Dynamic labels: