@2byte/tgbot-framework 1.0.3 → 1.0.5

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 (58) hide show
  1. package/README.md +300 -300
  2. package/bin/2byte-cli.ts +97 -97
  3. package/package.json +6 -5
  4. package/src/cli/CreateBotCommand.ts +181 -181
  5. package/src/cli/GenerateCommand.ts +195 -195
  6. package/src/cli/InitCommand.ts +107 -107
  7. package/src/cli/TgAccountManager.ts +50 -0
  8. package/src/console/migrate.ts +82 -82
  9. package/src/core/ApiService.ts +20 -20
  10. package/src/core/ApiServiceManager.ts +63 -63
  11. package/src/core/App.ts +1143 -1113
  12. package/src/core/BotArtisan.ts +79 -79
  13. package/src/core/BotMigration.ts +30 -30
  14. package/src/core/BotSeeder.ts +66 -66
  15. package/src/core/Model.ts +84 -84
  16. package/src/core/utils.ts +2 -2
  17. package/src/illumination/Artisan.ts +149 -149
  18. package/src/illumination/InlineKeyboard.ts +61 -61
  19. package/src/illumination/Message2Byte.ts +255 -255
  20. package/src/illumination/Message2ByteLiveProgressive.ts +278 -278
  21. package/src/illumination/Message2bytePool.ts +107 -107
  22. package/src/illumination/Migration.ts +186 -186
  23. package/src/illumination/RunSectionRoute.ts +85 -85
  24. package/src/illumination/Section.ts +410 -410
  25. package/src/illumination/SectionComponent.ts +64 -64
  26. package/src/illumination/Telegraf2byteContext.ts +32 -32
  27. package/src/index.ts +42 -35
  28. package/src/libs/TelegramAccountControl.ts +1140 -738
  29. package/src/libs/TgSender.ts +53 -0
  30. package/src/models/Model.ts +67 -0
  31. package/src/models/Proxy.ts +218 -0
  32. package/src/models/TgAccount.ts +362 -0
  33. package/src/models/index.ts +3 -0
  34. package/src/types.ts +191 -188
  35. package/src/user/UserModel.ts +297 -297
  36. package/src/user/UserStore.ts +119 -119
  37. package/src/workflow/services/MassSendApiService.ts +80 -80
  38. package/templates/bot/.env.example +23 -19
  39. package/templates/bot/artisan.ts +8 -8
  40. package/templates/bot/bot.ts +82 -79
  41. package/templates/bot/database/dbConnector.ts +4 -4
  42. package/templates/bot/database/migrate.ts +9 -9
  43. package/templates/bot/database/migrations/001_create_users.sql +18 -18
  44. package/templates/bot/database/migrations/007_proxy.sql +27 -0
  45. package/templates/bot/database/migrations/008_tg_accounts.sql +32 -0
  46. package/templates/bot/database/seed.ts +14 -14
  47. package/templates/bot/docs/CLI_SERVICES.md +536 -0
  48. package/templates/bot/docs/INPUT_SYSTEM.md +211 -0
  49. package/templates/bot/docs/SERVICE_EXAMPLES.md +384 -0
  50. package/templates/bot/docs/TASK_SYSTEM.md +156 -0
  51. package/templates/bot/models/Model.ts +7 -0
  52. package/templates/bot/models/index.ts +2 -0
  53. package/templates/bot/package.json +30 -30
  54. package/templates/bot/sectionList.ts +9 -9
  55. package/templates/bot/sections/ExampleInputSection.ts +85 -85
  56. package/templates/bot/sections/ExampleLiveTaskerSection.ts +60 -60
  57. package/templates/bot/sections/HomeSection.ts +63 -63
  58. package/templates/bot/workflow/services/{ExampleServise.ts → ExampleService.ts} +23 -23
