telegram-inline-keyboard-builder 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +146 -121
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,187 +1,212 @@
1
- ### Inline Keyboard Builder
1
+ ![Logo](https://i.ibb.co/BKVnp8dZ/20260202-141042.png) [![npm version](https://img.shields.io/npm/v/telegram-inline-keyboard-builder?style=flat&logo=npm&logoColor=white&color=cb3837)](https://www.npmjs.com/package/telegram-inline-keyboard-builder) [![npm downloads](https://img.shields.io/npm/dw/telegram-inline-keyboard-builder?style=flat&logo=npm&logoColor=white&color=2CA5E0)](https://www.npmjs.com/package/telegram-inline-keyboard-builder) [![license](https://img.shields.io/npm/l/telegram-inline-keyboard-builder?style=flat&color=555555)](LICENSE) ![Telegram](https://img.shields.io/badge/Telegram-Inline%20Keyboard-2CA5E0?style=flat&logo=telegram&logoColor=white)
2
2
 
3
- Small inline button builder for Telegram, designed to be library-agnostic (Telegraf, node-telegram-bot-api, Aiogram, Pyrogram...).
3
+ # Inline Keyboard Builder (v2) Universal inline keyboard builder for Telegram Bots.
4
+ Produces **pure Telegram Bot API compliant JSON**, usable with **any library** (Telegraf, node-telegram-bot-api, Pyrogram, Aiogram, Puregram, Telebot…).
4
5
 
5
- > Principle: a central logic core handles button creation and placement. Adapters transform the neutral structure into the format expected by the target API.
6
+ > Version 2 removes adapters and focuses on a single universal output:
7
+ > **valid `inline_keyboard` JSON as expected by Telegram API**.
6
8
 
9
+ ---
10
+ ## 🚀 Key Features - Fluent & chainable API - Library-agnostic (no adapters, no dependencies)
7
11
 
12
+ - Produces **pure Telegram inline keyboard JSON**
13
+ - Auto-wrap & row control - Works with **any Telegram framework**
14
+ - Zero abstraction leak
8
15
 
9
-
10
- ---
11
-
12
- ## 🚀 Key Features
13
-
14
- - Fluent, chainable API
15
-
16
- - Core independent of any Telegram library
17
-
18
- - Easy-to-write adapters (Telegraf, node-telegram-bot-api, Aiogram...)
19
-
20
- - Auto-wrap / control of buttons per row
21
-
22
-
23
-
24
- ---
16
+ ---
25
17
 
26
18
  ## Installation
27
19
 
28
- If the package is published on npm:
29
-
30
- ```bash
20
+ ```bash
31
21
  npm install telegram-inline-keyboard-builder
32
22
  ```
33
- Then in your project:
34
-
23
+ ## importation
35
24
  ```js
36
- import { InlineKeyboardTelegraf } from "telegram-inline-keyboard-builder";
25
+ import { InlineKeyboardBuilder } from "telegram-inline-keyboard-builder";
37
26
  ```
38
27
 
39
- ---
28
+ ## 🧠 Core Concept
40
29
 
41
- ## Quick Concepts
30
+ Telegram inline keyboards follow **one universal schema**.
42
31
 
43
- - **Core**: InlineKeyboardBuilder (placement logic, chainable API). Does not know any Telegram library.
32
+ This builder:
44
33
 
45
- - **Adapter**: object responsible for creating buttons (callback/url/pay/custom) and producing the final structure (reply_markup/Markup depending on the library).
34
+ * generates the keyboard **directly in Telegram format**
46
35
 
47
- - **Facade Builder**: ready-to-use classes that inject an adapter (e.g., InlineKeyboardTelegraf, InlineKeyboardNodeTelegram).
36
+ * lets you pass the result to **any Telegram library**
37
+ ```js
38
+ { reply_markup: { inline_keyboard: [...] } }
39
+ ```
40
+ - **No adapters**.
41
+ - **No wrappers**.
42
+ - **No framework coupling**.
48
43
 
44
+ ## 🔧 Public API
49
45
 
46
+ ### Constructor
50
47
 
51
- ---
48
+ ```js
49
+ new InlineKeyboardBuilder({ buttonsPerRow = 2, autoWrapMaxChars = 0 })
52
50
 
53
- ## 🔧 Public Interface (API)
51
+ //Chainable Methods
54
52
 
55
- ### Constructors
56
- ```js
57
- // Core (internal)
53
+ .addCallbackButton(text, callback_data, hide = false)
54
+ .addUrlButton(text, url, hide = false)
55
+ .addPayButton(text, options = {})
56
+ .addCustomButton(buttonObject)
57
+ .addButtons(config)
58
+ .setButtonsPerRow(n)
59
+ .setAutoWrapMaxChars(n)
60
+ .newRow()
58
61
 
59
- new InlineKeyboardBuilder(adapter, buttonsPerRow = 2, autoWrapMaxChars = 0)
62
+ // build
63
+ .build()
60
64
 
61
- // Facade (exposed to users, encapsulates core + adapter)
62
- new InlineKeyboardTelegraf({ buttonsPerRow = 2, autoWrapMaxChars = 0 })
63
- new InlineKeyboardNodeTelegram({ ... })
65
+ const keyboard = builder.build();
64
66
 
65
- Chainable Methods
67
+ // Always returns:
66
68
 
67
- .addCallbackButton(text, callback_data, hide = false)
68
- .addUrlButton(text, url, hide = false)
69
- .addPayButton(text, options = {})
70
- .addCustomButton(btnObj)
71
- .addButtons(config) // array or { type, buttons: [...] }
72
- .setButtonsPerRow(n)
73
- .setAutoWrapMaxChars(n)
74
- .newRow() // forces a new row
75
- .build() // returns the keyboard formatted by the adapter
69
+ { reply_markup: { inline_keyboard: [...] } }
76
70
  ```
77
- **Return of build()**: depends on the adapter.
71
+ Fully compliant with Telegram Bot API.
78
72
 
79
- **Telegraf adapte** → Markup.inlineKeyboard(rows) (Markup object)
73
+ ## Usage Example (Telegraf)
80
74
 
81
- **Node‑Telegram adapt** → { reply_markup: { inline_keyboard: rows } }
75
+ ```js
76
+ import { Telegraf } from "telegraf";
82
77
 
78
+ import { InlineKeyboardBuilder } from "telegram-inline-keyboard-builder";
83
79
 
80
+ const bot = new Telegraf(process.env.BOT_TOKEN);
84
81
 
85
- ---
82
+ bot.start(ctx => {
83
+ const keyboard = new InlineKeyboardBuilder({ buttonsPerRow: 2, autoWrapMaxChars: 24 })
84
+ .addCallbackButton("✅ OK", "OK_ACTION")
85
+ .addUrlButton("🌍 Website", "https://example.com")
86
+ .newRow()
87
+ .addCallbackButton("❌ Cancel", "CANCEL_ACTION")
88
+ .build();
89
+ ctx.reply("Welcome 👋\nChoose an action:", keyboard); });
86
90
 
87
- ## 🧩 Usage Example (Telegraf)
91
+ bot.launch();
92
+
93
+ ```
94
+ ## Usage Example (node-telegram-bot-api)
88
95
 
89
96
  ```js
90
- import { Telegraf } from "telegraf";
91
- import { InlineKeyboardTelegraf } from "telegram-inline-keyboard-builder";
97
+ import TelegramBot from "node-telegram-bot-api";
92
98
 
93
- const bot = new Telegraf(process.env.BOT_TOKEN);
99
+ import { InlineKeyboardBuilder } from "telegram-inline-keyboard-builder";
94
100
 
95
- bot.start(ctx => {
96
- const keyboard = new InlineKeyboardTelegraf({ buttonsPerRow: 2, autoWrapMaxChars: 24 })
97
- .addCallbackButton("✅ OK", "OK_ACTION")
98
- .addUrlButton("🌍 Site", "https://example.com")
99
- .newRow()
100
- .addCallbackButton("❌ Cancel", "CANCEL_ACTION")
101
- .build();
102
-
103
- ctx.reply("Welcome 👋\nChoose an action:", keyboard);
104
- });
101
+ const bot = new TelegramBot(TOKEN, { polling: true });
102
+ bot.onText(/\/start/, msg => {
103
+ const keyboard = new InlineKeyboardBuilder()
104
+ .addCallbackButton("OK", "OK")
105
+ .addUrlButton("Site", "https://example.com")
106
+ .build();
105
107
 
106
- bot.launch();
108
+ bot.sendMessage(msg.chat.id, "Hello", keyboard); });
107
109
  ```
108
110
 
109
- ## 🧾 Usage Example (node-telegram-bot-api)
111
+ ## 💳 Payment Buttons
110
112
 
111
- ```js
112
- import TelegramBot from "node-telegram-bot-api";
113
- import { InlineKeyboardNodeTelegram } from "telegram-inline-keyboard-builder";
114
-
115
- const bot = new TelegramBot(TOKEN, { polling: true });
113
+ ### ⚠️ Telegram limitation
116
114
 
117
- bot.onText(/\/start/, (msg) => {
118
- const kb = new InlineKeyboardNodeTelegram()
119
- .addCallbackButton("OK", "OK")
120
- .addUrlButton("Site", "https://example.com")
121
- .build();
115
+ > [!WARNING]
116
+ > Payment buttons must only be used with:
117
+ - sendInvoice
118
+ - replyWithInvoice
122
119
 
123
- // kb === { reply_markup: { inline_keyboard: [...] } }
124
- bot.sendMessage(msg.chat.id, "Hello", kb);
125
- });
120
+ They must be hidden in normal messages.
126
121
 
122
+ ```js
123
+ .addPayButton("Pay now");
127
124
  ```
128
- ---
129
125
 
130
- ## 🛠️ Writing an Adapter (expected interface)
126
+ Using a visible payment button outside invoices will cause Telegram API errors.
131
127
 
132
- A simple adapter must expose:
128
+ ## 🧯 Common Errors
129
+
130
+ **Telegram API error**
131
+
132
+ Make sure the keyboard object is passed directly:
133
133
 
134
134
  ```js
135
- // create a button
136
- adapter.createCallback(text, data, hide = false)
137
- adapter.createUrl(text, url, hide = false)
138
- adapter.createPay(text, hide = false)
135
+ const keyboard = new InlineKeyboardBuilder(1)
136
+ .addCallbackButton("Setting","show_setting")
137
+ .build()
138
+ // telegraf
139
+ ctx.reply("Text", keyboard);
139
140
 
140
- // build the final keyboard from rows (rows = array of array of buttons)
141
- adapter.buildKeyboard(rows)
141
+ // node telegram bot api
142
+ bot.sendMessage(chatId, "Text", keyboard);
142
143
 
143
- Minimal Example (node-telegram-bot-api)
144
+ // CORRECT
144
145
 
145
- export class NodeTelegramInlineAdapter {
146
- createCallback(text, data) {
147
- return { text, callback_data: data };
148
- }
146
+ // OR if you want to include it in the options
149
147
 
150
- createUrl(text, url) {
151
- return { text, url };
152
- }
148
+ const keyboard = new InlineKeyboardBuilder(1)
149
+ .addCallbackButton("Setting","show_setting")
150
+ .build()
153
151
 
154
- createPay(text) {
155
- return { text, pay: true };
156
- }
152
+ // telegraf
153
+ ctx.reply("Text", {
154
+ reply_markup: keyboard.reply_makup, // inline keyboard
155
+ parse_mode: "HTML",
156
+ // ...
157
+ });
157
158
 
158
- buildKeyboard(rows) {
159
- return { reply_markup: { inline_keyboard: rows } };
160
- }
161
- }
159
+ // node telegram bot api
160
+ bot.sendMessage(chatId, "Text", {
161
+ reply_markup: keyboard.reply_makup, // inline keyboard
162
+ parse_mode: "HTML",
163
+ // ...
164
+ );
162
165
  ```
163
- ---
164
- ## ✅ API supported
165
- - Telegraf
166
- - Purgram
167
- - Node-telegram-bot-api
168
- - Telebot
169
- ---
166
+ ## Migration to V2
167
+
168
+ - **V1**: The inline keyboard builder used **adapters** for each new API, resulting in code that was **unmaintainable** in case of **updates**.
169
+
170
+ - **V2**: Here we **simply construct an object valid for all types of APIs** without **adapting** it.
171
+
172
+ ## 💜 Support This Project (Crypto)
173
+
174
+ This project is maintained in my free time.
175
+ If it helped you, consider supporting it with a crypto donation ❤️
176
+ It helps me maintain and improve the project.
177
+
178
+ You can send donations to the following addresses:
170
179
 
171
- ## 🧯 Common Errors & Debug
180
+ | Crypto | Address |
181
+ |--------|---------|
182
+ | **USDT (TRC20)** | `0x607c1430601989d43c9CD2eeD9E516663e0BdD1F` |
183
+ | **USDC (Polygon/ETH)** | `0x607c1430601989d43c9CD2eeD9E516663e0BdD1F` |
184
+ | **Ethereum (ETH)** | `0x607c1430601989d43c9CD2eeD9E516663e0BdD1F` |
185
+ | **Bitcoin (BTC)** | `bc1qmysepz6eerz2mqyx5dd0yy87c3gk6hccwla5x2` |
186
+ | **Tron (TRX)** | `TE9RiTaDpx7DGZzCMw7qds51nzszKiyeR8` |
187
+ | **TON** | `UQA1NPW4GqgIVa9R6lebN_0v64Q-Sz_nHrmK9LCk-FfdjVOH` |
172
188
 
173
- **ReferenceError**: Markup is not defined you are still using Markup in the core; move button creation into the adapter.
189
+ ### 🔹 Optional QR Codes for quick mobile donation
174
190
 
175
- **Missing adapter** → the core constructor must receive a valid adapter: new InlineKeyboardBuilder(adapter).
191
+ **USDT (TRC20)**
192
+ ![USDT TRC20 QR](https://api.qrserver.com/v1/create-qr-code/?data=0x607c1430601989d43c9cd2eed9e516663e0bdd1f&size=150x150)
176
193
 
177
- **Library not installed** (e.g., telegraf missing) → adapter should detect and throw a clear error: npm install telegraf.
194
+ **USDC**
195
+ ![USDC QR](https://api.qrserver.com/v1/create-qr-code/?data=0x607c1430601989d43c9CD2eeD9E516663e0BdD1F&size=150x150)
178
196
 
197
+ **Ethereum (ETH)**
198
+ ![ETH QR](https://api.qrserver.com/v1/create-qr-code/?data=0x607c1430601989d43c9CD2eeD9E516663e0BdD1F&size=150x150)
179
199
 
180
- ---
200
+ **Bitcoin (BTC)**
201
+ ![BTC QR](https://api.qrserver.com/v1/create-qr-code/?data=bc1qmysepz6eerz2mqyx5dd0yy87c3gk6hccwla5x2&size=150x150)
181
202
 
182
- ✍️ Contribution
203
+ **Tron (TRX)**
204
+ ![TRX QR](https://api.qrserver.com/v1/create-qr-code/?data=TE9RiTaDpx7DGZzCMw7qds51nzszKiyeR8&size=150x150)
183
205
 
184
- Everyone are welcome. Open an issue to discuss a feature before implementing major changes.
206
+ **TON**
207
+ ![TON QR](https://api.qrserver.com/v1/create-qr-code/?data=UQA1NPW4GqgIVa9R6lebN_0v64Q-Sz_nHrmK9LCk-FfdjVOH&size=150x150)
185
208
 
209
+ ## ✍️ Contribution
186
210
 
187
- ---
211
+ Contributions are welcome ❤️
212
+ Please open an issue before proposing major changes.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-inline-keyboard-builder",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Universal inline keyboard builder for Telegram APIs",
5
5
  "main": "core/InlineKeyboardBuilder.js",
6
6
  "type": "module",