EdJoPaTo / grammy-inline-menu

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

How to specify message_thread_id in the replyToContext()? #201

Closed ProgrammingLife closed 1 year ago

ProgrammingLife commented 1 year ago

Describe your goal My code is:

bot.command( "menu", ctx => {
    console.log( `[/menu] called` );
    console.log( "ctx:", ctx );
    console.log( "ctx.message.message_thread_id:", ctx.message.message_thread_id ); // ctx.message.message_thread_id: 314
    return menuMiddleware.replyToContext( ctx );
} );

Expected I call /menu@mybot from a supergroup's topic (some thread). So I expect the answer to get exactly in the same topic (or a thread) from which I called it. I see there is some "message_thread_id: 314," in the ctx object: ctx.message.message_thread_id.

How to specify it in the replyToContext() function? Since now it replies in wrong topic. It now replies to main topic of the supergroup but not to topic from where I call it.

EdJoPaTo commented 1 year ago

The ctx.reply variants are used under the hood so I would expect to have the same behaviour as ctx.reply has. Can you verify that ctx.reply('something') also ends up in the wrong thread?

When that's the case then it probably requires some grammY reply function improvements. But it also might be something else I am overlooking currently.

ProgrammingLife commented 1 year ago

@EdJoPaTo, well, in my case ctx.reply() works fine and it replies exactly in the same thread_id because I specify it manually:

  await ctx.reply( "msg", {
    message_thread_id: 314,
  } );

If I don't specify thread_id then it will send to default (or a first) thread_id, I believe. So it will send in the wrong thread_id if I don't specify it.

EdJoPaTo commented 1 year ago

If I don't specify thread_id then it will send to default (or a first) thread_id, I believe. So it will send in the wrong thread_id if I don't specify it.

That was my question. As grammy-inline-menu does the same it ends up in the wrong thread. So its a bug in grammy: https://github.com/grammyjs/grammY/issues/352

As an ugly workaround you can use replyMenuToContext. Normally menuMiddleware.replyToContext simplifies the use of replyMenuToContext. Using it manually allows you to add custom options via the other argument.

https://github.com/EdJoPaTo/grammy-inline-menu/blob/7aad06eaa0b8549de0d2e932f482760837b6f033/source/send-menu.ts#L47-L52

Assuming you are using a single menuMiddleware with the default path (/) you can use this:

bot.command("menu", ctx => {
  // TODO: refactor to menuMiddleware.replyToContext(ctx) when grammy supports this
  await replyMenuToContext(menu, ctx, '/', {
    message_thread_id: ctx.message.message_thread_id,
  });
});
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.