Отображаются только доступные для роли команды
parent
0fe1d7e670
commit
a03a6abdfc
|
@ -1,19 +0,0 @@
|
||||||
package ru.ldeloff.servermonitorbot.model;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
import lombok.Data;
|
|
||||||
@Entity
|
|
||||||
@Data
|
|
||||||
@Table(name = "commands")
|
|
||||||
public class Command {
|
|
||||||
@Id
|
|
||||||
@Column(name = "id")
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
@Column(name = "name")
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@Column(name = "level")
|
|
||||||
private int level;
|
|
||||||
}
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.model;
|
||||||
|
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.CommandUtil;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CommandList {
|
||||||
|
private final Map<String, CommandTemplate> services;
|
||||||
|
@Getter
|
||||||
|
private HashMap<String, Long> commands;
|
||||||
|
@PostConstruct
|
||||||
|
private void setCommands() {
|
||||||
|
commands = new HashMap<>();
|
||||||
|
for (Map.Entry<String, CommandTemplate> entry : services.entrySet()) {
|
||||||
|
commands.put(entry.getValue().getName(), entry.getValue().getExpectedRole());
|
||||||
|
}
|
||||||
|
CommandUtil.setCommands(commands);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,13 +0,0 @@
|
||||||
package ru.ldeloff.servermonitorbot.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
import ru.ldeloff.servermonitorbot.model.Command;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface CommandRepository extends JpaRepository<Command, Long> {
|
|
||||||
Optional<Command> findCommandByName(String name);
|
|
||||||
Optional<Command> findCommandById(Long id);
|
|
||||||
}
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package ru.ldeloff.servermonitorbot.utils;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
public class CommandUtil {
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
private HashMap<String, Long> commands;
|
||||||
|
}
|
|
@ -6,48 +6,58 @@ import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
|
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
|
||||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
|
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
|
||||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
|
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
|
||||||
|
import ru.ldeloff.servermonitorbot.model.CommandList;
|
||||||
import ru.ldeloff.servermonitorbot.model.Role;
|
import ru.ldeloff.servermonitorbot.model.Role;
|
||||||
|
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||||
|
import ru.ldeloff.servermonitorbot.utils.CommandUtil;
|
||||||
import ru.ldeloff.servermonitorbot.utils.RoleChecker;
|
import ru.ldeloff.servermonitorbot.utils.RoleChecker;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class TelegramBotKeyboard implements UiFormer {
|
public class TelegramBotKeyboard implements UiFormer {
|
||||||
private final String STATUS = "Статус";
|
|
||||||
private final String UNAME = "uname";
|
|
||||||
private final String CPU_TEMP = "CPU.temp";
|
|
||||||
private final String HDD_TEMP = "HDD.temp";
|
|
||||||
private final String MDADM = "MDADM";
|
|
||||||
private final RoleChecker roleChecker;
|
private final RoleChecker roleChecker;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SendMessage uiForm(SendMessage message) {
|
public SendMessage uiForm(SendMessage message) {
|
||||||
|
|
||||||
Role role = roleChecker.getRole(message);
|
|
||||||
|
|
||||||
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
|
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
|
||||||
replyKeyboardMarkup.setResizeKeyboard(true);
|
replyKeyboardMarkup.setResizeKeyboard(true);
|
||||||
replyKeyboardMarkup.setOneTimeKeyboard(false);
|
replyKeyboardMarkup.setOneTimeKeyboard(false);
|
||||||
|
|
||||||
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
|
ArrayList<KeyboardRow> keyboardRows = formKeyboard(
|
||||||
|
roleChecker.getRole(message).getId());
|
||||||
KeyboardRow keyboardRow1 = new KeyboardRow();
|
|
||||||
keyboardRows.add(keyboardRow1);
|
|
||||||
|
|
||||||
keyboardRow1.add(new KeyboardButton(STATUS));
|
|
||||||
keyboardRow1.add(new KeyboardButton(UNAME));
|
|
||||||
keyboardRow1.add(new KeyboardButton(CPU_TEMP));
|
|
||||||
keyboardRow1.add(new KeyboardButton(HDD_TEMP));
|
|
||||||
|
|
||||||
KeyboardRow keyboardRow2 = new KeyboardRow();
|
|
||||||
keyboardRows.add(keyboardRow2);
|
|
||||||
keyboardRow2.add(new KeyboardButton(MDADM));
|
|
||||||
|
|
||||||
replyKeyboardMarkup.setKeyboard(keyboardRows);
|
replyKeyboardMarkup.setKeyboard(keyboardRows);
|
||||||
message.setReplyMarkup(replyKeyboardMarkup);
|
message.setReplyMarkup(replyKeyboardMarkup);
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ArrayList<KeyboardRow> formKeyboard(long role) {
|
||||||
|
HashMap<String, Long> commands = CommandUtil.getCommands();
|
||||||
|
if (role < 3) {
|
||||||
|
commands.remove("/start");
|
||||||
|
commands.remove("Not found");
|
||||||
|
}
|
||||||
|
int buttonOnLine = 3;
|
||||||
|
|
||||||
|
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
|
||||||
|
KeyboardRow keyboardRow = new KeyboardRow();
|
||||||
|
|
||||||
|
for (Map.Entry<String, Long> entry : commands.entrySet()) {
|
||||||
|
if (role <= entry.getValue()) {
|
||||||
|
keyboardRow.add(new KeyboardButton(entry.getKey()));
|
||||||
|
}
|
||||||
|
if (keyboardRow.size()>=buttonOnLine) {
|
||||||
|
keyboardRows.add(keyboardRow);
|
||||||
|
keyboardRow = new KeyboardRow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (keyboardRow.size() > 0) {
|
||||||
|
keyboardRows.add(keyboardRow);
|
||||||
|
}
|
||||||
|
return keyboardRows;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue