Skip to content

Commit faae8bc

Browse files
committed
update commands
1 parent 234455a commit faae8bc

2 files changed

Lines changed: 137 additions & 131 deletions

File tree

src/library/telegram/TelegramMonitor.cpp

Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "TelegramMonitor.hpp"
22

33
TelegramMonitor::TelegramMonitor(bool &isMonitoringEnable, CpuMonitor &cpu, MemoryMonitor &memory, const Settings settings, Log logger)
4-
: isMonitoringEnable(isMonitoringEnable), cpu(cpu), memory(memory), settings(settings), logger(logger), tgNotificationStatus(false) {}
4+
: isMonitoringEnable(isMonitoringEnable), cpu(cpu), memory(memory), settings(settings), logger(logger), tgNotificationStatus(false), bot(settings.getBotToken())
5+
{
6+
}
57

68
/**
79
* @brief Starts a new thread to handle Telegram bot requests.
@@ -83,159 +85,157 @@ void TelegramMonitor::startTelegramNotificationWatchThread()
8385
}
8486

8587
/**
86-
* @brief Handles incoming Telegram bot commands and interacts with users.
87-
*
88-
* This method initializes a Telegram bot and sets up event handlers for various bot commands.
89-
* The bot listens for commands such as /start, /stop, /usage, /help, and /status. Based on the
90-
* received command, it performs actions such as starting or stopping monitoring, sending server
91-
* usage statistics, and providing help or status information.
92-
*
93-
* The method operates as follows:
94-
*
95-
* 1. **Bot Initialization**: Creates an instance of `TgBot::Bot` using the bot token retrieved
96-
* from settings.
88+
* @brief Handles the /start command to initiate monitoring.
9789
*
98-
* 2. **Command Handlers**:
99-
* - **start**: Enables monitoring if the chat ID matches the expected ID, logs the event,
100-
* and sends a welcome message with a list of available commands. Updates the monitoring
101-
* status to "Enable".
102-
* - **stop**: Disables monitoring if the chat ID matches, logs the event, and sends a
103-
* message indicating that monitoring has stopped. Updates the monitoring status to "Disable".
104-
* - **usage**: Sends current server usage statistics (CPU and memory) if monitoring is enabled.
105-
* If monitoring is disabled, it sends a message indicating how to re-enable monitoring.
106-
* - **help**: Sends a message listing available commands to help the user interact with the bot.
107-
* - **status**: Sends the current monitoring status ("Enable" or "Disable") along with
108-
* instructions for starting or stopping monitoring.
109-
*
110-
* 3. **Bot Polling**: Sets up a `TgBot::TgLongPoll` object for long polling to receive updates
111-
* from the Telegram server. Enters an infinite loop, continuously calling `longPoll.start()`
112-
* to process incoming messages and commands.
113-
*
114-
* 4. **Error Handling**: Catches and logs any exceptions thrown by the Telegram Bot API, ensuring
115-
* that errors are reported for debugging purposes.
116-
*
117-
* **Note**: Ensure that the `settings.getChatId()` and `settings.getBotToken()` methods return
118-
* correct values for the bot to function properly. The bot must be properly configured in Telegram
119-
* and have the necessary permissions to interact with users.
90+
* Checks if the user ID matches the allowed chat ID, logs the start command,
91+
* enables monitoring, and sends a welcome message with available commands to the user.
12092
*
121-
* Example usage:
122-
* @code
123-
* TelegramMonitor monitor;
124-
*monitor.startTelegramBot();
125-
* // The bot will now be running and handling commands.
126-
*@endcode
127-
**/
128-
void TelegramMonitor::thread_telegramBot()
93+
* @param message Pointer to the incoming message containing the /start command.
94+
*/
95+
void TelegramMonitor::handleStartCommand(TgBot::Message::Ptr message)
12996
{
130-
TgBot::Bot bot(settings.getBotToken());
97+
if (message->chat->id != settings.getChatId())
98+
return;
13199

132-
// command Start
133-
bot.getEvents().onCommand("start", [&bot, this](TgBot::Message::Ptr message)
134-
{
135-
// check user id
136-
if (message->chat->id != settings.getChatId()) return;
100+
logger.logToConsole("send /start command, start monitoring");
101+
this->isMonitoringEnable = true;
137102

138-
// log event
139-
logger.logToConsole("send /start command,start monitoring");
103+
bot.getApi().sendMessage(message->chat->id,
104+
"Welcome to LinuxMonitoring\n"
105+
"\nCommands:\n"
106+
"/start start monitoring\n"
107+
"/stop stop monitoring\n"
108+
"/status monitoring status\n"
109+
"/usage get server status\n"
110+
"/help get bot command list\n"
111+
"\nMonitoring Status : Enable\n"
112+
"\nPowered By Mr.Mansouri");
113+
}
140114

141-
// enable monitoring
142-
this->isMonitoringEnable=true;
115+
/**
116+
* @brief Handles the /stop command to disable monitoring.
117+
*
118+
* Checks if the user ID matches the allowed chat ID, logs the stop command,
119+
* disables monitoring, and notifies the user that monitoring has been stopped.
120+
*
121+
* @param message Pointer to the incoming message containing the /stop command.
122+
*/
123+
void TelegramMonitor::handleStopCommand(TgBot::Message::Ptr message)
124+
{
125+
if (message->chat->id != settings.getChatId())
126+
return;
143127

144-
// send user message
145-
bot.getApi().sendMessage(message->chat->id, "Welcome to LinuxMonitoring\n"
146-
"\nCommands:\n"
147-
"/start start monitoring\n"
148-
"/stop stop monitoring\n"
149-
"/status monitoring status\n"
150-
"/usage get server status\n"
151-
"/help get bot command list\n"
152-
"\nMonitoring Status : Enable\n"
153-
"\nPowered By Mr.Mansouri"); });
128+
logger.logToConsole("send /stop command, stop monitoring");
129+
this->isMonitoringEnable = false;
154130

155-
// command Stop
156-
bot.getEvents().onCommand("stop", [&bot, this](TgBot::Message::Ptr message)
157-
{
158-
// check user id
159-
if (message->chat->id != settings.getChatId())return;
131+
bot.getApi().sendMessage(message->chat->id,
132+
"Monitoring Stopped!\n"
133+
"\nMonitoring Status : Disable\n"
134+
"\n- To re-enable monitoring, please enter the /start command.\n"
135+
"\n- To check monitoring status, please enter the /status command.\n");
136+
}
160137

161-
// log event
162-
logger.logToConsole("send /stop command,stop monitoring");
138+
/**
139+
* @brief Handles the /usage command to report server CPU and memory usage.
140+
*
141+
* Checks if the user ID matches the allowed chat ID and logs the usage command.
142+
* If monitoring is enabled, sends the current CPU and memory usage to the user.
143+
* If monitoring is disabled, informs the user that monitoring is inactive.
144+
*
145+
* @param message Pointer to the incoming message containing the /usage command.
146+
*/
147+
void TelegramMonitor::handleUsageCommand(TgBot::Message::Ptr message)
148+
{
149+
if (message->chat->id != settings.getChatId())
150+
return;
163151

164-
// disbale monitoring
165-
this->isMonitoringEnable=false;
152+
logger.logToConsole("send /usage command");
166153

167-
// send message
168-
bot.getApi().sendMessage(message->chat->id,
169-
"Monitoring Stopped!\n"
170-
"\nMonitoring Status : Disable\n"
171-
"\n- To re-enable monitoring, please enter the /start command.\n"
172-
"\n- To check monitoring status, please enter the /status command.\n"
173-
); });
154+
if (!this->isMonitoringEnable)
155+
{
156+
bot.getApi().sendMessage(message->chat->id,
157+
"Monitoring Status : Disable\n"
158+
"\nTo monitor the server again, please enter the /start command.");
159+
return;
160+
}
174161

175-
// command Usage
176-
bot.getEvents().onCommand("usage", [&bot, this](TgBot::Message::Ptr message)
177-
{
178-
// check user id
179-
if (message->chat->id != settings.getChatId()) return;
162+
bot.getApi().sendMessage(message->chat->id,
163+
"Server Usage :\n\n"
164+
"CPU : " +
165+
std::to_string(static_cast<int>(cpu.getLastCpuUsage())) +
166+
"%\nMemory : " + std::to_string(static_cast<int>(memory.getLastMemoryUsage())) + "%");
167+
}
180168

181-
// log event
182-
logger.logToConsole("send /usage command");
169+
/**
170+
* @brief Handles the /help command to display available bot commands.
171+
*
172+
* Checks if the user ID matches the allowed chat ID and sends a list of all available
173+
* bot commands to the user, explaining their usage.
174+
*
175+
* @param message Pointer to the incoming message containing the /help command.
176+
*/
177+
void TelegramMonitor::handleHelpCommand(TgBot::Message::Ptr message)
178+
{
179+
if (message->chat->id != settings.getChatId())
180+
return;
183181

184-
// if monitoring disable
185-
if(!this->isMonitoringEnable){
186-
bot.getApi().sendMessage(message->chat->id,
187-
"Monitoring Status : Disable\n"
188-
"\nTo monitor the server again, please enter the /start command."
189-
);
190-
return;
191-
}
182+
bot.getApi().sendMessage(message->chat->id,
183+
"Commands:\n\n"
184+
"/start start server monitoring\n"
185+
"/stop stop server monitoring\n"
186+
"/status get server monitoring status\n"
187+
"/usage get server usage\n");
188+
}
192189

193-
// send message
194-
bot.getApi().sendMessage(message->chat->id,
195-
"Server Usage :\n\n"
196-
"CPU : " + std::to_string((int)cpu.getLastCpuUsage()) +
197-
"%\nMemory : " + std::to_string((int)memory.getLastMemoryUsage()) + "%"
198-
); });
190+
/**
191+
* @brief Handles the /status command to report monitoring status.
192+
*
193+
* Checks if the user ID matches the allowed chat ID and sends the current
194+
* monitoring status (enabled or disabled) to the user.
195+
*
196+
* @param message Pointer to the incoming message containing the /status command.
197+
*/
198+
void TelegramMonitor::handleStatusCommand(TgBot::Message::Ptr message)
199+
{
200+
if (message->chat->id != settings.getChatId())
201+
return;
199202

200-
// command Help
201-
bot.getEvents().onCommand("help", [&bot, this](TgBot::Message::Ptr message)
202-
{
203-
// check user id
204-
if (message->chat->id != settings.getChatId()) return;
205-
206-
// send message
207-
bot.getApi().sendMessage(message->chat->id,
208-
"Commands:\n\n"
209-
"/start start server monitoring\n"
210-
"/stop stop server monitoring\n"
211-
"/status get server monitoring status\n"
212-
"/usage get server usage\n"
213-
); });
203+
std::string statusString = this->isMonitoringEnable ? "Enable" : "Disable";
204+
bot.getApi().sendMessage(message->chat->id,
205+
"Monitoring Status : " + statusString + "\n"
206+
"\n/start start server monitoring\n"
207+
"/stop stop server monitoring\n");
208+
}
214209

