Skip to content

Commit 3581c7c

Browse files
committed
refactor: streamline Telegram bot event handling and integrate logging
- Removed unused HttpModule and CqrsModule imports from telegram-bot.module.ts. - Updated NodesEvents and UsersEvents classes to replace direct bot message sending with a logging queue service for better message handling. - Refactored webhook event processing to utilize a dedicated logging service, enhancing maintainability and separation of concerns. - Introduced new queue names for Telegram and webhook logging in queue.enum.ts. - Adjusted job intervals for user traffic resets in intervals.ts for improved scheduling accuracy.
1 parent e63c5e1 commit 3581c7c

File tree

36 files changed

+493
-128
lines changed

36 files changed

+493
-128
lines changed

src/integration-modules/telegram-bot/events/nodes/nodes.events.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { InjectBot } from '@kastov/grammy-nestjs';
2-
import { parseMode } from '@grammyjs/parse-mode';
3-
import { Bot, Context } from 'grammy';
2+
import { Context } from 'grammy';
3+
import { Bot } from 'grammy';
44
import dayjs from 'dayjs';
55

66
import { OnEvent } from '@nestjs/event-emitter';
@@ -9,20 +9,24 @@ import { ConfigService } from '@nestjs/config';
99
import { prettyBytesUtil } from '@common/utils/bytes';
1010
import { EVENTS } from '@libs/contracts/constants';
1111

12-
import { BOT_NAME } from '../../constants';
12+
import { BOT_NAME } from '@integration-modules/webhook-module/constants/bot-name.constant';
13+
14+
import { TelegramBotLoggerQueueService } from '@queue/loggers/telegram-bot-logger';
15+
1316
import { NodeEvent } from './interfaces';
1417

1518
export class NodesEvents {
1619
private readonly adminId: string;
1720
private readonly notifyChatId: string;
1821
constructor(
1922
@InjectBot(BOT_NAME)
20-
private readonly bot: Bot<Context>,
23+
private readonly _: Bot<Context>,
24+
25+
private readonly telegramBotLoggerQueueService: TelegramBotLoggerQueueService,
2126
private readonly configService: ConfigService,
2227
) {
2328
this.adminId = this.configService.getOrThrow<string>('TELEGRAM_ADMIN_ID');
2429
this.notifyChatId = this.configService.getOrThrow<string>('NODES_NOTIFY_CHAT_ID');
25-
this.bot.api.config.use(parseMode('html'));
2630
}
2731

2832
@OnEvent(EVENTS.NODE.CREATED)
@@ -34,7 +38,10 @@ export class NodesEvents {
3438
<b>Address:</b> <code>${event.node.address}</code>
3539
<b>Port:</b> <code>${event.node.port}</code>
3640
`;
37-
await this.bot.api.sendMessage(this.adminId, msg);
41+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
42+
message: msg,
43+
chatId: this.adminId,
44+
});
3845
}
3946

4047
@OnEvent(EVENTS.NODE.MODIFIED)
@@ -46,7 +53,10 @@ export class NodesEvents {
4653
<b>Address:</b> <code>${event.node.address}</code>
4754
<b>Port:</b> <code>${event.node.port}</code>
4855
`;
49-
await this.bot.api.sendMessage(this.adminId, msg);
56+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
57+
message: msg,
58+
chatId: this.adminId,
59+
});
5060
}
5161

5262
@OnEvent(EVENTS.NODE.DISABLED)
@@ -58,19 +68,25 @@ export class NodesEvents {
5868
<b>Address:</b> <code>${event.node.address}</code>
5969
<b>Port:</b> <code>${event.node.port}</code>
6070
`;
61-
await this.bot.api.sendMessage(this.adminId, msg);
71+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
72+
message: msg,
73+
chatId: this.adminId,
74+
});
6275
}
6376

6477
@OnEvent(EVENTS.NODE.ENABLED)
6578
async onNodeEnabled(event: NodeEvent): Promise<void> {
6679
const msg = `
67-
🟢 <b>#nodeEnabled</b>
80+
🟩 <b>#nodeEnabled</b>
6881
➖➖➖➖➖➖➖➖➖
6982
<b>Name:</b> <code>${event.node.name}</code>
7083
<b>Address:</b> <code>${event.node.address}</code>
7184
<b>Port:</b> <code>${event.node.port}</code>
7285
`;
73-
await this.bot.api.sendMessage(this.adminId, msg);
86+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
87+
message: msg,
88+
chatId: this.adminId,
89+
});
7490
}
7591

7692
@OnEvent(EVENTS.NODE.CONNECTION_LOST)
@@ -84,7 +100,10 @@ export class NodesEvents {
84100
<b>Last status change:</b> <code>${dayjs(event.node.lastStatusChange).format('DD.MM.YYYY HH:mm')}</code>
85101
<b>Address:</b> <code>${event.node.address}:${event.node.port}</code>
86102
`;
87-
await this.bot.api.sendMessage(this.adminId, msg);
103+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
104+
message: msg,
105+
chatId: this.adminId,
106+
});
88107
}
89108

