EdJoPaTo / grammy-inline-menu

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

"set" is never called for the "select" elements #200

Closed ProgrammingLife closed 1 year ago

ProgrammingLife commented 1 year ago

Describe your goal I try to make it work with the following code:

import { Bot, Context, session, SessionFlavor, InputFile } from "grammy";
import { run, sequentialize } from "@grammyjs/runner";

interface SessionData {};

type MyContext = Context & SessionFlavor<SessionData>
const bot = new Bot< MyContext > ( "...key....." );

const menu = new MenuTemplate < MyContext > ( ctx => {
    if ( ctx && ctx.from ) { return `Hey ${ ctx.from.first_name }!`; }
    return "";
} );
const menuMiddleware = new MenuMiddleware( "/", menu );
bot.use( menuMiddleware );

let selectedKey = "";
menu.select('select', ['A', 'B', 'C'], {
    showFalseEmoji: true,
    async set(ctx, key) {
        console.log( `[set] called` );
        selectedKey = key;
        await ctx.answerCallbackQuery({text: `you selected ${key}`});
        return true;
    },
    isSet: (_, key) => {
        console.log( `[isSet] called` );
        return key === selectedKey;
    },
});
let mediaOption = 'photo1';
menu.select('type', ['animation', 'document', 'photo1', 'photo2', 'video', 'location', 'venue', 'just text'], {
    columns: 2,
    isSet: (_, key) => {
        console.log( `[mediaOption::isSet] called` );
        return mediaOption === key;
    },
    set(_, key) {
        console.log( `[mediaOption::set] called` );
        mediaOption = key;
        return true;
    },
});
let mainMenuToggle = false
menu.toggle('toggle me', 'toggle me', {
    set(_, newState) {
        mainMenuToggle = newState
        console.log( `[toggleMe] called` );
        return true
    },
    isSet: () => mainMenuToggle,
});
bot.command( "menu", ctx => menuMiddleware.replyToContext( ctx ) );

run( bot );

Expected I expect some messages in the console which contains "set" text but looks the set function is never called. isSet called everytime when I click any button in my bot. How to make "set" functions work?

My env: $ npm list | grep grammy ├── @grammyjs/runner@1.0.4 ├── grammy-inline-menu@8.0.0 ├── grammy@1.13.0

ProgrammingLife commented 1 year ago

Solved by replacing these lines to the end of the code:

const menuMiddleware = new MenuMiddleware( "/", menu );
bot.use( menuMiddleware );