package/README.md CHANGED
@@ -1,301 +1,301 @@
1
- # @2byte/tgbot-framework
2
-
3
- 🚀 TypeScript framework для создания Telegram ботов с sections-based архитектурой, оптимизированный для **Bun**.
4
-
5
- ## 🎯 Концепция
6
-
7
- Это **npm библиотека** которая предоставляет:
8
- - 🏗️ **Framework** для создания ботов
9
- - 🛠️ **CLI инструменты** для генерации кода
10
- - � **Готовые компоненты** (Section, Migration, Artisan)
11
- - 🎨 **TypeScript типы** из коробки
12
-
13
- ## 📦 Установка
14
-
15
- ```bash
16
- # Глобальная установка для CLI
17
- bun add -g @2byte/tgbot-framework
18
-
19
- # Или локальная установка в проект
20
- bun add @2byte/tgbot-framework
21
- ```
22
-
23
- ## 🚀 Быстрый старт
24
-
25
- ### 1. Создание нового бота
26
-
27
- ```bash
28
- # Создать новый бот с помощью CLI
29
- 2byte-bot create my-awesome-bot
30
- cd my-awesome-bot
31
-
32
- # Настроить окружение
33
- cp .env.example .env
34
- # Добавить BOT_TOKEN в .env
35
-
36
- # Установить зависимости и запустить
37
- bun install
38
- bun run dev
39
- ```
40
-
41
- ### 2. Использование в существующем проекте
42
-
43
- ```bash
44
- # Установить фреймворк
45
- bun add @2byte/tgbot-framework
46
-
47
- # Инициализировать структуру
48
- 2byte-bot init
49
- ```
50
-
51
- ## CLI Commands
52
-
53
- ### Bot Management
54
-
55
- ```bash
56
- 2byte create-bot <name> # Create a new bot
57
- 2byte init # Initialize 2byte bot in current directory
58
- ```
59
-
60
- ### Code Generation
61
-
62
- ```bash
63
- 2byte generate section <name> # Generate a new section
64
- 2byte generate migration <name> # Generate a new migration
65
- ```
66
-
67
- ### Project Commands (inside bot directory)
68
-
69
- ```bash
70
- bun run artisan make:section <name> # Create new section
71
- bun run artisan add:method <section> <method> # Add method to section
72
- bun run artisan list:sections # List all sections
73
-
74
- bun run migrate # Run migrations
75
- bun run seed # Seed database
76
- bun run seed:clear # Clear and reseed database
77
- bun run seed:clean # Clean database only
78
- ```
79
-
80
- ## Project Structure
81
-
82
- When you create a new bot, you'll get this structure:
83
-
84
- ```
85
- my-awesome-bot/
86
- ├── bot.ts # Main bot entry point
87
- ├── artisan.ts # Artisan CLI for this bot
88
- ├── sections/ # Bot sections
89
- │ └── HomeSection.ts # Default home section
90
- ├── database/
91
- │ ├── migrate.ts # Migration runner
92
- │ ├── seed.ts # Database seeder
93
- │ ├── migrations/ # Migration files
94
- │ └── database.sqlite # SQLite database
95
- ├── package.json
96
- └── .env.example
97
- ```
98
-
99
- ## Creating Sections
100
-
101
- Sections are the main building blocks of your bot. Each section handles specific functionality:
102
-
103
- ```typescript
104
- import { Section, SectionOptions, InlineKeyboard } from '2bytetgbot';
105
-
106
- export default class AuthSection extends Section {
107
- static command = "auth";
108
- static description = "Authentication section";
109
- static actionRoutes = {
110
- "auth.login": "login",
111
- "auth.register": "register",
112
- };
113
-
114
- public sectionId = "auth";
115
-
116
- constructor(options: SectionOptions) {
117
- super(options);
118
- }
119
-
120
- async login() {
121
- const message = "Please enter your credentials...";
122
- await this.message(message).send();
123
- }
124
-
125
- async register() {
126
- const message = "Registration form...";
127
- await this.message(message).send();
128
- }
129
- }
130
- ```
131
-
132
- ## Database Migrations
133
-
134
- Create database tables and modify schema using migrations:
135
-
136
- ```bash
137
- bun run artisan generate migration create_users_table
138
- ```
139
-
140
- This creates a migration file like `001_create_users_table.sql`:
141
-
142
- ```sql
143
- -- UP
144
- CREATE TABLE IF NOT EXISTS users (
145
- id INTEGER PRIMARY KEY AUTOINCREMENT,
146
- username TEXT NOT NULL UNIQUE,
147
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP
148
- );
149
-
150
- -- DOWN
151
- DROP TABLE IF EXISTS users;
152
- ```
153
-
154
- Run migrations:
155
-
156
- ```bash
157
- bun run migrate up # Run all pending migrations
158
- bun run migrate down 2 # Rollback last 2 migrations
159
- bun run migrate status # Show migration status
160
- ```
161
-
162
- ## Database Seeding
163
-
164
- Populate your database with test data:
165
-
166
- ```typescript
167
- import { Database } from 'bun:sqlite';
168
-
169
- export async function seedUsers(db: Database) {
170
- const stmt = db.prepare(`
171
- INSERT INTO users (username) VALUES (?)
172
- `);
173
-
174
- const users = ['alice', 'bob', 'charlie'];
175
-
176
- for (const username of users) {
177
- stmt.run(username);
178
- console.log(`✅ Created user: ${username}`);
179
- }
180
- }
181
- ```
182
-
183
- ## Bot Configuration
184
-
185
- Configure your bot in `bot.ts`:
186
-
187
- ```typescript
188
- import 'dotenv/config';
189
- import { App } from '2bytetgbot';
190
- import HomeSection from './sections/HomeSection';
191
- import AuthSection from './sections/AuthSection';
192
-
193
- const sections = [
194
- HomeSection,
195
- AuthSection,
196
- ];
197
-
198
- const app = new App({
199
- token: process.env.BOT_TOKEN!,
200
- sections: sections,
201
- database: {
202
- path: './database/database.sqlite'
203
- }
204
- });
205
-
206
- app.launch();
207
- ```
208
-
209
- ## Environment Variables
210
-
211
- Create a `.env` file based on `.env.example`:
212
-
213
- ```env
214
- BOT_TOKEN=your_bot_token_from_botfather
215
- DATABASE_PATH=./database/database.sqlite
216
- LOG_LEVEL=info
217
- ```
218
-
219
- ## Advanced Usage
220
-
221
- ### Custom Migration Path
222
-
223
- ```typescript
224
- import { BotMigration } from '2bytetgbot';
225
-
226
- const migration = new BotMigration({
227
- botPath: __dirname,
228
- migrationsPath: './custom/migrations',
229
- databasePath: './custom/database.sqlite'
230
- });
231
- ```
232
-
233
- ### Custom Seeder
234
-
235
- ```typescript
236
- import { BotSeeder } from '2bytetgbot';
237
- import { seedUsers } from './seeds/users';
238
- import { seedProducts } from './seeds/products';
239
-
240
- const seeder = new BotSeeder({
241
- botPath: __dirname,
242
- databasePath: './database/database.sqlite',
243
- seeders: [seedUsers, seedProducts]
244
- });
245
- ```
246
-
247
- ### Custom Artisan Commands
248
-
249
- ```typescript
250
- import { BotArtisan } from '2bytetgbot';
251
-
252
- const artisan = new BotArtisan(__dirname, {
253
- botName: 'MyBot',
254
- sectionsPath: './src/sections'
255
- });
256
- ```
257
-
258
- ## Examples
259
-
260
- Check out example bots:
261
-
262
- - **Reward Bot** - User rewards and social media integration
263
- - **Registration Bot** - Telegram account management and automation
264
-
265
- ## Development
266
-
267
- ### Building the Library
268
-
269
- ```bash
270
- cd lib/
271
- bun install
272
- bun run build
273
- ```
274
-
275
- ### Local Testing
276
-
277
- ```bash
278
- bun run publish:local # Install globally for testing
279
- ```
280
-
281
- ## Contributing
282
-
283
- 1. Fork the repository
284
- 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
285
- 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
286
- 4. Push to the branch (`git push origin feature/amazing-feature`)
287
- 5. Open a Pull Request
288
-
289
- ## License
290
-
291
- MIT License - see [LICENSE](LICENSE) file for details.
292
-
293
- ## Support
294
-
295
- - 📚 [Documentation](https://github.com/your-org/2bytetgbot/wiki)
296
- - 🐛 [Issue Tracker](https://github.com/your-org/2bytetgbot/issues)
297
- - 💬 [Discussions](https://github.com/your-org/2bytetgbot/discussions)
298
-
299
- ---
300
-
1
+ # @2byte/tgbot-framework
2
+
3
+ 🚀 TypeScript framework для создания Telegram ботов с sections-based архитектурой, оптимизированный для **Bun**.
4
+
5
+ ## 🎯 Концепция
6
+
7
+ Это **npm библиотека** которая предоставляет:
8
+ - 🏗️ **Framework** для создания ботов
9
+ - 🛠️ **CLI инструменты** для генерации кода
10
+ - � **Готовые компоненты** (Section, Migration, Artisan)
11
+ - 🎨 **TypeScript типы** из коробки
12
+
13
+ ## 📦 Установка
14
+
15
+ ```bash
16
+ # Глобальная установка для CLI
17
+ bun add -g @2byte/tgbot-framework
18
+
19
+ # Или локальная установка в проект
20
+ bun add @2byte/tgbot-framework
21
+ ```
22
+
23
+ ## 🚀 Быстрый старт
24
+
25
+ ### 1. Создание нового бота
26
+
27
+ ```bash
28
+ # Создать новый бот с помощью CLI
29
+ 2byte-bot create my-awesome-bot
30
+ cd my-awesome-bot
31
+
32
+ # Настроить окружение
33
+ cp .env.example .env
34
+ # Добавить BOT_TOKEN в .env
35
+
36
+ # Установить зависимости и запустить
37
+ bun install
38
+ bun run dev
39
+ ```
40
+
41
+ ### 2. Использование в существующем проекте
42
+
43
+ ```bash
44
+ # Установить фреймворк
45
+ bun add @2byte/tgbot-framework
46
+
47
+ # Инициализировать структуру
48
+ 2byte-bot init
49
+ ```
50
+
51
+ ## CLI Commands
52
+
53
+ ### Bot Management
54
+
55
+ ```bash
56
+ 2byte create-bot <name> # Create a new bot
57
+ 2byte init # Initialize 2byte bot in current directory
58
+ ```
59
+
60
+ ### Code Generation
61
+
62
+ ```bash
63
+ 2byte generate section <name> # Generate a new section
64
+ 2byte generate migration <name> # Generate a new migration
65
+ ```
66
+
67
+ ### Project Commands (inside bot directory)
68
+
69
+ ```bash
70
+ bun run artisan make:section <name> # Create new section
71
+ bun run artisan add:method <section> <method> # Add method to section
72
+ bun run artisan list:sections # List all sections
73
+
74
+ bun run migrate # Run migrations
75
+ bun run seed # Seed database
76
+ bun run seed:clear # Clear and reseed database
77
+ bun run seed:clean # Clean database only
78
+ ```
79
+
80
+ ## Project Structure
81
+
82
+ When you create a new bot, you'll get this structure:
83
+
84
+ ```
85
+ my-awesome-bot/
86
+ ├── bot.ts # Main bot entry point
87
+ ├── artisan.ts # Artisan CLI for this bot
88
+ ├── sections/ # Bot sections
89
+ │ └── HomeSection.ts # Default home section
90
+ ├── database/
91
+ │ ├── migrate.ts # Migration runner
92
+ │ ├── seed.ts # Database seeder
93
+ │ ├── migrations/ # Migration files
94
+ │ └── database.sqlite # SQLite database
95
+ ├── package.json
96
+ └── .env.example
97
+ ```
98
+
99
+ ## Creating Sections
100
+
101
+ Sections are the main building blocks of your bot. Each section handles specific functionality:
102
+
103
+ ```typescript
104
+ import { Section, SectionOptions, InlineKeyboard } from '2bytetgbot';
105
+
106
+ export default class AuthSection extends Section {
107
+ static command = "auth";
108
+ static description = "Authentication section";
109
+ static actionRoutes = {
110
+ "auth.login": "login",
111
+ "auth.register": "register",
112
+ };
113
+
114
+ public sectionId = "auth";
115
+
116
+ constructor(options: SectionOptions) {
117
+ super(options);
118
+ }
119
+
120
+ async login() {
121
+ const message = "Please enter your credentials...";
122
+ await this.message(message).send();
123
+ }
124
+
125
+ async register() {
126
+ const message = "Registration form...";
127
+ await this.message(message).send();
128
+ }
129
+ }
130
+ ```
131
+
132
+ ## Database Migrations
133
+
134
+ Create database tables and modify schema using migrations:
135
+
136
+ ```bash
137
+ bun run artisan generate migration create_users_table
138
+ ```
139
+
140
+ This creates a migration file like `001_create_users_table.sql`:
141
+
142
+ ```sql
143
+ -- UP
144
+ CREATE TABLE IF NOT EXISTS users (
145
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
146
+ username TEXT NOT NULL UNIQUE,
147
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP
148
+ );
149
+
150
+ -- DOWN
151
+ DROP TABLE IF EXISTS users;
152
+ ```
153
+
154
+ Run migrations:
155
+
156
+ ```bash
157
+ bun run migrate up # Run all pending migrations
158
+ bun run migrate down 2 # Rollback last 2 migrations
159
+ bun run migrate status # Show migration status
160
+ ```
161
+
162
+ ## Database Seeding
163
+
164
+ Populate your database with test data:
165
+
166
+ ```typescript
167
+ import { Database } from 'bun:sqlite';
168
+
169
+ export async function seedUsers(db: Database) {
170
+ const stmt = db.prepare(`
171
+ INSERT INTO users (username) VALUES (?)
172
+ `);
173
+
174
+ const users = ['alice', 'bob', 'charlie'];
175
+
176
+ for (const username of users) {
177
+ stmt.run(username);
178
+ console.log(`✅ Created user: ${username}`);
179
+ }
180
+ }
181
+ ```
182
+
183
+ ## Bot Configuration
184
+
185
+ Configure your bot in `bot.ts`:
186
+
187
+ ```typescript
188
+ import 'dotenv/config';
189
+ import { App } from '2bytetgbot';
190
+ import HomeSection from './sections/HomeSection';
191
+ import AuthSection from './sections/AuthSection';
192
+
193
+ const sections = [
194
+ HomeSection,
195
+ AuthSection,
196
+ ];
197
+
198
+ const app = new App({
199
+ token: process.env.BOT_TOKEN!,
200
+ sections: sections,
201
+ database: {
202
+ path: './database/database.sqlite'
203
+ }
204
+ });
205
+
206
+ app.launch();
207
+ ```
208
+
209
+ ## Environment Variables
210
+
211
+ Create a `.env` file based on `.env.example`:
212
+
213
+ ```env
214
+ BOT_TOKEN=your_bot_token_from_botfather
215
+ DATABASE_PATH=./database/database.sqlite
216
+ LOG_LEVEL=info
217
+ ```
218
+
219
+ ## Advanced Usage
220
+
221
+ ### Custom Migration Path
222
+
223
+ ```typescript
224
+ import { BotMigration } from '2bytetgbot';
225
+
226
+ const migration = new BotMigration({
227
+ botPath: __dirname,
228
+ migrationsPath: './custom/migrations',
229
+ databasePath: './custom/database.sqlite'
230
+ });
231
+ ```
232
+
233
+ ### Custom Seeder
234
+
235
+ ```typescript
236
+ import { BotSeeder } from '2bytetgbot';
237
+ import { seedUsers } from './seeds/users';
238
+ import { seedProducts } from './seeds/products';
239
+
240
+ const seeder = new BotSeeder({
241
+ botPath: __dirname,
242
+ databasePath: './database/database.sqlite',
243
+ seeders: [seedUsers, seedProducts]
244
+ });
245
+ ```
246
+
247
+ ### Custom Artisan Commands
248
+
249
+ ```typescript
250
+ import { BotArtisan } from '2bytetgbot';
251
+
252
+ const artisan = new BotArtisan(__dirname, {
253
+ botName: 'MyBot',
254
+ sectionsPath: './src/sections'
255
+ });
256
+ ```
257
+
258
+ ## Examples
259
+
260
+ Check out example bots:
261
+
262
+ - **Reward Bot** - User rewards and social media integration
263
+ - **Registration Bot** - Telegram account management and automation
264
+
265
+ ## Development
266
+
267
+ ### Building the Library
268
+
269
+ ```bash
270
+ cd lib/
271
+ bun install
272
+ bun run build
273
+ ```
274
+
275
+ ### Local Testing
276
+
277
+ ```bash
278
+ bun run publish:local # Install globally for testing
279
+ ```
280
+
281
+ ## Contributing
282
+
283
+ 1. Fork the repository
284
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
285
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
286
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
287
+ 5. Open a Pull Request
288
+
289
+ ## License
290
+
291
+ MIT License - see [LICENSE](LICENSE) file for details.
292
+
293
+ ## Support
294
+
295
+ - 📚 [Documentation](https://github.com/your-org/2bytetgbot/wiki)
296
+ - 🐛 [Issue Tracker](https://github.com/your-org/2bytetgbot/issues)
297
+ - 💬 [Discussions](https://github.com/your-org/2bytetgbot/discussions)
298
+
299
+ ---
300
+
301
301
  Made with ❤️ by 2byte Team