telegram-inline-keyboard-builder 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +182 -0
- package/adapters/node-telegram.adapter.js +21 -0
- package/adapters/telegraf.adapter.js +19 -0
- package/builders/InlineKeyboardNodeTelegram.js +13 -0
- package/builders/InlineKeyboardTelegraf.js +12 -0
- package/core/InlineKeyboardBuilder.js +89 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Neon Craft X
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
### Inline Keyboard Builder
|
|
2
|
+
|
|
3
|
+
Small inline button builder for Telegram, designed to be library-agnostic (Telegraf, node-telegram-bot-api, Aiogram, Pyrogram...).
|
|
4
|
+
|
|
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
|
+
|
|
7
|
+
|
|
8
|
+
|
|
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
|
+
---
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
If the package is published on npm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install telegram-inline-keyboard-builder
|
|
32
|
+
```
|
|
33
|
+
Then in your project:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { InlineKeyboardTelegraf } from "telegram-inline-keyboard-builder";
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Quick Concepts
|
|
42
|
+
|
|
43
|
+
- **Core**: InlineKeyboardBuilder (placement logic, chainable API). Does not know any Telegram library.
|
|
44
|
+
|
|
45
|
+
- **Adapter**: object responsible for creating buttons (callback/url/pay/custom) and producing the final structure (reply_markup/Markup depending on the library).
|
|
46
|
+
|
|
47
|
+
- **Facade Builder**: ready-to-use classes that inject an adapter (e.g., InlineKeyboardTelegraf, InlineKeyboardNodeTelegram).
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 🔧 Public Interface (API)
|
|
54
|
+
|
|
55
|
+
### Constructors
|
|
56
|
+
```js
|
|
57
|
+
// Core (internal)
|
|
58
|
+
|
|
59
|
+
new InlineKeyboardBuilder(adapter, buttonsPerRow = 2, autoWrapMaxChars = 0)
|
|
60
|
+
|
|
61
|
+
// Facade (exposed to users, encapsulates core + adapter)
|
|
62
|
+
new InlineKeyboardTelegraf({ buttonsPerRow = 2, autoWrapMaxChars = 0 })
|
|
63
|
+
new InlineKeyboardNodeTelegram({ ... })
|
|
64
|
+
|
|
65
|
+
Chainable Methods
|
|
66
|
+
|
|
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
|
|
76
|
+
```
|
|
77
|
+
**Return of build()**: depends on the adapter.
|
|
78
|
+
|
|
79
|
+
**Telegraf adapte** → Markup.inlineKeyboard(rows) (Markup object)
|
|
80
|
+
|
|
81
|
+
**Node‑Telegram adapt** → { reply_markup: { inline_keyboard: rows } }
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## 🧩 Usage Example (Telegraf)
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import { Telegraf } from "telegraf";
|
|
91
|
+
import { InlineKeyboardTelegraf } from "telegram-inline-keyboard-builder";
|
|
92
|
+
|
|
93
|
+
const bot = new Telegraf(process.env.BOT_TOKEN);
|
|
94
|
+
|
|
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
|
+
});
|
|
105
|
+
|
|
106
|
+
bot.launch();
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 🧾 Usage Example (node-telegram-bot-api)
|
|
110
|
+
|
|
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 });
|
|
116
|
+
|
|
117
|
+
bot.onText(/\/start/, (msg) => {
|
|
118
|
+
const kb = new InlineKeyboardNodeTelegram()
|
|
119
|
+
.addCallbackButton("OK", "OK")
|
|
120
|
+
.addUrlButton("Site", "https://example.com")
|
|
121
|
+
.build();
|
|
122
|
+
|
|
123
|
+
// kb === { reply_markup: { inline_keyboard: [...] } }
|
|
124
|
+
bot.sendMessage(msg.chat.id, "Hello", kb);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 🛠️ Writing an Adapter (expected interface)
|
|
131
|
+
|
|
132
|
+
A simple adapter must expose:
|
|
133
|
+
|
|
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)
|
|
139
|
+
|
|
140
|
+
// build the final keyboard from rows (rows = array of array of buttons)
|
|
141
|
+
adapter.buildKeyboard(rows)
|
|
142
|
+
|
|
143
|
+
Minimal Example (node-telegram-bot-api)
|
|
144
|
+
|
|
145
|
+
export class NodeTelegramInlineAdapter {
|
|
146
|
+
createCallback(text, data) {
|
|
147
|
+
return { text, callback_data: data };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
createUrl(text, url) {
|
|
151
|
+
return { text, url };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
createPay(text) {
|
|
155
|
+
return { text, pay: true };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
buildKeyboard(rows) {
|
|
159
|
+
return { reply_markup: { inline_keyboard: rows } };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## 🧯 Common Errors & Debug
|
|
167
|
+
|
|
168
|
+
**ReferenceError**: Markup is not defined → you are still using Markup in the core; move button creation into the adapter.
|
|
169
|
+
|
|
170
|
+
**Missing adapter** → the core constructor must receive a valid adapter: new InlineKeyboardBuilder(adapter).
|
|
171
|
+
|
|
172
|
+
**Library not installed** (e.g., telegraf missing) → adapter should detect and throw a clear error: npm install telegraf.
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
✍️ Contribution
|
|
178
|
+
|
|
179
|
+
Everyone are welcome. Open an issue to discuss a feature before implementing major changes.
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
---
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class NodeTelegramInlineAdapter {
|
|
2
|
+
createCallback(text, data) {
|
|
3
|
+
return { text, callback_data: data };
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
createUrl(text, url) {
|
|
7
|
+
return { text, url };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
createPay(text) {
|
|
11
|
+
return { text, pay: true };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
buildKeyboard(rows) {
|
|
15
|
+
return {
|
|
16
|
+
reply_markup: {
|
|
17
|
+
inline_keyboard: rows
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Markup } from "telegraf";
|
|
2
|
+
|
|
3
|
+
export class TelegrafInlineAdapter {
|
|
4
|
+
createCallback(text, data, hide = false) {
|
|
5
|
+
return Markup.button.callback(text, data, hide);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
createUrl(text, url, hide = false) {
|
|
9
|
+
return Markup.button.url(text, url, hide);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
createPay(text, hide = false) {
|
|
13
|
+
return Markup.button.pay(text, hide);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
buildKeyboard(rows) {
|
|
17
|
+
return Markup.inlineKeyboard(rows);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
import { InlineKeyboardBuilder } from "../core/InlineKeyboardBuilder.js";
|
|
3
|
+
import { NodeTelegramInlineAdapter } from "../adapters/node-telegram.adapter.js";
|
|
4
|
+
|
|
5
|
+
export class InlineKeyboardNodeTelegram extends InlineKeyboardBuilder {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
super(
|
|
8
|
+
new NodeTelegramInlineAdapter(),
|
|
9
|
+
options.buttonsPerRow,
|
|
10
|
+
options.autoWrapMaxChars
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { InlineKeyboardBuilder } from "../core/InlineKeyboardBuilder.js";
|
|
2
|
+
import { TelegrafInlineAdapter } from "../adapters/telegraf.adapter.js";
|
|
3
|
+
|
|
4
|
+
export class InlineKeyboardTelegraf extends InlineKeyboardBuilder {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
super(
|
|
7
|
+
new TelegrafInlineAdapter(),
|
|
8
|
+
options.buttonsPerRow,
|
|
9
|
+
options.autoWrapMaxChars
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export class InlineKeyboardBuilder {
|
|
2
|
+
constructor(adapter, buttonsPerRow = 2, autoWrapMaxChars = 0) {
|
|
3
|
+
if (!adapter) {
|
|
4
|
+
throw new Error("InlineKeyboardBuilder requires an adapter");
|
|
5
|
+
}
|
|
6
|
+
this.adapter = adapter;
|
|
7
|
+
this.buttonsPerRow = buttonsPerRow;
|
|
8
|
+
this.autoWrapMaxChars = autoWrapMaxChars;
|
|
9
|
+
this._buttons = [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_pushButton(btn) {
|
|
13
|
+
this._buttons.push(btn);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
addCallbackButton(text, callback_data, hide = false) {
|
|
18
|
+
return this._pushButton(
|
|
19
|
+
this.adapter.createCallback(text, callback_data, hide)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
addUrlButton(text, url, hide = false) {
|
|
24
|
+
return this._pushButton(
|
|
25
|
+
this.adapter.createUrl(text, url, hide)
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
addPayButton(text, options = {}) {
|
|
30
|
+
const hide = options.hide ?? false;
|
|
31
|
+
return this._pushButton(
|
|
32
|
+
this.adapter.createPay(text, hide)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
addCustomButton(btnObj) {
|
|
37
|
+
return this._pushButton(btnObj);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
newRow() {
|
|
41
|
+
this._buttons.push({ __newRow: true });
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
_layoutButtons() {
|
|
45
|
+
const rows = [];
|
|
46
|
+
let row = [];
|
|
47
|
+
let rowChars = 0;
|
|
48
|
+
|
|
49
|
+
const pushRow = () => {
|
|
50
|
+
if (row.length > 0) {
|
|
51
|
+
rows.push(row);
|
|
52
|
+
row = [];
|
|
53
|
+
rowChars = 0;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
for (const b of this._buttons) {
|
|
58
|
+
if (b && b.__newRow) {
|
|
59
|
+
pushRow();
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const text = b?.text || "";
|
|
64
|
+
const len = String(text).length || 0;
|
|
65
|
+
|
|
66
|
+
if (
|
|
67
|
+
this.autoWrapMaxChars > 0 &&
|
|
68
|
+
row.length > 0 &&
|
|
69
|
+
rowChars + len > this.autoWrapMaxChars
|
|
70
|
+
) {
|
|
71
|
+
pushRow();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (row.length >= this.buttonsPerRow) {
|
|
75
|
+
pushRow();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
row.push(b);
|
|
79
|
+
rowChars += len;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
pushRow();
|
|
83
|
+
return rows;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
build() {
|
|
87
|
+
return this.adapter.buildKeyboard(this._layoutButtons());
|
|
88
|
+
}
|
|
89
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "telegram-inline-keyboard-builder",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Universal inline keyboard builder for Telegram APIs",
|
|
5
|
+
"main": "builders/InlineKeyboardTelegraf.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"telegram",
|
|
9
|
+
"inline-keyboard",
|
|
10
|
+
"telegraf",
|
|
11
|
+
"aiogram",
|
|
12
|
+
"builder",
|
|
13
|
+
"python-telegram-bot",
|
|
14
|
+
"node-telegram-bot-api"
|
|
15
|
+
],
|
|
16
|
+
"author": "neoncraftx",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/neoncraftx/telegram-inline-keyboard-builder.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/neoncraftx/telegram-inline-keyboard-builder.git/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/neoncraftx/telegram-inline-keyboard-builder.git#readme",
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"telegraf": "^4.0.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"telegram-inline-keyboard-builder": "file:telegram-inline-keyboard-builder-1.0.0.tgz"
|
|
31
|
+
}
|
|
32
|
+
}
|