DEV Community

Nรฉon Craftx
Nรฉon Craftx

Posted on

๐Ÿš€ Simplify Telegram Inline Keyboards with telegram-inline-keyboard-builder

Building Telegram bots is fun, but creating inline keyboards can be repetitive, verbose, and messy.

telegram-inline-keyboard-builder is a lightweight, library-agnostic package that allows you to build inline buttons easily for Telegraf, node-telegram-bot-api, Aiogram, or Pyrogram.


๐Ÿš€ Why this package?

Creating inline buttons usually requires writing repetitive, nested code.

The "Old" Way (Verbose):

ctx.reply('Choose an option', Markup.inlineKeyboard([
  Markup.button.callback('Yes', 'YES'),
  Markup.button.callback('No', 'NO')
]));

Enter fullscreen mode Exit fullscreen mode

The "Builder" Way (Fluent & Clean):

import { InlineKeyboardTelegraf } from 'telegram-inline-keyboard-builder';

const keyboard = new InlineKeyboardTelegraf({ buttonsPerRow: 2 })
  .addCallbackButton('โœ… OK', 'OK_ACTION')
  .addUrlButton('๐ŸŒ Visit site', '[https://example.com](https://example.com)')
  .newRow()
  .addCallbackButton('โŒ Cancel', 'CANCEL_ACTION')
  .build();

ctx.reply('Welcome ๐Ÿ‘‹\nChoose an action:', keyboard);

Enter fullscreen mode Exit fullscreen mode

โœ… Cleaner Code โœ… Chainable API โœ… Smart Row Management & Auto-wrap


๐Ÿ› ๏ธ Key Features

  • Fluent API: Chainable calls like .addCallbackButton().addUrlButton().newRow() for maximum readability.
  • Library Agnostic: The core logic is independent; adapters handle the specific integration for different Telegram libraries.
  • Ready-to-use Adapters: Out-of-the-box support for Telegraf and Node Telegram Bot API.
  • Flexible Configuration: Control the number of buttons per row or let the builder handle auto-wrapping based on character count.

๐Ÿ“ฆ Installation

Install the package via npm:

npm install telegram-inline-keyboard-builder

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก How it works

The package uses a core builder (InlineKeyboardBuilder) that handles the layout logic, row management, and button types.

Adapters then convert this core output into library-specific formats:

  • Markup.inlineKeyboard structure for Telegraf.
  • { reply_markup: { inline_keyboard: [...] } } for node-telegram-bot-api.

Quick Example (Telegraf)

import { InlineKeyboardTelegraf } from 'telegram-inline-keyboard-builder';

// Create a 2-column keyboard
const myKeyboard = new InlineKeyboardTelegraf({ buttonsPerRow: 2 })
  .addCallbackButton('Option 1', 'ID_1')
  .addCallbackButton('Option 2', 'ID_2')
  .build();

bot.launch();

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“š Examples & Documentation

Check the GitHub Repository for:

  • Detailed usage examples.
  • Node.js and Python adapters.
  • Custom buttons, Pay buttons, and Login URLs.

telegram-inline-keyboard-builder is perfect if you want less boilerplate and more focus on your bot logic.

Happy coding! ๐Ÿš€


---

### Would you like me to create a `CONTRIBUTING.md` file as well to encourage others to build adapters for more libraries?

Enter fullscreen mode Exit fullscreen mode

Top comments (0)