Command Details

← Back to Commands
Help with category button

Help with category button

Enhanced Help Command for Telegram Bot

This enhanced help command provides users with a comprehensive guide to all available bot commands, allowing them to view commands by category or specific command details.

Purpose

The /help command is designed to assist users in exploring bot capabilities by organizing commands into categories and providing detailed descriptions and usage guides for each command.

Features

  • Category Selection: Users can select a category to view all commands within it, making it easier to find relevant commands.
  • Detailed Command Info: If a specific command is entered (e.g., /help commandName), users receive an in-depth description, usage guide, and category information for that command.
  • Dynamic Inline Keyboard: Displays an inline keyboard of command categories, allowing users to explore the bot’s capabilities interactively.

How It Works

  1. When a user types /help without arguments, the bot presents a keyboard with all command categories.
  2. Users can tap a category, and the bot will list commands available in that category.
  3. To view information on a specific command, users type /help <commandName>, and the bot provides detailed information on that command.

Example Usage

  • All Commands by Category: /help
  • Specific Command Info: /help ping (provides details on the ping command)

Error Handling

If a command name is invalid, the bot notifies the user and suggests using /help to view all categories.

const { MessageEntity } = require('node-telegram-bot-api');

module.exports = {
  name: 'help',
  adminOnly: false,
  ownerOnly: false,
  category: 'Utility',
  description: 'Show available commands or info about a specific command',
  guide: 'Use /help for all commands, or /help <command> for specific info',
  execute: async (bot, msg, args) => {
    const chatId = msg.chat.id;
    const commands = bot.commands;

    if (!commands) {
      return bot.sendMessage(chatId, 'Error: Commands not available. Please try again later.');
    }

    const createCommandList = (cmds) => {
      return Object.entries(cmds)
        .map(([name, cmd]) => `/${name} - ${cmd.description || 'No description'}`)
        .join('\n');
    };

    const createCategoryKeyboard = () => {
      const categories = [...new Set(Object.values(commands).map(cmd => cmd.category || 'Uncategorized'))];
      const keyboard = categories.map(category => [{
        text: category,
        callback_data: `help_category_${category}`
      }]);
      return { reply_markup: JSON.stringify({ inline_keyboard: keyboard }) };
    };

    const handleCallback = async (callbackQuery) => {
      const action = callbackQuery.data;
      const chatId = callbackQuery.message.chat.id;
      const messageId = callbackQuery.message.message_id;

      if (action.startsWith('help_category_')) {
        const category = action.split('_')[2];
        const categoryCommands = Object.fromEntries(
          Object.entries(commands).filter(([_, cmd]) => cmd.category === category)
        );
        const commandList = createCommandList(categoryCommands);
        await bot.editMessageText(`Commands in ${category}:\n\n${commandList}`, {
          chat_id: chatId,
          message_id: messageId,
          parse_mode: 'Markdown'
        });
      }

      await bot.answerCallbackQuery(callbackQuery.id);
    };

    bot.on('callback_query', handleCallback);

    if (args.length === 0) {
      const message = 'Choose a category to see commands:';
      await bot.sendMessage(chatId, message, createCategoryKeyboard());
    } else {
      const commandName = args[0].toLowerCase();
      const command = commands[commandName];

      if (command) {
        const helpMessage = `*/${commandName}*\n` +
                            `Description: ${command.description || 'No description'}\n` +
                            `Usage: ${command.guide || 'No usage guide'}\n` +
                            `Category: ${command.category || 'Uncategorized'}`;
        await bot.sendMessage(chatId, helpMessage, { parse_mode: 'Markdown' });
      } else {
        await bot.sendMessage(chatId, `Command /${commandName} not found. Use /help to see all categories.`);
      }
    }
  }
};