Применил паттерн Template
parent
99d06bb3bc
commit
32147b0e52
|
@ -4,18 +4,14 @@ import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.Role;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
||||||
import ru.ldeloff.servermonitorbot.model.User;
|
|
||||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||||
import ru.ldeloff.servermonitorbot.service.TelegramBotService;
|
import ru.ldeloff.servermonitorbot.service.command.FirstUseCommand;
|
||||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions;
|
||||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu;
|
||||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand;
|
||||||
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -24,115 +20,46 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
||||||
|
|
||||||
final TelegramBot telegramBot;
|
final TelegramBot telegramBot;
|
||||||
final SshRepository sshRepository;
|
final SshRepository sshRepository;
|
||||||
final TelegramBotService telegramBotService;
|
|
||||||
final UserService userService;
|
final FirstUseCommand firstUseCommand;
|
||||||
final RoleService roleService;
|
final GetStatusSessions getStatusSessions;
|
||||||
|
final UnameAggregateCommand unameAggregateCommand;
|
||||||
|
final SwitchToMainMenu switchToMainMenu;
|
||||||
|
final UnameCommand unameCommand;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpdateReceived(Update update) {
|
public void onUpdateReceived(Update update) {
|
||||||
Role role = getRole(update);
|
|
||||||
if (update.hasMessage()) {
|
if (update.hasMessage()) {
|
||||||
if (update.getMessage().hasText()) {
|
if (update.getMessage().hasText()) {
|
||||||
String messageText = update.getMessage().getText();
|
String messageText = update.getMessage().getText();
|
||||||
switch (messageText) {
|
switch (messageText) {
|
||||||
case "/start" -> {
|
case "/start" -> {
|
||||||
if (role.getId() < 3) {
|
firstUseCommand.execute(update, this);
|
||||||
log.info("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). OK");
|
|
||||||
telegramBotService.firstUse(update, this);
|
|
||||||
} else {
|
|
||||||
log.warn("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "Статус" -> {
|
case "Статус" -> {
|
||||||
if (role.getId() < 3) {
|
getStatusSessions.execute(update, this);
|
||||||
log.info("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). OK");
|
|
||||||
telegramBotService.getStatusSessions(update, this);
|
|
||||||
} else {
|
|
||||||
log.warn("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "uname" -> {
|
case "uname" -> {
|
||||||
if (role.getId() < 3) {
|
unameAggregateCommand.execute(update, this);
|
||||||
log.info("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). OK");
|
|
||||||
telegramBotService.sendUnameAggregate(update, this);
|
|
||||||
} else {
|
|
||||||
log.warn("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
default -> {
|
default -> {
|
||||||
if (role.getId() < 3) {
|
switchToMainMenu.execute(update, this);
|
||||||
log.warn("Неизвестная команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). ОК");
|
|
||||||
telegramBotService.switchToMainMenu(update, this);
|
|
||||||
} else {
|
|
||||||
log.warn("Неизвестная команда '" + messageText
|
|
||||||
+ "' от " + update.getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (update.hasCallbackQuery()) {
|
} else if (update.hasCallbackQuery()) {
|
||||||
String messageText = update.getCallbackQuery().getData();
|
String [] tags = update.getCallbackQuery().getData().split(":");
|
||||||
String [] tags = messageText.split(":");
|
|
||||||
if (tags.length > 1) {
|
if (tags.length > 1) {
|
||||||
switch (tags[0]) {
|
switch (tags[0]) {
|
||||||
case "uname":
|
case "uname" -> {
|
||||||
if (role.getId() < 3) {
|
unameCommand.execute(update, this);
|
||||||
log.info("Получена команда '" + messageText
|
}
|
||||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName()
|
default -> {
|
||||||
+ " (" + update.getCallbackQuery().getMessage().getChat().getId() + "). ОК");
|
switchToMainMenu.execute(update, this);
|
||||||
telegramBotService.uname(update, this);
|
}
|
||||||
} else {
|
|
||||||
log.info("Получена команда '" + messageText
|
|
||||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getCallbackQuery().getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (role.getId() < 3) {
|
|
||||||
log.warn("Неизвестная команда '" + messageText
|
|
||||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getCallbackQuery().getMessage().getChat().getId() + "). ОК");
|
|
||||||
telegramBotService.switchToMainMenu(update, this);
|
|
||||||
} else {
|
|
||||||
log.warn("Неизвестная команда '" + messageText
|
|
||||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getCallbackQuery().getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (role.getId() < 3) {
|
switchToMainMenu.execute(update, this);
|
||||||
log.warn("Неверный формат команды '" + messageText
|
|
||||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getCallbackQuery().getMessage().getChat().getId() + "). ОК");
|
|
||||||
telegramBotService.switchToMainMenu(update, this);
|
|
||||||
} else {
|
|
||||||
log.warn("Неверный формат команды '" + messageText
|
|
||||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName()
|
|
||||||
+ " (" + update.getCallbackQuery().getMessage().getChat().getId() + "). Нет прав");
|
|
||||||
telegramBotService.notAuthorization(update, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,19 +79,4 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
||||||
super.onClosing();
|
super.onClosing();
|
||||||
sshRepository.disconnectSessions();
|
sshRepository.disconnectSessions();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Role getRole(Update update) {
|
|
||||||
Long id = null;
|
|
||||||
if (update.hasMessage()) {
|
|
||||||
id = update.getMessage().getChat().getId();
|
|
||||||
} else if (update.hasCallbackQuery()) {
|
|
||||||
id = update.getCallbackQuery().getMessage().getChat().getId();
|
|
||||||
}
|
|
||||||
User user = userService.getByTelegramId(id);
|
|
||||||
if (user == null) {
|
|
||||||
return roleService.findRoleById(3L);
|
|
||||||
} else {
|
|
||||||
return user.getRole();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,4 +18,7 @@ public class User {
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "role_id")
|
@JoinColumn(name = "role_id")
|
||||||
private Role role;
|
private Role role;
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
private String login;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package ru.ldeloff.servermonitorbot.service;
|
|
||||||
|
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
|
||||||
|
|
||||||
public interface TelegramBotService {
|
|
||||||
|
|
||||||
void switchToMainMenu(Update update, TelegramBotController bot);
|
|
||||||
void getStatusSessions(Update update, TelegramBotController bot);
|
|
||||||
void sendUnameAggregate(Update update, TelegramBotController bot);
|
|
||||||
void uname(Update update, TelegramBotController bot);
|
|
||||||
void firstUse(Update update, TelegramBotController bot);
|
|
||||||
void notAuthorization(Update update, TelegramBotController bot);
|
|
||||||
}
|
|
|
@ -1,91 +0,0 @@
|
||||||
package ru.ldeloff.servermonitorbot.service;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
||||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
|
||||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
|
||||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
|
||||||
import ru.ldeloff.servermonitorbot.service.uname.UnameService;
|
|
||||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
|
||||||
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TelegramBotServiceImpl implements TelegramBotService {
|
|
||||||
final TelegramBotKeyboard telegramBotKeyboard;
|
|
||||||
final UnameChatButtonAggregate unameChatButtonAggregate;
|
|
||||||
final UnameService unameService;
|
|
||||||
final SshService sshService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void firstUse(Update update, TelegramBotController bot) {
|
|
||||||
SendMessage message = new SendMessage();
|
|
||||||
long chatId = 0L;
|
|
||||||
chatId = update.getMessage().getChatId();
|
|
||||||
message.setText("Добро пожаловать " + update.getMessage().getChat().getUserName() + "!");
|
|
||||||
message.setChatId(chatId);
|
|
||||||
sendMessage(telegramBotKeyboard.uiForm(message), bot);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void notAuthorization(Update update, TelegramBotController bot) {
|
|
||||||
long id = 0L;
|
|
||||||
String login = null;
|
|
||||||
long chatId = 0L;
|
|
||||||
if (update.hasMessage()) {
|
|
||||||
id = update.getMessage().getChat().getId();
|
|
||||||
login = update.getMessage().getChat().getUserName();
|
|
||||||
chatId = update.getMessage().getChatId();
|
|
||||||
} else if (update.hasCallbackQuery()) {
|
|
||||||
id = update.getCallbackQuery().getMessage().getChat().getId();
|
|
||||||
login = update.getCallbackQuery().getMessage().getChat().getUserName();
|
|
||||||
chatId = update.getCallbackQuery().getMessage().getChatId();
|
|
||||||
}
|
|
||||||
SendMessage message = new SendMessage();
|
|
||||||
message.setText("У пользователя " + login
|
|
||||||
+ " (" + id + ") недостаточно прав для выполнения этой команды");
|
|
||||||
message.setChatId(chatId);
|
|
||||||
sendMessage(message, bot);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void switchToMainMenu(Update update, TelegramBotController bot) {
|
|
||||||
SendMessage message = new SendMessage();
|
|
||||||
long chatId = 0L;
|
|
||||||
try {
|
|
||||||
chatId = update.getMessage().getChatId();
|
|
||||||
} catch (Exception e) {
|
|
||||||
chatId = update.getCallbackQuery().getMessage().getChatId();
|
|
||||||
}
|
|
||||||
message.setText("Неверная команда и или формат. Попробуем сначала.");
|
|
||||||
message.setChatId(chatId);
|
|
||||||
sendMessage(telegramBotKeyboard.uiForm(message), bot);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void getStatusSessions(Update update, TelegramBotController bot) {
|
|
||||||
sendMessage(sshService.getStatusSessions(update), bot);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void sendUnameAggregate(Update update, TelegramBotController bot) {
|
|
||||||
sendMessage(unameService.sendUnameAggregate(update), bot);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void uname(Update update, TelegramBotController bot) {
|
|
||||||
sendMessage(unameService.uname(update), bot);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendMessage(SendMessage message, TelegramBotController bot) {
|
|
||||||
try {
|
|
||||||
bot.execute(message);
|
|
||||||
} catch (TelegramApiException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
log.warn(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.service.command;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||||
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||||
|
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public abstract class CommandTemplate {
|
||||||
|
final UserService userService;
|
||||||
|
final RoleService roleService;
|
||||||
|
long expectedRole = 2L;
|
||||||
|
public abstract SendMessage actionForAuth(User user, SendMessage message);
|
||||||
|
|
||||||
|
public SendMessage actionForNotAuth(User user, SendMessage message) {
|
||||||
|
message.setText("У пользователя " + user.getLogin() + " (" + user.getTelegramId()
|
||||||
|
+ ") недостаточно прав для выполнения этой команды");
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
public void execute(Update update, TelegramBotController bot) {
|
||||||
|
User user = getUser(update);
|
||||||
|
String message = getMessage(update);
|
||||||
|
SendMessage answer = new SendMessage();
|
||||||
|
answer.setChatId(user.getTelegramId());
|
||||||
|
answer.setText(message);
|
||||||
|
if (user.getRole().getId() <= expectedRole) {
|
||||||
|
logSuccess(user, message);
|
||||||
|
sendMessage(actionForAuth(user, answer), bot);
|
||||||
|
} else {
|
||||||
|
logNotAuth(user, message);
|
||||||
|
sendMessage(actionForNotAuth(user, answer), bot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected void logSuccess(User user, String message) {
|
||||||
|
log.info("Получена команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). OK");
|
||||||
|
}
|
||||||
|
protected void logNotAuth(User user, String message) {
|
||||||
|
log.warn("Получена команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). Нет прав");
|
||||||
|
}
|
||||||
|
private User getUser(Update update) {
|
||||||
|
long id = -1L;
|
||||||
|
String login = null;
|
||||||
|
if (update.hasMessage()) {
|
||||||
|
id = update.getMessage().getChat().getId();
|
||||||
|
login = update.getMessage().getChat().getUserName();
|
||||||
|
} else if (update.hasCallbackQuery()) {
|
||||||
|
id = update.getCallbackQuery().getMessage().getChat().getId();
|
||||||
|
login = update.getCallbackQuery().getMessage().getChat().getUserName();
|
||||||
|
}
|
||||||
|
User user = userService.getByTelegramId(id);
|
||||||
|
if (user == null) {
|
||||||
|
user = new User();
|
||||||
|
user.setTelegramId(id);
|
||||||
|
user.setRole(roleService.findRoleById(3L));
|
||||||
|
}
|
||||||
|
user.setLogin(login);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getMessage(Update update) {
|
||||||
|
if (update.hasMessage()) {
|
||||||
|
return update.getMessage().getText();
|
||||||
|
} else if (update.hasCallbackQuery()) {
|
||||||
|
return update.getCallbackQuery().getData();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(SendMessage message, TelegramBotController bot) {
|
||||||
|
try {
|
||||||
|
bot.execute(message);
|
||||||
|
} catch (TelegramApiException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.service.command;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FirstUseCommand extends CommandTemplate {
|
||||||
|
final TelegramBotKeyboard telegramBotKeyboard;
|
||||||
|
|
||||||
|
public FirstUseCommand(UserService userService, RoleService roleService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
message.setText("Добро пожаловать " + user.getLogin() + "!");
|
||||||
|
return telegramBotKeyboard.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.service.command;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GetStatusSessions extends CommandTemplate {
|
||||||
|
final SshService sshService;
|
||||||
|
|
||||||
|
public GetStatusSessions(UserService userService, RoleService roleService, SshService sshService) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.sshService = sshService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return sshService.getStatusSessions(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.service.command;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class SwitchToMainMenu extends CommandTemplate {
|
||||||
|
final TelegramBotKeyboard telegramBotKeyboard;
|
||||||
|
|
||||||
|
public SwitchToMainMenu(UserService userService, RoleService roleService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
message.setText("Неверная команда. Попробуем сначала.");
|
||||||
|
return telegramBotKeyboard.uiForm(message);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void logSuccess(User user, String message) {
|
||||||
|
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). OK");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void logNotAuth(User user, String message) {
|
||||||
|
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||||
|
+ " (" + user.getTelegramId() + "). Нет прав");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.service.command.uname;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UnameAggregateCommand extends CommandTemplate {
|
||||||
|
final UnameChatButtonAggregate unameChatButtonAggregate;
|
||||||
|
|
||||||
|
public UnameAggregateCommand(UserService userService, RoleService roleService, UnameChatButtonAggregate unameChatButtonAggregate) {
|
||||||
|
super(userService, roleService);
|
||||||
|
this.unameChatButtonAggregate = unameChatButtonAggregate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
|
return unameChatButtonAggregate.uiForm(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,15 +1,16 @@
|
||||||
package ru.ldeloff.servermonitorbot.service.uname;
|
package ru.ldeloff.servermonitorbot.service.command.uname;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.Command;
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.User;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||||
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -17,46 +18,32 @@ import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class UnameServiceImpl implements UnameService {
|
public class UnameCommand extends CommandTemplate {
|
||||||
final SshService sshService;
|
final SshService sshService;
|
||||||
final UnameChatButtonAggregate unameChatButtonAggregate;
|
public UnameCommand(UserService userService, RoleService roleService, SshService sshService) {
|
||||||
@Override
|
super(userService, roleService);
|
||||||
public SendMessage sendUnameAggregate(Update update) {
|
this.sshService = sshService;
|
||||||
SendMessage message = new SendMessage();
|
|
||||||
long chatId = update.getMessage().getChatId();
|
|
||||||
message.setChatId(chatId);
|
|
||||||
return unameChatButtonAggregate.uiForm(message);
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public SendMessage uname(Update update) {
|
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||||
String messageText = update.getCallbackQuery().getData();
|
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||||
|
|
||||||
String [] tags = Arrays.stream(messageText.split(":"))
|
|
||||||
.map(String::trim)
|
.map(String::trim)
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
|
||||||
String result;
|
|
||||||
if (tags[1].equals("all")) {
|
if (tags[1].equals("all")) {
|
||||||
result = unameAllHost();
|
message.setText(unameAllHost());
|
||||||
} else {
|
} else {
|
||||||
result = unameSpecificHost(tags[1]);
|
message.setText(Objects.requireNonNull(unameSpecificHost(tags[1])));
|
||||||
}
|
}
|
||||||
SendMessage message = new SendMessage();
|
|
||||||
long chatId = update.getCallbackQuery().getMessage().getChatId();
|
|
||||||
message.setChatId(chatId);
|
|
||||||
message.setText(result);
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String unameAllHost() {
|
private String unameAllHost() {
|
||||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder response = new StringBuilder();
|
||||||
servers.forEach(server -> {
|
servers.forEach(server -> response.append(unameSpecificHost(server.getName())).append("\n"));
|
||||||
response.append(unameSpecificHost(server.getName())).append("\n");
|
return response.toString();
|
||||||
});
|
|
||||||
return response.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String unameSpecificHost(String serverName) {
|
private String unameSpecificHost(String serverName) {
|
|
@ -2,14 +2,14 @@ package ru.ldeloff.servermonitorbot.service.ssh;
|
||||||
|
|
||||||
|
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.Command;
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface SshService {
|
public interface SshService {
|
||||||
SendMessage getStatusSessions(Update update);
|
SendMessage getStatusSessions(SendMessage update);
|
||||||
|
|
||||||
List<SshServer> getSshServers();
|
List<SshServer> getSshServers();
|
||||||
Command execute(Command command);
|
Command execute(Command command);
|
||||||
List<Command> execute(List<Command> commands);
|
List<Command> execute(List<Command> commands);
|
||||||
|
|
|
@ -7,7 +7,6 @@ import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.Command;
|
import ru.ldeloff.servermonitorbot.model.Command;
|
||||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||||
|
@ -19,14 +18,11 @@ import java.util.List;
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SshServiceImpl implements SshService {
|
public class SshServiceImpl implements SshService {
|
||||||
final SshRepository sshRepository;
|
final SshRepository sshRepository;
|
||||||
@Override
|
@Override
|
||||||
public SendMessage getStatusSessions(Update update) {
|
public SendMessage getStatusSessions(SendMessage message) {
|
||||||
SendMessage message = new SendMessage();
|
|
||||||
long chatId = update.getMessage().getChatId();
|
|
||||||
message.setText(sshRepository.getStatusSessions());
|
message.setText(sshRepository.getStatusSessions());
|
||||||
message.setChatId(chatId);
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
package ru.ldeloff.servermonitorbot.service.uname;
|
|
||||||
|
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
||||||
|
|
||||||
public interface UnameService {
|
|
||||||
SendMessage sendUnameAggregate(Update update);
|
|
||||||
|
|
||||||
SendMessage uname(Update update);
|
|
||||||
}
|
|
Loading…
Reference in New Issue