telegram-inline-keyboard-builder 1.1.1 β 1.1.2
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/README.md +187 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
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
|
+
## β
API supported
|
|
165
|
+
- Telegraf
|
|
166
|
+
- Purgram
|
|
167
|
+
- Node-telegram-bot-api
|
|
168
|
+
- Telebot
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## π§― Common Errors & Debug
|
|
172
|
+
|
|
173
|
+
**ReferenceError**: Markup is not defined β you are still using Markup in the core; move button creation into the adapter.
|
|
174
|
+
|
|
175
|
+
**Missing adapter** β the core constructor must receive a valid adapter: new InlineKeyboardBuilder(adapter).
|
|
176
|
+
|
|
177
|
+
**Library not installed** (e.g., telegraf missing) β adapter should detect and throw a clear error: npm install telegraf.
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
βοΈ Contribution
|
|
183
|
+
|
|
184
|
+
Everyone are welcome. Open an issue to discuss a feature before implementing major changes.
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
---
|