grammyjs / conversations

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

Conversations are not accessible from Menu callback #119

Closed cakeinpanic closed 1 month ago

cakeinpanic commented 1 month ago

Hi, I'm trying to build a following logic:

  1. User presses /settings
  2. We show him menu with possible settings
  3. He clicks on one of them
  4. Conversation starts to check that user would enter a valid value (kinda like in botfather)

But I've faced that in conversation object is not present in context, so I can not start it :(

import { Menu } from "@grammyjs/menu";
import { CommandContext, Context } from "grammy";
import { MyContext, MyConversation } from "../entities/my-context";

export const chooseModel = async (ctx:CommandContext<Context>) => {
    await ctx.reply("Choose model settings to adjust:", { reply_markup: modelSettingsMenu });
}

export async function chooseTokens(conversation: MyConversation, ctx: MyContext) {
   //logic here
}

const chooseTemperature = async (ctx) => {
  //ctx.conversation is undefined here and causes an error
   await ctx.conversation.enter("chooseTokens");
}

export const modelSettingsMenu = new Menu("model-settings")
  .text("Temperature", chooseTemperature).row()

Is it like this by design? Can this be bypassed?

KnorpelSenf commented 1 month ago

It just means that the order of your middleware is incorrect. You need to install the conversation before the menu if you want to enter it from a menu button handler.

cakeinpanic commented 1 month ago

@KnorpelSenf it worked as a charm, thank you!

KnorpelSenf commented 1 month ago

Nice!