90109
@OnEvent(EVENTS.NODE.CONNECTION_RESTORED)
@@ -98,7 +117,10 @@ export class NodesEvents {
98117
<b>Last status change:</b> <code>${dayjs(event.node.lastStatusChange).format('DD.MM.YYYY HH:mm')}</code>
99118
<b>Address:</b> <code>${event.node.address}:${event.node.port}</code>
100119
`;
101-
await this.bot.api.sendMessage(this.adminId, msg);
120+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
121+
message: msg,
122+
chatId: this.adminId,
123+
});
102124
}
103125

104126
@OnEvent(EVENTS.NODE.TRAFFIC_NOTIFY)
@@ -116,6 +138,9 @@ export class NodesEvents {
116138
<b>Traffic reset day:</b> <code>${event.node.trafficResetDay}</code>
117139
<b>Percent:</b> <code>${event.node.notifyPercent} %</code>
118140
`;
119-
await this.bot.api.sendMessage(this.notifyChatId, msg);
141+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
142+
message: msg,
143+
chatId: this.notifyChatId,
144+
});
120145
}
121146
}
Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { InjectBot } from '@kastov/grammy-nestjs';
2-
import { parseMode } from '@grammyjs/parse-mode';
3-
import { Bot, Context } from 'grammy';
2+
import { Context } from 'grammy';
3+
import { Bot } from 'grammy';
44
import dayjs from 'dayjs';
55

66
import { OnEvent } from '@nestjs/event-emitter';
@@ -9,19 +9,23 @@ import { ConfigService } from '@nestjs/config';
99
import { prettyBytesUtil } from '@common/utils/bytes';
1010
import { EVENTS } from '@libs/contracts/constants';
1111

12-
import { BOT_NAME } from '../../constants';
12+
import { BOT_NAME } from '@integration-modules/telegram-bot/constants';
13+
14+
import { TelegramBotLoggerQueueService } from '@queue/loggers/telegram-bot-logger';
15+
1316
import { UserEvent } from './interfaces';
1417

1518
export class UsersEvents {
1619
private readonly adminId: string;
1720

1821
constructor(
1922
@InjectBot(BOT_NAME)
20-
private readonly bot: Bot<Context>,
23+
private readonly _: Bot<Context>,
24+
25+
private readonly telegramBotLoggerQueueService: TelegramBotLoggerQueueService,
2126
private readonly configService: ConfigService,
2227
) {
2328
this.adminId = this.configService.getOrThrow<string>('TELEGRAM_ADMIN_ID');
24-
this.bot.api.config.use(parseMode('html'));
2529
}
2630

2731
@OnEvent(EVENTS.USER.CREATED)
@@ -31,11 +35,14 @@ export class UsersEvents {
3135
➖➖➖➖➖➖➖➖➖
3236
<b>Username:</b> <code>${event.user.username}</code>
3337
<b>Traffic limit:</b> <code>${prettyBytesUtil(event.user.trafficLimitBytes)}</code>
34-
<b>Expired at:</b> <code>${dayjs(event.user.expireAt).format('DD.MM.YYYY HH:mm')}</code>
38+
<b>Valid until:</b> <code>${dayjs(event.user.expireAt).format('DD.MM.YYYY HH:mm')}</code>
3539
<b>Sub:</b> <code>${event.user.shortUuid}</code>
3640
<b>Inbounds:</b> <code>${event.user.activeUserInbounds.map((inbound) => inbound.tag).join(', ')}</code>
3741
`;
38-
await this.bot.api.sendMessage(this.adminId, msg);
42+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
43+
message: msg,
44+
chatId: this.adminId,
45+
});
3946
}
4047

4148
@OnEvent(EVENTS.USER.MODIFIED)
@@ -45,11 +52,14 @@ export class UsersEvents {
4552
➖➖➖➖➖➖➖➖➖
4653
<b>Username:</b> <code>${event.user.username}</code>
4754
<b>Traffic limit:</b> <code>${prettyBytesUtil(event.user.trafficLimitBytes)}</code>
48-
<b>Expired at:</b> <code>${dayjs(event.user.expireAt).format('DD.MM.YYYY HH:mm')}</code>
55+
<b>Valid until:</b> <code>${dayjs(event.user.expireAt).format('DD.MM.YYYY HH:mm')}</code>
4956
<b>Sub:</b> <code>${event.user.shortUuid}</code>
5057
<b>Inbounds:</b> <code>${event.user.activeUserInbounds.map((inbound) => inbound.tag).join(', ')}</code>
5158
`;
52-
await this.bot.api.sendMessage(this.adminId, msg);
59+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
60+
message: msg,
61+
chatId: this.adminId,
62+
});
5363
}
5464

