grammyjs / conversations

Conversational interfaces for grammY.
https://grammy.dev/plugins/conversations
MIT License
48 stars 17 forks source link

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

Closed sergej-js closed 1 year ago

sergej-js commented 1 year ago

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

KnorpelSenf commented 1 year ago

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?

sergej-js commented 1 year ago

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.

KnorpelSenf commented 1 year ago

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

sergej-js commented 1 year ago

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.

KnorpelSenf commented 1 year ago

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?

sergej-js commented 1 year ago

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);
KnorpelSenf commented 1 year ago

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

KnorpelSenf commented 1 year ago

What do you even mean by scene?

sergej-js commented 1 year ago

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

sergej-js commented 1 year ago

What do you even mean by scene?

used to call it a scene

KnorpelSenf commented 1 year ago

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.

sergej-js commented 1 year ago

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

KnorpelSenf commented 1 year ago

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?

KnorpelSenf commented 1 year ago

Closing due to inactivity.