@xbibzlibrary/telebibz 0.1.2 → 0.1.3
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/CHANGELOG.md +16 -1
- package/README.id.md +158 -0
- package/README.md +53 -33
- package/README.zh-CN.md +158 -0
- package/RELEASE_AUTOMATION.md +66 -0
- package/RELEASE_POLICY.md +2 -2
- package/assets/readme-preview.html +75 -0
- package/assets/telebibz-readme-preview.png +0 -0
- package/dist/src/api/index.d.ts +1 -0
- package/dist/src/api/index.d.ts.map +1 -1
- package/dist/src/api/index.js +1 -0
- package/dist/src/api/index.js.map +1 -1
- package/dist/src/api/telegram-types/LICENSE +21 -0
- package/dist/src/api/telegram-types/api.d.ts +22 -0
- package/dist/src/api/telegram-types/checklist.d.ts +72 -0
- package/dist/src/api/telegram-types/inline.d.ts +692 -0
- package/dist/src/api/telegram-types/langs.d.ts +193 -0
- package/dist/src/api/telegram-types/manage.d.ts +1144 -0
- package/dist/src/api/telegram-types/markup.d.ts +268 -0
- package/dist/src/api/telegram-types/message.d.ts +1537 -0
- package/dist/src/api/telegram-types/methods.d.ts +2870 -0
- package/dist/src/api/telegram-types/mod.d.ts +14 -0
- package/dist/src/api/telegram-types/passport.d.ts +163 -0
- package/dist/src/api/telegram-types/payment.d.ts +570 -0
- package/dist/src/api/telegram-types/rich.d.ts +1010 -0
- package/dist/src/api/telegram-types/settings.d.ts +120 -0
- package/dist/src/api/telegram-types/story.d.ts +89 -0
- package/dist/src/api/telegram-types/update.d.ts +84 -0
- package/dist/src/api/telegram.d.ts +7 -0
- package/dist/src/api/telegram.d.ts.map +1 -0
- package/dist/src/api/telegram.js +2 -0
- package/dist/src/api/telegram.js.map +1 -0
- package/dist/src/approval/approval.d.ts +8 -0
- package/dist/src/approval/approval.d.ts.map +1 -1
- package/dist/src/approval/approval.js +9 -0
- package/dist/src/approval/approval.js.map +1 -1
- package/dist/src/cache/cache.d.ts +6 -5
- package/dist/src/cache/cache.d.ts.map +1 -1
- package/dist/src/cache/cache.js +7 -3
- package/dist/src/cache/cache.js.map +1 -1
- package/dist/src/context/context.d.ts.map +1 -1
- package/dist/src/context/context.js +26 -3
- package/dist/src/context/context.js.map +1 -1
- package/dist/src/core/bot.d.ts +6 -4
- package/dist/src/core/bot.d.ts.map +1 -1
- package/dist/src/core/bot.js +48 -7
- package/dist/src/core/bot.js.map +1 -1
- package/dist/src/core/events.d.ts +4 -0
- package/dist/src/core/events.d.ts.map +1 -1
- package/dist/src/core/events.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/queue/queue.d.ts +25 -0
- package/dist/src/queue/queue.d.ts.map +1 -1
- package/dist/src/queue/queue.js +175 -51
- package/dist/src/queue/queue.js.map +1 -1
- package/dist/src/router/router.d.ts +8 -1
- package/dist/src/router/router.d.ts.map +1 -1
- package/dist/src/router/router.js +75 -17
- package/dist/src/router/router.js.map +1 -1
- package/dist/src/state/conversation.d.ts +6 -0
- package/dist/src/state/conversation.d.ts.map +1 -1
- package/dist/src/state/conversation.js +79 -11
- package/dist/src/state/conversation.js.map +1 -1
- package/dist/src/state/menu.d.ts +53 -5
- package/dist/src/state/menu.d.ts.map +1 -1
- package/dist/src/state/menu.js +116 -17
- package/dist/src/state/menu.js.map +1 -1
- package/dist/src/storage/storage.d.ts +115 -12
- package/dist/src/storage/storage.d.ts.map +1 -1
- package/dist/src/storage/storage.js +130 -4
- package/dist/src/storage/storage.js.map +1 -1
- package/dist/src/telegram-features.d.ts +33 -0
- package/dist/src/telegram-features.d.ts.map +1 -0
- package/dist/src/telegram-features.js +69 -0
- package/dist/src/telegram-features.js.map +1 -0
- package/dist/src/testing.d.ts +1 -0
- package/dist/src/testing.d.ts.map +1 -1
- package/dist/src/testing.js +16 -0
- package/dist/src/testing.js.map +1 -1
- package/dist-cjs/src/api/index.js +1 -0
- package/dist-cjs/src/api/telegram-types/LICENSE +21 -0
- package/dist-cjs/src/api/telegram-types/api.d.ts +22 -0
- package/dist-cjs/src/api/telegram-types/checklist.d.ts +72 -0
- package/dist-cjs/src/api/telegram-types/inline.d.ts +692 -0
- package/dist-cjs/src/api/telegram-types/langs.d.ts +193 -0
- package/dist-cjs/src/api/telegram-types/manage.d.ts +1144 -0
- package/dist-cjs/src/api/telegram-types/markup.d.ts +268 -0
- package/dist-cjs/src/api/telegram-types/message.d.ts +1537 -0
- package/dist-cjs/src/api/telegram-types/methods.d.ts +2870 -0
- package/dist-cjs/src/api/telegram-types/mod.d.ts +14 -0
- package/dist-cjs/src/api/telegram-types/passport.d.ts +163 -0
- package/dist-cjs/src/api/telegram-types/payment.d.ts +570 -0
- package/dist-cjs/src/api/telegram-types/rich.d.ts +1010 -0
- package/dist-cjs/src/api/telegram-types/settings.d.ts +120 -0
- package/dist-cjs/src/api/telegram-types/story.d.ts +89 -0
- package/dist-cjs/src/api/telegram-types/update.d.ts +84 -0
- package/dist-cjs/src/api/telegram.js +2 -0
- package/dist-cjs/src/approval/approval.js +11 -1
- package/dist-cjs/src/cache/cache.js +7 -3
- package/dist-cjs/src/context/context.js +26 -3
- package/dist-cjs/src/core/bot.js +48 -7
- package/dist-cjs/src/index.js +1 -0
- package/dist-cjs/src/queue/queue.js +177 -51
- package/dist-cjs/src/router/router.js +75 -17
- package/dist-cjs/src/state/conversation.js +79 -11
- package/dist-cjs/src/state/menu.js +118 -18
- package/dist-cjs/src/storage/storage.js +135 -5
- package/dist-cjs/src/telegram-features.js +74 -0
- package/dist-cjs/src/testing.js +17 -0
- package/docs/API.id.md +1800 -0
- package/docs/API.md +1799 -0
- package/docs/API.zh-CN.md +1794 -0
- package/docs/README.md +26 -15
- package/package.json +13 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- Callback-query contexts now resolve `message` and `chat` from `callback_query.message`, so `ctx.reply()`, `ctx.edit()`, and `ctx.delete()` work for button callbacks.
|
|
8
|
+
- Router matching is first-match by default; explicit `matchMode: "all"` preserves deliberate fan-out without accidental double replies.
|
|
9
|
+
- Polling isolates handler failures per update, continues the remainder of a batch, emits `update:error`, and uses abortable reconnect backoff.
|
|
10
|
+
- Regex matchers reset `lastIndex` before reuse.
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- JSON-file, Redis, SQL-driver, Mongo-driver, and persistent approval storage adapters.
|
|
15
|
+
- Storage-backed conversations, full five-field cron parsing, scheduler error hooks, permission-aware menus, `MenuController`, Web App init-data validation, PaymentsClient, and vendored Telegram declarations.
|
|
16
|
+
- Callback-update test fixtures and expanded failure-path/regression coverage.
|
|
17
|
+
|
|
3
18
|
## 0.1.2 — 2026-08-19
|
|
4
19
|
|
|
5
20
|
### Changed
|
|
@@ -32,4 +47,4 @@
|
|
|
32
47
|
|
|
33
48
|
### Known limitations
|
|
34
49
|
|
|
35
|
-
The
|
|
50
|
+
The generated method list has runtime coverage for official method names, while specialized request/result inference remains concentrated on the core method map; full Telegram declarations are available through `TelegramTypes`. Scene orchestration and a full Mini App UI layer remain application-owned. Redis, SQL, and Mongo adapters require the application to provide the corresponding vendor driver interface. See `FEATURE_MATRIX.md`.
|
package/README.id.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# telebibz
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
**`@xbibzlibrary/telebibz`** adalah SDK dan framework Telegram Bot untuk Node.js dan TypeScript. Paket ini menyediakan API client, polling, router, middleware, context, keyboard builder, state/session, webhook handler, queue, scheduler, cache, plugin lifecycle, CLI, dan utilitas pengujian.
|
|
6
|
+
|
|
7
|
+
[English](README.md) · **Bahasa Indonesia** · [简体中文](README.zh-CN.md)
|
|
8
|
+
|
|
9
|
+
Referensi API lengkap: [English](docs/API.md) · **Indonesia** · [中文](docs/API.zh-CN.md)
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Instalasi
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @xbibzlibrary/telebibz
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Node.js **20 atau lebih baru** diperlukan.
|
|
20
|
+
|
|
21
|
+
## Bot sederhana
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Bot } from "@xbibzlibrary/telebibz";
|
|
25
|
+
|
|
26
|
+
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!);
|
|
27
|
+
|
|
28
|
+
bot.command("start", (ctx) => ctx.reply("Bot aktif."));
|
|
29
|
+
bot.onText("ping", (ctx) => ctx.reply("pong"));
|
|
30
|
+
|
|
31
|
+
await bot.start();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`Bot.start()` menjalankan long polling. Untuk siklus hidup manual, gunakan `init()`, `launch({ mode: "polling" })`, `health()`, `stop()`, atau `restart()`.
|
|
35
|
+
|
|
36
|
+
## Router dan middleware
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
bot.use(async (ctx, next) => {
|
|
40
|
+
const started = Date.now();
|
|
41
|
+
await next();
|
|
42
|
+
console.log(`processed in ${Date.now() - started}ms`);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
bot.command("help", (ctx) => ctx.reply("Bantuan tersedia."));
|
|
46
|
+
bot.onRegex(/^order:(\\d+)$/, (ctx) => ctx.reply("Order diterima."));
|
|
47
|
+
bot.callback("profile:", (ctx) => ctx.answerCallbackQuery("Dibuka."));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Router mendukung command, text, regex, pola callback, predikat kustom, router bersarang, middleware per rute, dan prioritas rute.
|
|
51
|
+
|
|
52
|
+
## Telegram API
|
|
53
|
+
|
|
54
|
+
Generated method access dan raw access tersedia melalui API client:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
await bot.api.methods.getMe();
|
|
58
|
+
await bot.api.methods.sendMessage({ chat_id: 123456789, text: "Halo." });
|
|
59
|
+
await bot.api.call("sendMessage", { chat_id: 123456789, text: "Halo." });
|
|
60
|
+
await bot.api.raw("futureTelegramMethod", { value: true });
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Transport bawaan menggunakan `fetch`, timeout, retry, exponential backoff, JSON payload, dan multipart upload.
|
|
64
|
+
|
|
65
|
+
Referensi API lengkap untuk setiap class, function, method, type, error, lifecycle, CLI command, dan generated Telegram method tersedia di [`docs/API.id.md`](docs/API.id.md).
|
|
66
|
+
|
|
67
|
+
## Keyboard
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { InlineKeyboard } from "@xbibzlibrary/telebibz";
|
|
71
|
+
|
|
72
|
+
const keyboard = new InlineKeyboard()
|
|
73
|
+
.text("Profil", "profile")
|
|
74
|
+
.url("Dokumentasi", "https://core.telegram.org/bots/api")
|
|
75
|
+
.build();
|
|
76
|
+
|
|
77
|
+
await ctx.reply("Pilih menu:", { reply_markup: keyboard });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Builder hanya menghasilkan payload keyboard native Telegram. UI HTML/CSS memerlukan Mini App atau Web App terpisah.
|
|
81
|
+
|
|
82
|
+
## Persetujuan pemilik
|
|
83
|
+
|
|
84
|
+
Gerbang persetujuan menahan pembaruan biasa sampai pemilik menyetujui bot melalui tombol **Izinkan** atau **Tidak Diizinkan**.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const bot = new Bot({
|
|
88
|
+
token: process.env.TELEGRAM_BOT_TOKEN!,
|
|
89
|
+
approval: {
|
|
90
|
+
ownerChatId: Number(process.env.TELEBIBZ_OWNER_CHAT_ID),
|
|
91
|
+
ownerUserId: Number(process.env.TELEBIBZ_OWNER_USER_ID),
|
|
92
|
+
ownerLabel: "Dev Gantenggg",
|
|
93
|
+
requireApproval: true,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Library mengirim notifikasi ke `ownerChatId`, sedangkan hanya `ownerUserId` yang dapat mengambil keputusan. Callback menggunakan nonce acak. Untuk deployment multi-instance, gunakan `ApprovalStore` persisten melalui database atau Redis; default-nya adalah memory store.
|
|
99
|
+
|
|
100
|
+
## Webhook
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { Bot, createWebhookHandler } from "@xbibzlibrary/telebibz";
|
|
104
|
+
|
|
105
|
+
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!);
|
|
106
|
+
const handler = createWebhookHandler(bot, {
|
|
107
|
+
secretToken: process.env.TELEGRAM_WEBHOOK_SECRET,
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`createWebhookHandler` menerima Request Web standar dan menghasilkan Response. Secret token, ukuran body, parsing JSON, dan penanganan update duplikat diverifikasi oleh handler.
|
|
112
|
+
|
|
113
|
+
## State, queue, scheduler, dan cache
|
|
114
|
+
|
|
115
|
+
Paket menyediakan `MemoryStorage` dengan TTL dan pembaruan atomik, `JsonFileStorage`, `RedisStorage`, `SqlStorage`, `MongoStorage`, persistent approval storage, session bot, conversation dan form berbasis Storage, menu berbasis permission, pagination `MenuController`, `MemoryCache`, token-bucket limiter, task queue dengan retry/backoff/concurrency/delay/cancel, serta scheduler interval, one-shot, dan cron lima field lengkap. Adapter Redis, SQL, dan Mongo memakai driver kecil sehingga core package tetap tanpa runtime dependency vendor.
|
|
116
|
+
|
|
117
|
+
## CLI
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
npx telebibz init my-bot
|
|
121
|
+
npx telebibz doctor
|
|
122
|
+
npx telebibz build
|
|
123
|
+
npx telebibz test
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Testing
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npm run typecheck
|
|
130
|
+
npm run test:types
|
|
131
|
+
npm run lint
|
|
132
|
+
npm test
|
|
133
|
+
npm run build
|
|
134
|
+
npm run security
|
|
135
|
+
npm run release:check
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
E2E Telegram nyata memerlukan `TELEGRAM_BOT_TOKEN` dan `TELEGRAM_TEST_CHAT_ID`. Tanpa kredensial, E2E akan dilewati dan tidak dihitung sebagai lulus.
|
|
139
|
+
|
|
140
|
+
## Web App dan pembayaran
|
|
141
|
+
|
|
142
|
+
`validateWebAppInitData()` memverifikasi signature dan expiration Telegram Web App. `PaymentsClient` menyediakan wrapper invoice link, invoice, jawaban pre-checkout, jawaban Web App query, transaksi Stars, dan refund Stars. Gunakan `TelegramTypes` serta alias seperti `TelegramUser`, `TelegramMessage`, dan `TelegramUpdate` untuk full Telegram declaration surface yang divendor.
|
|
143
|
+
|
|
144
|
+
## API target dan batasan
|
|
145
|
+
|
|
146
|
+
Daftar method dihasilkan dari dokumentasi Telegram Bot API saat skema diperbarui. Akses runtime tersedia untuk method resmi yang terdeteksi, sedangkan inferensi parameter/result khusus dipusatkan pada core method map. Full declaration Telegram untuk object, union, enum, dan method tersedia melalui `TelegramTypes`. Lihat [FEATURE_MATRIX.md](FEATURE_MATRIX.md) untuk status implementasi dan [APPROVAL_FEATURE.md](APPROVAL_FEATURE.md) untuk detail persetujuan.
|
|
147
|
+
|
|
148
|
+
## Otomatisasi release
|
|
149
|
+
|
|
150
|
+
Repository GitHub menyediakan CI dan workflow auto-publish. Setiap push ke `main` menjalankan quality gates, memilih patch version yang belum dipakai, membuat commit dan tag, menerbitkan package ke npm, lalu membuat GitHub Release. Karena source repository bersifat private, workflow menggunakan `--provenance=false`, sesuai batasan npm untuk source private. Konfigurasikan secret `NPM_TOKEN` pada GitHub Actions sebelum mengandalkan publish otomatis. Lihat [RELEASE_AUTOMATION.md](RELEASE_AUTOMATION.md).
|
|
151
|
+
|
|
152
|
+
## Keamanan
|
|
153
|
+
|
|
154
|
+
Jangan commit token Telegram atau npm. Gunakan variabel lingkungan atau secret manager. Untuk kebijakan keamanan dan peningkatan keamanan rilis, lihat [SECURITY.md](SECURITY.md) dan [RELEASE_POLICY.md](RELEASE_POLICY.md).
|
|
155
|
+
|
|
156
|
+
## Lisensi
|
|
157
|
+
|
|
158
|
+
MIT. Lihat [LICENSE](LICENSE).
|
package/README.md
CHANGED
|
@@ -2,32 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://github.com/XbibzOfficial777/telebibz/actions/workflows/ci.yml)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
**`@xbibzlibrary/telebibz`** is a full-scale Telegram Bot SDK and framework for Node.js and TypeScript. It provides a typed API client, polling, routing, middleware, context helpers, keyboard builders, state/session primitives, webhooks, queues, scheduling, caching, plugin lifecycle, approval gates, CLI tooling, and testing utilities.
|
|
8
|
+
|
|
9
|
+
## Documentation languages
|
|
10
|
+
|
|
11
|
+
**English (default)** · [Bahasa Indonesia](README.id.md) · [简体中文](README.zh-CN.md)
|
|
12
|
+
|
|
13
|
+
Complete API references: [English](docs/API.md) · [Indonesia](docs/API.id.md) · [中文](docs/API.zh-CN.md)
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
## Installation
|
|
8
18
|
|
|
9
19
|
```bash
|
|
10
20
|
npm install @xbibzlibrary/telebibz
|
|
11
21
|
```
|
|
12
22
|
|
|
13
|
-
Node.js **20
|
|
23
|
+
Node.js **20 or newer** is required.
|
|
14
24
|
|
|
15
|
-
##
|
|
25
|
+
## Minimal bot
|
|
16
26
|
|
|
17
27
|
```ts
|
|
18
28
|
import { Bot } from "@xbibzlibrary/telebibz";
|
|
19
29
|
|
|
20
30
|
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!);
|
|
21
31
|
|
|
22
|
-
bot.command("start", (ctx) => ctx.reply("Bot
|
|
32
|
+
bot.command("start", (ctx) => ctx.reply("Bot is active."));
|
|
23
33
|
bot.onText("ping", (ctx) => ctx.reply("pong"));
|
|
24
34
|
|
|
25
35
|
await bot.start();
|
|
26
36
|
```
|
|
27
37
|
|
|
28
|
-
`Bot.start()`
|
|
38
|
+
`Bot.start()` runs long polling. For manual lifecycle control, use `init()`, `launch({ mode: "polling" })`, `health()`, `stop()`, or `restart()`.
|
|
29
39
|
|
|
30
|
-
## Router
|
|
40
|
+
## Router and middleware
|
|
31
41
|
|
|
32
42
|
```ts
|
|
33
43
|
bot.use(async (ctx, next) => {
|
|
@@ -36,44 +46,44 @@ bot.use(async (ctx, next) => {
|
|
|
36
46
|
console.log(`processed in ${Date.now() - started}ms`);
|
|
37
47
|
});
|
|
38
48
|
|
|
39
|
-
bot.command("help", (ctx) => ctx.reply("
|
|
40
|
-
bot.onRegex(/^order:(\\d+)$/, (ctx) => ctx.reply("Order
|
|
41
|
-
bot.callback("profile:", (ctx) => ctx.answerCallbackQuery("
|
|
49
|
+
bot.command("help", (ctx) => ctx.reply("Help is available."));
|
|
50
|
+
bot.onRegex(/^order:(\\d+)$/, (ctx) => ctx.reply("Order received."));
|
|
51
|
+
bot.callback("profile:", (ctx) => ctx.answerCallbackQuery("Opened."));
|
|
42
52
|
```
|
|
43
53
|
|
|
44
|
-
|
|
54
|
+
The router supports commands, exact text, regular expressions, callback patterns, custom predicates, nested routers, per-route middleware, and route priority.
|
|
45
55
|
|
|
46
56
|
## Telegram API
|
|
47
57
|
|
|
48
|
-
Generated method access
|
|
58
|
+
Generated method access and raw access are available through the API client:
|
|
49
59
|
|
|
50
60
|
```ts
|
|
51
61
|
await bot.api.methods.getMe();
|
|
52
|
-
await bot.api.methods.sendMessage({ chat_id: 123456789, text: "
|
|
53
|
-
await bot.api.call("sendMessage", { chat_id: 123456789, text: "
|
|
62
|
+
await bot.api.methods.sendMessage({ chat_id: 123456789, text: "Hello." });
|
|
63
|
+
await bot.api.call("sendMessage", { chat_id: 123456789, text: "Hello." });
|
|
54
64
|
await bot.api.raw("futureTelegramMethod", { value: true });
|
|
55
65
|
```
|
|
56
66
|
|
|
57
|
-
|
|
67
|
+
The built-in transport uses `fetch`, timeouts, retries, exponential backoff, JSON payloads, and multipart upload.
|
|
58
68
|
|
|
59
|
-
## Keyboard
|
|
69
|
+
## Keyboard builders
|
|
60
70
|
|
|
61
71
|
```ts
|
|
62
72
|
import { InlineKeyboard } from "@xbibzlibrary/telebibz";
|
|
63
73
|
|
|
64
74
|
const keyboard = new InlineKeyboard()
|
|
65
|
-
.text("
|
|
66
|
-
.url("
|
|
75
|
+
.text("Profile", "profile")
|
|
76
|
+
.url("Documentation", "https://core.telegram.org/bots/api")
|
|
67
77
|
.build();
|
|
68
78
|
|
|
69
|
-
await ctx.reply("
|
|
79
|
+
await ctx.reply("Choose an option:", { reply_markup: keyboard });
|
|
70
80
|
```
|
|
71
81
|
|
|
72
|
-
|
|
82
|
+
Builders produce native Telegram keyboard payloads. HTML/CSS interfaces require a separate Mini App or Web App.
|
|
73
83
|
|
|
74
|
-
## Owner approval
|
|
84
|
+
## Owner approval gate
|
|
75
85
|
|
|
76
|
-
|
|
86
|
+
The approval gate pauses regular updates until the owner approves or denies a new bot through inline buttons.
|
|
77
87
|
|
|
78
88
|
```ts
|
|
79
89
|
const bot = new Bot({
|
|
@@ -87,7 +97,7 @@ const bot = new Bot({
|
|
|
87
97
|
});
|
|
88
98
|
```
|
|
89
99
|
|
|
90
|
-
|
|
100
|
+
The library sends the notification to `ownerChatId`, while only `ownerUserId` can decide. Callback data uses a random nonce. For multi-instance deployments, provide a persistent `ApprovalStore`; the default store is in memory.
|
|
91
101
|
|
|
92
102
|
## Webhook
|
|
93
103
|
|
|
@@ -100,11 +110,11 @@ const handler = createWebhookHandler(bot, {
|
|
|
100
110
|
});
|
|
101
111
|
```
|
|
102
112
|
|
|
103
|
-
`createWebhookHandler`
|
|
113
|
+
`createWebhookHandler` accepts a standard Web `Request` and returns a `Response`. It verifies the optional secret token, body size, JSON payload, and update shape before calling `bot.handleUpdate()`.
|
|
104
114
|
|
|
105
|
-
## State, queue, scheduler,
|
|
115
|
+
## State, queue, scheduler, and cache
|
|
106
116
|
|
|
107
|
-
|
|
117
|
+
The package provides `MemoryStorage` with TTL and serialized per-key updates, `JsonFileStorage`, `RedisStorage`, `SqlStorage`, `MongoStorage`, persistent approval storage, bot sessions, storage-backed conversations and forms, permission-aware menus, `MenuController` pagination, `MemoryCache`, a token-bucket limiter, a task queue with retry/backoff/concurrency/delay/cancel, and schedulers for intervals, one-shot tasks, and full five-field cron expressions. Redis, SQL, and Mongo adapters use small driver interfaces so the core package remains free of vendor runtime dependencies.
|
|
108
118
|
|
|
109
119
|
## CLI
|
|
110
120
|
|
|
@@ -127,16 +137,26 @@ npm run security
|
|
|
127
137
|
npm run release:check
|
|
128
138
|
```
|
|
129
139
|
|
|
130
|
-
Real Telegram E2E
|
|
140
|
+
Real Telegram E2E tests require `TELEGRAM_BOT_TOKEN` and `TELEGRAM_TEST_CHAT_ID`. Without credentials, E2E tests are skipped and are not counted as passing.
|
|
141
|
+
|
|
142
|
+
## Web Apps and payments
|
|
143
|
+
|
|
144
|
+
`validateWebAppInitData()` verifies Telegram Web App signatures and expiration. `PaymentsClient` provides wrappers for invoice links, invoices, pre-checkout answers, Web App query answers, Stars transactions, and Stars refunds. Use `TelegramTypes` and aliases such as `TelegramUser`, `TelegramMessage`, and `TelegramUpdate` for the vendored full Telegram declaration surface.
|
|
145
|
+
|
|
146
|
+
## API targets and limitations
|
|
147
|
+
|
|
148
|
+
The generated method list is derived from the Telegram Bot API schema when it is updated. Runtime access is available for detected official methods, while specialized request/result inference remains concentrated on the core method map. The complete vendored Telegram object, union, enum, and method declarations are available through `TelegramTypes`. See [FEATURE_MATRIX.md](FEATURE_MATRIX.md) for implementation status and [APPROVAL_FEATURE.md](APPROVAL_FEATURE.md) for approval details.
|
|
149
|
+
|
|
150
|
+
For every exported class, function, method, type, error, lifecycle hook, CLI command, and generated Telegram method, see the [complete English API reference](docs/API.md).
|
|
131
151
|
|
|
132
|
-
##
|
|
152
|
+
## Release automation
|
|
133
153
|
|
|
134
|
-
|
|
154
|
+
The GitHub repository includes CI and an auto-publish workflow. A push to `main` runs the quality gates, chooses the next unused patch version, commits the version, creates a tag, publishes to npm, and creates a GitHub Release. Because the source repository is private, the workflow uses `--provenance=false`, which npm requires for private source repositories. Configure the `NPM_TOKEN` GitHub Actions secret before relying on automatic publication. See [RELEASE_AUTOMATION.md](RELEASE_AUTOMATION.md).
|
|
135
155
|
|
|
136
|
-
##
|
|
156
|
+
## Security
|
|
137
157
|
|
|
138
|
-
|
|
158
|
+
Never commit Telegram tokens or npm credentials. Use environment variables or a secret manager. See [SECURITY.md](SECURITY.md) and [RELEASE_POLICY.md](RELEASE_POLICY.md) for security and release hardening policies.
|
|
139
159
|
|
|
140
|
-
##
|
|
160
|
+
## License
|
|
141
161
|
|
|
142
|
-
MIT.
|
|
162
|
+
MIT. See [LICENSE](LICENSE).
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# telebibz
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
**`@xbibzlibrary/telebibz`** 是一个面向 Node.js 和 TypeScript 的 Telegram Bot SDK 和框架。该包提供 API 客户端、轮询、路由器、中间件、上下文、键盘构造器、状态/会话、Webhook 处理、队列、调度器、缓存、插件生命周期、CLI 以及测试工具。
|
|
6
|
+
|
|
7
|
+
[English](README.md) · [Bahasa Indonesia](README.id.md) · **简体中文**
|
|
8
|
+
|
|
9
|
+
完整 API 参考:[English](docs/API.md) · [Indonesia](docs/API.id.md) · **中文**
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @xbibzlibrary/telebibz
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
需要 Node.js **20 或更高版本**。
|
|
20
|
+
|
|
21
|
+
## 简单机器人
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Bot } from "@xbibzlibrary/telebibz";
|
|
25
|
+
|
|
26
|
+
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!);
|
|
27
|
+
|
|
28
|
+
bot.command("start", (ctx) => ctx.reply("Bot aktif."));
|
|
29
|
+
bot.onText("ping", (ctx) => ctx.reply("pong"));
|
|
30
|
+
|
|
31
|
+
await bot.start();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`Bot.start()` 会运行长轮询。要手动管理生命周期,请使用 `init()`, `launch({ mode: "polling" })`, `health()`, `stop()`, 或 `restart()`。
|
|
35
|
+
|
|
36
|
+
## 路由器与中间件
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
bot.use(async (ctx, next) => {
|
|
40
|
+
const started = Date.now();
|
|
41
|
+
await next();
|
|
42
|
+
console.log(`processed in ${Date.now() - started}ms`);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
bot.command("help", (ctx) => ctx.reply("Bantuan tersedia."));
|
|
46
|
+
bot.onRegex(/^order:(\\d+)$/, (ctx) => ctx.reply("Order diterima."));
|
|
47
|
+
bot.callback("profile:", (ctx) => ctx.answerCallbackQuery("Dibuka."));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
路由器支持命令、文本、正则、回调模式、自定义谓词、嵌套路由器、每条路由的中间件,以及路由优先级。
|
|
51
|
+
|
|
52
|
+
## Telegram API
|
|
53
|
+
|
|
54
|
+
通过 API 客户端可以使用生成的方法调用和原始调用:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
await bot.api.methods.getMe();
|
|
58
|
+
await bot.api.methods.sendMessage({ chat_id: 123456789, text: "Halo." });
|
|
59
|
+
await bot.api.call("sendMessage", { chat_id: 123456789, text: "Halo." });
|
|
60
|
+
await bot.api.raw("futureTelegramMethod", { value: true });
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
内置传输使用 `fetch`,支持超时、重试、指数退避、JSON 载荷和多部分上传。
|
|
64
|
+
|
|
65
|
+
关于每个 class、function、method、type、error、lifecycle、CLI 命令和生成的 Telegram 方法的完整 API 参考请参见 [`docs/API.zh-CN.md`](docs/API.zh-CN.md)。
|
|
66
|
+
|
|
67
|
+
## 键盘
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { InlineKeyboard } from "@xbibzlibrary/telebibz";
|
|
71
|
+
|
|
72
|
+
const keyboard = new InlineKeyboard()
|
|
73
|
+
.text("Profil", "profile")
|
|
74
|
+
.url("Dokumentasi", "https://core.telegram.org/bots/api")
|
|
75
|
+
.build();
|
|
76
|
+
|
|
77
|
+
await ctx.reply("Pilih menu:", { reply_markup: keyboard });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
构造器仅生成 Telegram 原生键盘的 payload。HTML/CSS 的 UI 需要单独的 Mini App 或 Web App。
|
|
81
|
+
|
|
82
|
+
## 所有者审批
|
|
83
|
+
|
|
84
|
+
审批门将在所有者通过 **允许** 或 **不允许** 按钮批准机器人之前拦截普通更新。
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const bot = new Bot({
|
|
88
|
+
token: process.env.TELEGRAM_BOT_TOKEN!,
|
|
89
|
+
approval: {
|
|
90
|
+
ownerChatId: Number(process.env.TELEBIBZ_OWNER_CHAT_ID),
|
|
91
|
+
ownerUserId: Number(process.env.TELEBIBZ_OWNER_USER_ID),
|
|
92
|
+
ownerLabel: "Dev Gantenggg",
|
|
93
|
+
requireApproval: true,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
库会向 `ownerChatId` 发送通知,但只有 `ownerUserId` 可以做出决策。回调使用随机 nonce。对于多实例部署,请通过数据库或 Redis 使用持久化的 `ApprovalStore`;默认是内存存储。
|
|
99
|
+
|
|
100
|
+
## Webhook
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { Bot, createWebhookHandler } from "@xbibzlibrary/telebibz";
|
|
104
|
+
|
|
105
|
+
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!);
|
|
106
|
+
const handler = createWebhookHandler(bot, {
|
|
107
|
+
secretToken: process.env.TELEGRAM_WEBHOOK_SECRET,
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`createWebhookHandler` 接受标准 Web `Request` 并返回 `Response`。处理程序会验证 secret token、body 大小、JSON 解析以及重复更新处理。
|
|
112
|
+
|
|
113
|
+
## 状态、队列、调度器和缓存
|
|
114
|
+
|
|
115
|
+
该包提供带 TTL 和原子更新的 `MemoryStorage`、`JsonFileStorage`、`RedisStorage`、`SqlStorage`、`MongoStorage`、持久化 approval storage、bot session、基于 Storage 的 conversation/form、基于 permission 的菜单、`MenuController` 分页、`MemoryCache`、令牌桶限流器、支持重试/退避/并发/延迟/取消的任务队列,以及间隔、一次性和完整五字段 cron 的调度器。Redis、SQL 和 Mongo 适配器使用小型 driver interface,因此 core package 不需要 vendor runtime dependency。
|
|
116
|
+
|
|
117
|
+
## CLI
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
npx telebibz init my-bot
|
|
121
|
+
npx telebibz doctor
|
|
122
|
+
npx telebibz build
|
|
123
|
+
npx telebibz test
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## 测试
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npm run typecheck
|
|
130
|
+
npm run test:types
|
|
131
|
+
npm run lint
|
|
132
|
+
npm test
|
|
133
|
+
npm run build
|
|
134
|
+
npm run security
|
|
135
|
+
npm run release:check
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
真实的 Telegram E2E 需要 `TELEGRAM_BOT_TOKEN` 和 `TELEGRAM_TEST_CHAT_ID`。没有凭证时,E2E 将被跳过且不计为通过。
|
|
139
|
+
|
|
140
|
+
## Web App 和支付
|
|
141
|
+
|
|
142
|
+
`validateWebAppInitData()` 会验证 Telegram Web App 的 signature 和 expiration。`PaymentsClient` 提供 invoice link、invoice、pre-checkout answer、Web App query answer、Stars transactions 和 Stars refunds 的 wrapper。使用 `TelegramTypes` 以及 `TelegramUser`、`TelegramMessage`、`TelegramUpdate` 等 alias 来访问完整的 vendored Telegram declaration surface。
|
|
143
|
+
|
|
144
|
+
## API 目标与限制
|
|
145
|
+
|
|
146
|
+
方法列表会在 schema 更新时根据 Telegram Bot API 文档生成。检测到的官方方法都可以运行时访问,而专门的参数/结果推断主要集中在 core method map。完整的 Telegram object、union、enum 和 method declaration 可通过 `TelegramTypes` 使用。有关实现状态请参见 [FEATURE_MATRIX.md](FEATURE_MATRIX.md),有关审批的详细信息请参见 [APPROVAL_FEATURE.md](APPROVAL_FEATURE.md)。
|
|
147
|
+
|
|
148
|
+
## 发布自动化
|
|
149
|
+
|
|
150
|
+
GitHub repository 提供 CI 和自动发布 workflow。每次推送到 `main` 都会运行 quality gates,选择尚未使用的 patch version,创建 commit 和 tag,发布 npm package,然后创建 GitHub Release。由于 source repository 是 private,workflow 使用 `--provenance=false`,符合 npm 对 private source 的限制。依赖自动发布前,请在 GitHub Actions 中配置 `NPM_TOKEN` secret。请参阅 [RELEASE_AUTOMATION.md](RELEASE_AUTOMATION.md)。
|
|
151
|
+
|
|
152
|
+
## 安全
|
|
153
|
+
|
|
154
|
+
不要将 Telegram token 或 npm 凭证提交到版本控制。使用环境变量或机密管理器。有关安全策略和发布加固,请参见 [SECURITY.md](SECURITY.md) 和 [RELEASE_POLICY.md](RELEASE_POLICY.md)。
|
|
155
|
+
|
|
156
|
+
## 许可证
|
|
157
|
+
|
|
158
|
+
MIT。参见 [LICENSE](LICENSE)。
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# GitHub-to-npm Release Automation
|
|
2
|
+
|
|
3
|
+
Repository ini menggunakan GitHub Actions untuk menjaga source GitHub dan package npm tetap sinkron melalui satu jalur release yang tervalidasi. Repository source tetap private; karena itu workflow menggunakan npm publish tanpa provenance, sesuai batasan registry npm untuk source private.
|
|
4
|
+
|
|
5
|
+
## Alur otomatis
|
|
6
|
+
|
|
7
|
+
Setiap push ke branch `main` menjalankan workflow `.github/workflows/auto-publish.yml`, kecuali commit tersebut memuat marker `[skip release]`.
|
|
8
|
+
|
|
9
|
+
| Tahap | Perilaku |
|
|
10
|
+
|---|---|
|
|
11
|
+
| Checkout | Mengambil seluruh history agar tag dapat dibuat dengan benar. |
|
|
12
|
+
| Install | Menjalankan `npm ci --ignore-scripts`. |
|
|
13
|
+
| Version | Membaca versi dari `package.json`, membaca versi latest npm, lalu memilih patch version berikutnya yang lebih tinggi dari keduanya. |
|
|
14
|
+
| Verification | Menjalankan typecheck, type-level tests, lint, runtime tests, build ESM/CommonJS, security audit, dan release check. |
|
|
15
|
+
| Immutable guard | Menolak publish jika versi target sudah ada di npm. |
|
|
16
|
+
| Git sync | Commit otomatis `chore(release): vX.Y.Z [skip release]`, membuat annotated tag `vX.Y.Z`, lalu push commit dan tag ke GitHub. |
|
|
17
|
+
| npm publish | Menerbitkan package public menggunakan `NPM_TOKEN`; provenance dinonaktifkan karena npm menolak provenance dari source repository private. |
|
|
18
|
+
| GitHub Release | Membuat GitHub Release dengan generated notes. |
|
|
19
|
+
|
|
20
|
+
Push commit version otomatis tidak memicu release kedua karena mengandung `[skip release]`. Workflow menggunakan concurrency sehingga release berjalan satu per satu.
|
|
21
|
+
|
|
22
|
+
## Secret yang wajib tersedia
|
|
23
|
+
|
|
24
|
+
Buka repository GitHub, kemudian masuk ke **Settings → Secrets and variables → Actions** dan tambahkan repository secret:
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
NPM_TOKEN=${NPM_TOKEN}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Jika memakai GitHub Environment bernama `npm-release`, secret dapat disimpan sebagai environment secret dan environment tersebut dapat diberi required reviewers untuk approval manual sebelum publish.
|
|
31
|
+
|
|
32
|
+
Jangan menyimpan token di repository, `.npmrc`, source code, issue, commit, atau workflow. Token npm yang pernah ditempelkan di chat harus dicabut dan diganti dengan granular token baru.
|
|
33
|
+
|
|
34
|
+
## Aturan penggunaan
|
|
35
|
+
|
|
36
|
+
Perubahan source biasa dapat dipush ke `main`; workflow akan menghasilkan patch release baru setelah seluruh quality gates lulus. Karena versi npm immutable, workflow tidak pernah menimpa versi yang telah ada.
|
|
37
|
+
|
|
38
|
+
Untuk perubahan besar, ubah `package.json` ke major/minor version yang diinginkan sebelum push. Workflow tetap memastikan hasil akhir lebih tinggi daripada versi npm yang sudah terbit, lalu menaikkan patch dari versi tertinggi tersebut.
|
|
39
|
+
|
|
40
|
+
Untuk perubahan dokumentasi atau perubahan internal yang tidak boleh menerbitkan npm, gunakan commit message yang memuat marker berikut:
|
|
41
|
+
|
|
42
|
+
```text
|
|
43
|
+
docs: update API reference [skip release]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Untuk memicu workflow secara manual, gunakan **Actions → Auto publish to npm → Run workflow**. Manual trigger tetap menjalankan versioning dan semua gate yang sama.
|
|
47
|
+
|
|
48
|
+
## Verifikasi lokal
|
|
49
|
+
|
|
50
|
+
Sebelum push, jalankan:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm run typecheck
|
|
54
|
+
npm run test:types
|
|
55
|
+
npm run lint
|
|
56
|
+
npm test
|
|
57
|
+
npm run build
|
|
58
|
+
npm run security
|
|
59
|
+
npm run release:check
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Setelah workflow selesai, verifikasi:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm view @xbibzlibrary/telebibz version dist.integrity dist.tarball
|
|
66
|
+
```
|
package/RELEASE_POLICY.md
CHANGED
|
@@ -6,9 +6,9 @@ The published artifact is scoped as `@xbibzlibrary/telebibz`. npm package versio
|
|
|
6
6
|
|
|
7
7
|
## Hardening controls
|
|
8
8
|
|
|
9
|
-
Every release must pass strict TypeScript typechecking, type-level tests, lint, runtime tests, build for ESM and CommonJS, dependency audit, and `release:check`. The release checker rejects an incorrect scope, private package status, missing public publish configuration,
|
|
9
|
+
Every release must pass strict TypeScript typechecking, type-level tests, lint, runtime tests, build for ESM and CommonJS, dependency audit, and `release:check`. The release checker rejects an incorrect scope, private package status, missing public publish configuration, install lifecycle scripts, credential patterns in tracked release content, and incomplete tarball contents. npm provenance is required when the source repository is public; npm rejects provenance for private source repositories, so the current private-repository workflow explicitly uses `--provenance=false`.
|
|
10
10
|
|
|
11
|
-
The package uses
|
|
11
|
+
The package uses a protected GitHub Actions release workflow. The publish token must be stored only as a repository secret or temporary `NPM_TOKEN` environment variable with the smallest possible scope. Tokens must never be committed, placed in `.npmrc` inside the repository, or included in archives.
|
|
12
12
|
|
|
13
13
|
## Release verification
|
|
14
14
|
|