/    Sign up×
Bounties /Pin to ProfileBookmark

I need help with a telegram bot made with the grammY keyboard.

 I am struggling to make the grammY javascript based Telegram bot work. I want a bot that has 1) start menu that has a welcome message and ‘main menu’ button. 2) when the main menu button is opened 4 buttons appear each with custom text and 4 sub menus for each. the sub-menus need to be able to be closed with a ‘close button’ but i am struggling with the middleware to make it work at the moment

to post a answer
DebuggingEngineerJavaScriptPHP

2 Replies

Davinci•003 auto-replyTo create a Telegram bot with the functionality you described using grammY, you can use the Composer class to create a middleware pipeline for your bot.
const { TelegramBot, Composer } = require('grammy');
const token = '';
const bot = new TelegramBot(token);

// Middleware to handle /start command
const startHandler = new Composer();
startHandler.command('start', async ctx => {
await ctx.reply('Welcome to my bot!');
await ctx.reply('Please select an option from the main menu below:', {
reply_markup: {
keyboard: [['Option 1', 'Option 2', 'Option 3', 'Option 4']],
resize_keyboard: true,
},
});
});

// Middleware to handle main menu button clicks
const menuHandler = new Composer();
menuHandler.on('text', async ctx => {
const option = ctx.message.text;
switch (option) {
case 'Option 1':
// Handle Option 1 click
break;
case 'Option 2':
// Handle Option 2 click
break;
case 'Option 3':
// Handle Option 3 click
break;
case 'Option 4':
// Handle Option 4 click
break;
default:
await ctx.reply("I'm sorry, I didn't understand that option.");
}
});

// Middleware to handle sub-menu clicks
const subMenuHandler = new Composer();
subMenuHandler.on('text', async ctx => {
const option = ctx.message.text;
switch (option) {
case 'Sub-Menu 1':
// Handle Sub-Menu 1 click
break;
case 'Sub-Menu 2':
// Handle Sub-Menu 2 click
break;
case 'Sub-Menu 3':
// Handle Sub-Menu 3 click
break;
case 'Sub-Menu 4':
// Handle Sub-Menu 4 click
break;
case 'Close':
// Close the sub-menu
await ctx.reply('Sub-Menu closed.');
bot.middleware.pop(); // Remove the sub-menu middleware from the pipeline
break;
default:
await ctx.reply("I'm sorry, I didn't understand that option.");
}
});

// Middleware to handle opening sub-menus
const openSubMenuHandler = new Composer();
openSubMenuHandler.on('text', async ctx => {
const option = ctx.message.text;
switch (option) {
case 'Option 1':
// Open sub-menu for Option 1
bot.middleware.unshift(subMenuHandler); // Add sub-menu middleware to the start of the pipeline
await ctx.reply('Please select an option from the sub-menu below:', {
reply_markup: {
keyboard: [['Sub-Menu 1', 'Sub-Menu 2']],
resize_keyboard: true,
},
});
break;
case 'Option 2':
// Open sub-menu for Option 2
bot.middleware.unshift(subMenuHandler); // Add sub-menu middleware to the start of the pipeline
await ctx.reply('Please select an option from the sub-menu below:', {
reply_markup: {
keyboard: [['Sub-Menu 3', 'Sub-Menu 4']],
resize_keyboard: true,
},
});
break;
default:
await ctx.reply("I'm sorry, I didn't understand that option.");
}
});

// Middleware pipeline for the bot
bot.use(startHandler);
bot.use(openSubMenuHandler);
bot.use(menuHandler);

// Start the bot
bot.start();

Was this helpful?

Copy linkTweet thisAlerts:
@themolitorMar 03.2023 — Hey there 👋 Just a note that I updated the Davinci•003 reply 👍
@tgone318authorunfortunately it doesn't work: Uncaught TypeError TypeError: TelegramBot is not a constructorMar 03.2023
@themolitorNoted!Mar 03.2023
×

Success!

Help @tgone318 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...