grammyjs/conversations

How can I make a separate exit handler for each conversation on the cancel button

sergej-js opened this issue · 14 comments

image
I have such code. And also there are several different conversations, and in each of them, on the cancel button, I have to exit to my handler.
When I use conversation.run for my target, when I click the button, I get an error, and conversation.run doesn't fire on it
image
image

Why are you using conversation.run in the first place? More importantly, why does https://grammy.dev/plugins/conversations.html#leaving-a-conversation not apply?

Why are you using conversation.run in the first place? More importantly, why does https://grammy.dev/plugins/conversations.html#leaving-a-conversation not apply?

For each dialog, I have my own method where you need to exit when you click on the cancel button. Creating a lot of cancel callbacks is a bad idea.

Did you open the link? It shows how to create a single cancel handler for the entire conversation.

I need to make a handler for each conversation when exiting it. If you do as shown in the documentation, you will have to write
if() {} else if() ...

Did you open the link? It shows how to create a single cancel handler for the entire conversation.

you will have to write
if() {} else if() ...

There is no such code in the example. I don't understand which code you're referring to. Are you sure we're talking about the same thing?

Let me copy the code here.

async function movie(conversation: MyConversation, ctx: MyContext) {
  // TODO: code the conversation
}

// Install the conversations plugin.
bot.use(conversations());

// Always exit any conversation upon /cancel
bot.command("cancel", async (ctx) => {
  await ctx.conversation.exit();
  await ctx.reply("Leaving.");
});

// Always exit the `movie` conversation 
// when the inline keyboard's `cancel` button is pressed.
bot.callbackQuery("cancel", async (ctx) => {
  await ctx.conversation.exit("movie");
  await ctx.answerCallbackQuery("Left conversation");
});

bot.use(createConversation(movie));
bot.command("movie", (ctx) => ctx.conversation.enter("movie"));

Where do you want to put if-else things now?

No, you did not understand me. I will try to show an example of what I want to do

async function mainMenu(ctx) {
    await ctx.reply("mainMenu");
}

async function settingMenu(ctx) {
    await ctx.reply("settingMenu");
}

async function movie(conversation: MyConversation, ctx: MyContext) {
    // TODO: code the conversation
}

async function movie1(conversation: MyConversation, ctx: MyContext) {
    // TODO: code the conversation
}

// Install the conversations plugin.
bot.use(conversations());

bot.callbackQuery("cancel", async (ctx) => {
await ctx.conversation.exit("movie");
    await mainMenu(ctx);
});

bot.callbackQuery("cancel1", async (ctx) => {
await ctx.conversation.exit("movie1");
    await settingMenu(ctx);
});

bot.use(createConversation(movie));
bot.use(createConversation(movie1));
bot.command("movie", (ctx) => ctx.conversation.enter("movie"));
bot.command("movie1", (ctx) => ctx.conversation.enter("movie1"));

I hope I gave the correct example. I will have to create my own cancel callback for each scene. How can I solve this problem of creating either cancel1-cancel2-cancel2
or cancel_ and in the handler do

if (ctx.match[1] === "mainMenu") return mainMenu(ctx); 
else if (ctx.match[1] === "settingsMenu") return settingMenu(ctx);

With this example I agree that I now really have no idea at all what you want.

What do you even mean by scene?

With this example I agree that I now really have no idea at all what you want.

What do you even mean by scene?

conversation

What do you even mean by scene?

used to call it a scene

Scenes are completely different from conversations. If you think they are similar in how they work, this is probably the main point here why it doesn't work the way you think it does.

Scenes are completely different from conversations. If you think they are similar in how they work, this is probably the main point here why it doesn't work the way you think it does.

I understand that scenes and conversations are different.
My problem was that in each conversation on the cancel button I have to go to a different handler. Let's say from conversation movie I have to go to mainMenu, and from movie1 I have to go to settingsMenu

Where is this mapping from conversations to menus defined? Do you want to do something with https://grammy.dev/plugins/conversations.html#inspecting-active-conversations?

Closing due to inactivity.