@2byte/tgbot-framework 1.0.12 → 1.0.14
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 +300 -300
- package/bin/2byte-cli.ts +97 -97
- package/package.json +55 -55
- package/src/cli/CreateBotCommand.ts +181 -181
- package/src/cli/GenerateCommand.ts +195 -195
- package/src/cli/InitCommand.ts +107 -107
- package/src/cli/TgAccountManager.ts +50 -50
- package/src/console/migrate.ts +82 -82
- package/src/core/ApiService.ts +28 -20
- package/src/core/ApiServiceManager.ts +79 -63
- package/src/core/App.ts +1340 -1178
- package/src/core/BotArtisan.ts +79 -79
- package/src/core/BotMigration.ts +30 -30
- package/src/core/BotSeeder.ts +66 -66
- package/src/core/Model.ts +84 -84
- package/src/core/utils.ts +2 -2
- package/src/illumination/Artisan.ts +149 -149
- package/src/illumination/InlineKeyboard.ts +79 -79
- package/src/illumination/Message2Byte.ts +256 -256
- package/src/illumination/Message2ByteLiveProgressive.ts +278 -278
- package/src/illumination/Message2bytePool.ts +113 -113
- package/src/illumination/Migration.ts +186 -186
- package/src/illumination/RunSectionRoute.ts +109 -95
- package/src/illumination/Section.ts +429 -420
- package/src/illumination/SectionComponent.ts +64 -64
- package/src/illumination/Telegraf2byteContext.ts +32 -32
- package/src/index.ts +43 -42
- package/src/libs/TelegramAccountControl.ts +1140 -1140
- package/src/libs/TgSender.ts +53 -53
- package/src/models/Model.ts +67 -67
- package/src/models/Proxy.ts +217 -217
- package/src/models/TgAccount.ts +362 -362
- package/src/models/index.ts +2 -2
- package/src/types.ts +200 -191
- package/src/user/UserModel.ts +297 -297
- package/src/user/UserStore.ts +119 -119
- package/src/workflow/services/MassSendApiService.ts +94 -83
- package/templates/bot/.env.example +33 -33
- package/templates/bot/artisan.ts +8 -8
- package/templates/bot/bot.ts +82 -82
- package/templates/bot/database/dbConnector.ts +4 -4
- package/templates/bot/database/migrate.ts +9 -9
- package/templates/bot/database/migrations/001_create_users.sql +18 -18
- package/templates/bot/database/migrations/007_proxy.sql +27 -27
- package/templates/bot/database/migrations/008_tg_accounts.sql +32 -32
- package/templates/bot/database/seed.ts +14 -14
- package/templates/bot/docs/CLI_SERVICES.md +536 -536
- package/templates/bot/docs/INPUT_SYSTEM.md +211 -211
- package/templates/bot/docs/MASS_SEND_SERVICE.md +327 -327
- package/templates/bot/docs/SERVICE_EXAMPLES.md +384 -384
- package/templates/bot/docs/TASK_SYSTEM.md +156 -156
- package/templates/bot/models/Model.ts +8 -8
- package/templates/bot/models/index.ts +1 -1
- package/templates/bot/package.json +30 -30
- package/templates/bot/sectionList.ts +9 -9
- package/templates/bot/sections/ExampleInputSection.ts +85 -85
- package/templates/bot/sections/ExampleLiveTaskerSection.ts +60 -60
- package/templates/bot/sections/HomeSection.ts +63 -63
- package/templates/bot/tsconfig.json +16 -16
- package/templates/bot/workflow/services/ExampleService.ts +23 -23
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
TelegramManagerCredentialsDB,
|
|
3
|
-
TelegramAccountRemote,
|
|
4
|
-
} from "../libs/TelegramAccountControl";
|
|
5
|
-
import { Model } from "../models/Model";
|
|
6
|
-
import Input from "input";
|
|
7
|
-
|
|
8
|
-
export const manualAdderTgAccount = async () => {
|
|
9
|
-
const credentialsManager = new TelegramManagerCredentialsDB(Model.getConnection());
|
|
10
|
-
|
|
11
|
-
const tgAccountControl = TelegramAccountRemote.init({
|
|
12
|
-
appId: process.env.TG_APP_ID!,
|
|
13
|
-
appHash: process.env.TG_APP_HASH!,
|
|
14
|
-
credetialsManager: credentialsManager,
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const phone = await Input.text("Введите номер телефона (с кодом страны, например, 79614416445):");
|
|
18
|
-
|
|
19
|
-
await credentialsManager.addCredential({
|
|
20
|
-
phone,
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const credentials = await credentialsManager.getCredential(
|
|
24
|
-
phone
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
if (!credentials) {
|
|
28
|
-
console.log("Учётная запись с таким номером телефона не найдена.");
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
await tgAccountControl.login(
|
|
33
|
-
credentials,
|
|
34
|
-
async () => {
|
|
35
|
-
console.log("Требуется код подтверждения");
|
|
36
|
-
return await Input.text("Введите код подтверждения:");
|
|
37
|
-
},
|
|
38
|
-
async () => {
|
|
39
|
-
console.log("Требуется пароль");
|
|
40
|
-
return await Input.password("Введите пароль:");
|
|
41
|
-
},
|
|
42
|
-
async (err: any) => {
|
|
43
|
-
console.log("Ошибка логина:", err);
|
|
44
|
-
tgAccountControl.disconnect();
|
|
45
|
-
throw new Error(err);
|
|
46
|
-
}
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
tgAccountControl.disconnect();
|
|
50
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
TelegramManagerCredentialsDB,
|
|
3
|
+
TelegramAccountRemote,
|
|
4
|
+
} from "../libs/TelegramAccountControl";
|
|
5
|
+
import { Model } from "../models/Model";
|
|
6
|
+
import Input from "input";
|
|
7
|
+
|
|
8
|
+
export const manualAdderTgAccount = async () => {
|
|
9
|
+
const credentialsManager = new TelegramManagerCredentialsDB(Model.getConnection());
|
|
10
|
+
|
|
11
|
+
const tgAccountControl = TelegramAccountRemote.init({
|
|
12
|
+
appId: process.env.TG_APP_ID!,
|
|
13
|
+
appHash: process.env.TG_APP_HASH!,
|
|
14
|
+
credetialsManager: credentialsManager,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const phone = await Input.text("Введите номер телефона (с кодом страны, например, 79614416445):");
|
|
18
|
+
|
|
19
|
+
await credentialsManager.addCredential({
|
|
20
|
+
phone,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const credentials = await credentialsManager.getCredential(
|
|
24
|
+
phone
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
if (!credentials) {
|
|
28
|
+
console.log("Учётная запись с таким номером телефона не найдена.");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await tgAccountControl.login(
|
|
33
|
+
credentials,
|
|
34
|
+
async () => {
|
|
35
|
+
console.log("Требуется код подтверждения");
|
|
36
|
+
return await Input.text("Введите код подтверждения:");
|
|
37
|
+
},
|
|
38
|
+
async () => {
|
|
39
|
+
console.log("Требуется пароль");
|
|
40
|
+
return await Input.password("Введите пароль:");
|
|
41
|
+
},
|
|
42
|
+
async (err: any) => {
|
|
43
|
+
console.log("Ошибка логина:", err);
|
|
44
|
+
tgAccountControl.disconnect();
|
|
45
|
+
throw new Error(err);
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
tgAccountControl.disconnect();
|
|
50
|
+
};
|
package/src/console/migrate.ts
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import { Database } from 'bun:sqlite';
|
|
3
|
-
import { Migration } from '../illumination/Migration';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
|
|
6
|
-
type setupParams = {
|
|
7
|
-
pathDatabase?: string;
|
|
8
|
-
pathMigrations?: string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
async function main(params: setupParams = {}) {
|
|
12
|
-
|
|
13
|
-
const MIGRATIONS_PATH = params.pathMigrations || path.join(__dirname, 'migrations');
|
|
14
|
-
|
|
15
|
-
const command = process.argv[2];
|
|
16
|
-
const args = process.argv.slice(3);
|
|
17
|
-
|
|
18
|
-
if (!command) {
|
|
19
|
-
showHelp();
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const db = new Database(params.pathDatabase || __dirname + '/database/database.sqlite');
|
|
24
|
-
|
|
25
|
-
const migration = new Migration(db, MIGRATIONS_PATH);
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
switch (command) {
|
|
29
|
-
case 'create':
|
|
30
|
-
if (args.length === 0) {
|
|
31
|
-
console.error('❌ Ошибка: Требуется имя миграции');
|
|
32
|
-
console.log('Использование: migrate create migration_name');
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
await Migration.create(args[0], MIGRATIONS_PATH);
|
|
36
|
-
break;
|
|
37
|
-
|
|
38
|
-
case 'up':
|
|
39
|
-
await migration.up();
|
|
40
|
-
console.log('✅ Миграции выполнены');
|
|
41
|
-
break;
|
|
42
|
-
|
|
43
|
-
case 'down':
|
|
44
|
-
const steps = args[0] ? parseInt(args[0]) : 1;
|
|
45
|
-
await migration.down(steps);
|
|
46
|
-
console.log('✅ Откат миграций выполнен');
|
|
47
|
-
break;
|
|
48
|
-
|
|
49
|
-
case 'status':
|
|
50
|
-
migration.status();
|
|
51
|
-
break;
|
|
52
|
-
|
|
53
|
-
default:
|
|
54
|
-
console.error(`❌ Неизвестная команда: ${command}`);
|
|
55
|
-
showHelp();
|
|
56
|
-
}
|
|
57
|
-
} catch (error) {
|
|
58
|
-
console.error('❌ Ошибка:', error);
|
|
59
|
-
} finally {
|
|
60
|
-
db.close();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function showHelp() {
|
|
65
|
-
console.log(`
|
|
66
|
-
🗃️ Migration CLI для SQLite
|
|
67
|
-
|
|
68
|
-
Доступные команды:
|
|
69
|
-
create <name> Создать новую миграцию
|
|
70
|
-
up Выполнить все новые миграции
|
|
71
|
-
down [steps] Откатить последние миграции (по умолчанию 1)
|
|
72
|
-
status Показать статус миграций
|
|
73
|
-
|
|
74
|
-
Примеры:
|
|
75
|
-
migrate create create_users_table
|
|
76
|
-
migrate up
|
|
77
|
-
migrate down 2
|
|
78
|
-
migrate status
|
|
79
|
-
`);
|
|
80
|
-
}
|
|
81
|
-
export const setupMigrations = async (params: setupParams) => {
|
|
82
|
-
return main(params).catch(console.error);
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { Database } from 'bun:sqlite';
|
|
3
|
+
import { Migration } from '../illumination/Migration';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
type setupParams = {
|
|
7
|
+
pathDatabase?: string;
|
|
8
|
+
pathMigrations?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
async function main(params: setupParams = {}) {
|
|
12
|
+
|
|
13
|
+
const MIGRATIONS_PATH = params.pathMigrations || path.join(__dirname, 'migrations');
|
|
14
|
+
|
|
15
|
+
const command = process.argv[2];
|
|
16
|
+
const args = process.argv.slice(3);
|
|
17
|
+
|
|
18
|
+
if (!command) {
|
|
19
|
+
showHelp();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const db = new Database(params.pathDatabase || __dirname + '/database/database.sqlite');
|
|
24
|
+
|
|
25
|
+
const migration = new Migration(db, MIGRATIONS_PATH);
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
switch (command) {
|
|
29
|
+
case 'create':
|
|
30
|
+
if (args.length === 0) {
|
|
31
|
+
console.error('❌ Ошибка: Требуется имя миграции');
|
|
32
|
+
console.log('Использование: migrate create migration_name');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
await Migration.create(args[0], MIGRATIONS_PATH);
|
|
36
|
+
break;
|
|
37
|
+
|
|
38
|
+
case 'up':
|
|
39
|
+
await migration.up();
|
|
40
|
+
console.log('✅ Миграции выполнены');
|
|
41
|
+
break;
|
|
42
|
+
|
|
43
|
+
case 'down':
|
|
44
|
+
const steps = args[0] ? parseInt(args[0]) : 1;
|
|
45
|
+
await migration.down(steps);
|
|
46
|
+
console.log('✅ Откат миграций выполнен');
|
|
47
|
+
break;
|
|
48
|
+
|
|
49
|
+
case 'status':
|
|
50
|
+
migration.status();
|
|
51
|
+
break;
|
|
52
|
+
|
|
53
|
+
default:
|
|
54
|
+
console.error(`❌ Неизвестная команда: ${command}`);
|
|
55
|
+
showHelp();
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error('❌ Ошибка:', error);
|
|
59
|
+
} finally {
|
|
60
|
+
db.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function showHelp() {
|
|
65
|
+
console.log(`
|
|
66
|
+
🗃️ Migration CLI для SQLite
|
|
67
|
+
|
|
68
|
+
Доступные команды:
|
|
69
|
+
create <name> Создать новую миграцию
|
|
70
|
+
up Выполнить все новые миграции
|
|
71
|
+
down [steps] Откатить последние миграции (по умолчанию 1)
|
|
72
|
+
status Показать статус миграций
|
|
73
|
+
|
|
74
|
+
Примеры:
|
|
75
|
+
migrate create create_users_table
|
|
76
|
+
migrate up
|
|
77
|
+
migrate down 2
|
|
78
|
+
migrate status
|
|
79
|
+
`);
|
|
80
|
+
}
|
|
81
|
+
export const setupMigrations = async (params: setupParams) => {
|
|
82
|
+
return main(params).catch(console.error);
|
|
83
83
|
}
|
package/src/core/ApiService.ts
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
|
-
import { App } from "./App";
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import { App } from "./App";
|
|
2
|
+
|
|
3
|
+
export class ApiService<T = any> {
|
|
4
|
+
|
|
5
|
+
public name: string = "ApiService";
|
|
6
|
+
|
|
7
|
+
protected app!: App;
|
|
8
|
+
|
|
9
|
+
constructor(
|
|
10
|
+
public params: T = {} as T
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
async setup(): Promise<void> {
|
|
14
|
+
// Implement your API logic here
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async unsetup(): Promise<void> {
|
|
18
|
+
// Implement your API logic here
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async run(): Promise<void> {
|
|
22
|
+
// Implement your API logic here
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public setApp(app: App): this {
|
|
26
|
+
this.app = app;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
21
29
|
}
|
|
@@ -1,63 +1,79 @@
|
|
|
1
|
-
import { App } from "./App";
|
|
2
|
-
import { ApiService } from "./ApiService";
|
|
3
|
-
import { readdirSync } from "fs";
|
|
4
|
-
|
|
5
|
-
export class ApiServiceManager {
|
|
6
|
-
private services: Map<string, ApiService> = new Map();
|
|
7
|
-
|
|
8
|
-
constructor(private app: App) {}
|
|
9
|
-
|
|
10
|
-
static init(app: App): ApiServiceManager {
|
|
11
|
-
return new ApiServiceManager(app);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async loadServicesFromDirectory(pathDirectory: string): Promise<void> {
|
|
15
|
-
for (const entry of readdirSync(pathDirectory, { withFileTypes: true })) {
|
|
16
|
-
if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
17
|
-
const serviceModule = await import(`${pathDirectory}/${entry.name}`);
|
|
18
|
-
const ServiceClass = serviceModule.default;
|
|
19
|
-
const serviceInstance = new ServiceClass(this.app);
|
|
20
|
-
this.registerService(entry.name.replace(".ts", ""), serviceInstance);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public registerService(name: string, service: ApiService): void {
|
|
26
|
-
this.services.set(name, service);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public getService(name: string): ApiService | undefined {
|
|
30
|
-
return this.services.get(name);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public async setupService(name: string): Promise<void> {
|
|
34
|
-
const service = this.getService(name);
|
|
35
|
-
if (service) {
|
|
36
|
-
await service.setup();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public async unsetupService(name: string): Promise<void> {
|
|
41
|
-
const service = this.getService(name);
|
|
42
|
-
if (service) {
|
|
43
|
-
await service.unsetup();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public async runService(name: string): Promise<void> {
|
|
48
|
-
const service = this.getService(name);
|
|
49
|
-
if (service) {
|
|
50
|
-
await service.run();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
1
|
+
import { App } from "./App";
|
|
2
|
+
import { ApiService } from "./ApiService";
|
|
3
|
+
import { readdirSync } from "fs";
|
|
4
|
+
|
|
5
|
+
export class ApiServiceManager {
|
|
6
|
+
private services: Map<string, ApiService> = new Map();
|
|
7
|
+
|
|
8
|
+
constructor(private app: App) {}
|
|
9
|
+
|
|
10
|
+
static init(app: App): ApiServiceManager {
|
|
11
|
+
return new ApiServiceManager(app);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async loadServicesFromDirectory(pathDirectory: string): Promise<void> {
|
|
15
|
+
for (const entry of readdirSync(pathDirectory, { withFileTypes: true })) {
|
|
16
|
+
if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
17
|
+
const serviceModule = await import(`${pathDirectory}/${entry.name}`);
|
|
18
|
+
const ServiceClass = serviceModule.default;
|
|
19
|
+
const serviceInstance = new ServiceClass(this.app);
|
|
20
|
+
this.registerService(entry.name.replace(".ts", ""), serviceInstance);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public registerService(name: string, service: ApiService): void {
|
|
26
|
+
this.services.set(name, service);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public getService(name: string): ApiService | undefined {
|
|
30
|
+
return this.services.get(name);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async setupService(name: string): Promise<void> {
|
|
34
|
+
const service = this.getService(name);
|
|
35
|
+
if (service) {
|
|
36
|
+
await service.setup();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async unsetupService(name: string): Promise<void> {
|
|
41
|
+
const service = this.getService(name);
|
|
42
|
+
if (service) {
|
|
43
|
+
await service.unsetup();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public async runService(name: string): Promise<void> {
|
|
48
|
+
const service = this.getService(name);
|
|
49
|
+
if (service) {
|
|
50
|
+
await service.run();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async runAllServices(): Promise<void> {
|
|
55
|
+
for (const [name, service] of this.services) {
|
|
56
|
+
this.app.debugLog(`Running API service: ${name}`);
|
|
57
|
+
await service.run();
|
|
58
|
+
this.app.debugLog(`API service run completed: ${name}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public getAll(): Map<string, ApiService> {
|
|
63
|
+
return this.services;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async setupAllServices(): Promise<void> {
|
|
67
|
+
for (const [name, service] of this.services) {
|
|
68
|
+
this.app.debugLog(`Setting up API service: ${name}`);
|
|
69
|
+
await this.setupService(name);
|
|
70
|
+
this.app.debugLog(`API service setup completed: ${name}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public async unsetupAllServices(): Promise<void> {
|
|
75
|
+
for (const [name, service] of this.services) {
|
|
76
|
+
await this.unsetupService(name);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|