215-
// command Status
216-
bot.getEvents().onCommand("status", [&bot, this](TgBot::Message::Ptr message)
217-
{
218-
// check user id
219-
if (message->chat->id != settings.getChatId()) return;
220-
221-
// send message
222-
std::string statusString;
223-
if(this->isMonitoringEnable){
224-
statusString="Enable";
225-
}else{
226-
statusString="Disable";
227-
}
228-
bot.getApi().sendMessage(message->chat->id,
229-
"Monitoring Status : " + statusString + "\n"
230-
"\n/start start server monitoring\n"
231-
"/stop stop server monitoring\n"
232-
); });
210+
/**
211+
* @brief Initializes Telegram bot commands and enters a long polling loop.
212+
*
213+
* Registers handlers for various bot commands (/start, /stop, /usage, /help, /status)
214+
* by associating each command with its respective function. Then, starts a long polling
215+
* loop to keep the bot actively processing incoming messages and commands.
216+
*
217+
* This function also outputs bot and user details (username, chat ID, API token)
218+
* for debugging purposes.
219+
*/
220+
void TelegramMonitor::thread_telegramBot()
221+
{
222+
bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message)
223+
{ handleStartCommand(message); });
224+
bot.getEvents().onCommand("stop", [this](TgBot::Message::Ptr message)
225+
{ handleStopCommand(message); });
226+
bot.getEvents().onCommand("usage", [this](TgBot::Message::Ptr message)
227+
{ handleUsageCommand(message); });
228+
bot.getEvents().onCommand("help", [this](TgBot::Message::Ptr message)
229+
{ handleHelpCommand(message); });
230+
bot.getEvents().onCommand("status", [this](TgBot::Message::Ptr message)
231+
{ handleStatusCommand(message); });
233232

234233
try
235234
{
236235
std::cout << "Bot Username : " << bot.getApi().getMe()->username.c_str() << std::endl;
237236
std::cout << "User ChatID : " << settings.getChatId() << std::endl;
238237
std::cout << "Bot API Token : " << settings.getBotToken() << std::endl;
238+
239239
TgBot::TgLongPoll longPoll(bot);
240240
while (true)
241241
{

src/library/telegram/TelegramMonitor.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class TelegramMonitor
2222
private:
2323
void thread_telegramBot();
2424
void thread_telegramNotification();
25+
void handleStartCommand(TgBot::Message::Ptr message);
26+
void handleStopCommand(TgBot::Message::Ptr message);
27+
void handleUsageCommand(TgBot::Message::Ptr message);
28+
void handleHelpCommand(TgBot::Message::Ptr message);
29+
void handleStatusCommand(TgBot::Message::Ptr message);
2530

2631
Log logger;
2732
Settings settings;
@@ -31,4 +36,5 @@ class TelegramMonitor
3136
bool tgNotificationStatus;
3237
std::thread botRequestThread;
3338
std::thread notificationThread;
39+
TgBot::Bot bot;
3440
};

0 commit comments

Comments
 (0)