5565
@OnEvent(EVENTS.USER.REVOKED)
@@ -59,21 +69,27 @@ export class UsersEvents {
5969
➖➖➖➖➖➖➖➖➖
6070
<b>Username:</b> <code>${event.user.username}</code>
6171
<b>Traffic limit:</b> <code>${prettyBytesUtil(event.user.trafficLimitBytes)}</code>
62-
<b>Expired at:</b> <code>${dayjs(event.user.expireAt).format('DD.MM.YYYY HH:mm')}</code>
72+
<b>Valid until:</b> <code>${dayjs(event.user.expireAt).format('DD.MM.YYYY HH:mm')}</code>
6373
<b>Sub:</b> <code>${event.user.shortUuid}</code>
6474
<b>Inbounds:</b> <code>${event.user.activeUserInbounds.map((inbound) => inbound.tag).join(', ')}</code>
6575
`;
66-
await this.bot.api.sendMessage(this.adminId, msg);
76+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
77+
message: msg,
78+
chatId: this.adminId,
79+
});
6780
}
6881

6982
@OnEvent(EVENTS.USER.DELETED)
7083
async onUserDeleted(event: UserEvent): Promise<void> {
7184
const msg = `
72-
🔴 <b>#deleted</b>
85+
🗑️ <b>#deleted</b>
7386
➖➖➖➖➖➖➖➖➖
7487
<b>Username:</b> <code>${event.user.username}</code>
7588
`;
76-
await this.bot.api.sendMessage(this.adminId, msg);
89+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
90+
message: msg,
91+
chatId: this.adminId,
92+
});
7793
}
7894

7995
@OnEvent(EVENTS.USER.DISABLED)
@@ -83,7 +99,10 @@ export class UsersEvents {
8399
➖➖➖➖➖➖➖➖➖
84100
<b>Username:</b> <code>${event.user.username}</code>
85101
`;
86-
await this.bot.api.sendMessage(this.adminId, msg);
102+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
103+
message: msg,
104+
chatId: this.adminId,
105+
});
87106
}
88107

89108
@OnEvent(EVENTS.USER.ENABLED)
@@ -93,7 +112,10 @@ export class UsersEvents {
93112
➖➖➖➖➖➖➖➖➖
94113
<b>Username:</b> <code>${event.user.username}</code>
95114
`;
96-
await this.bot.api.sendMessage(this.adminId, msg);
115+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
116+
message: msg,
117+
chatId: this.adminId,
118+
});
97119
}
98120

99121
@OnEvent(EVENTS.USER.LIMITED)
@@ -103,7 +125,10 @@ export class UsersEvents {
103125
➖➖➖➖➖➖➖➖➖
104126
<b>Username:</b> <code>${event.user.username}</code>
105127
`;
106-
await this.bot.api.sendMessage(this.adminId, msg);
128+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
129+
message: msg,
130+
chatId: this.adminId,
131+
});
107132
}
108133

109134
@OnEvent(EVENTS.USER.EXPIRED)
@@ -113,6 +138,9 @@ export class UsersEvents {
113138
➖➖➖➖➖➖➖➖➖
114139
<b>Username:</b> <code>${event.user.username}</code>
115140
`;
116-
await this.bot.api.sendMessage(this.adminId, msg);
141+
await this.telegramBotLoggerQueueService.addJobToSendTelegramMessage({
142+
message: msg,
143+
chatId: this.adminId,
144+
});
117145
}
118146
}

src/integration-modules/telegram-bot/telegram-bot.module.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { NestjsGrammyModule } from '@kastov/grammy-nestjs';
22

33
import { ConfigModule, ConfigService } from '@nestjs/config';
4-
import { HttpModule } from '@nestjs/axios';
5-
import { CqrsModule } from '@nestjs/cqrs';
64
import { Module } from '@nestjs/common';
75

86
import { BOT_NAME } from './constants/bot-name.constant';
@@ -11,8 +9,6 @@ import { TELEGRAM_BOT_EVENTS } from './events';
119

1210
@Module({
1311
imports: [
14-
HttpModule,
15-
CqrsModule,
1612
ConfigModule,
1713
NestjsGrammyModule.forRootAsync({
1814
imports: [ConfigModule],

0 commit comments

Comments
 (0)