Добавлена кнопка uname, пока не полный функционал

feature/add-uname-command
L_DelOff 2023-08-06 00:55:44 +03:00
parent 26d9f83917
commit 7b18162d30
7 changed files with 114 additions and 77 deletions

View File

@ -9,7 +9,6 @@ import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import ru.ldeloff.servermonitorbot.model.TelegramBot;
import ru.ldeloff.servermonitorbot.service.TelegramBotService; import ru.ldeloff.servermonitorbot.service.TelegramBotService;
@Slf4j @Slf4j

View File

@ -7,10 +7,11 @@ import ru.ldeloff.servermonitorbot.model.SshServer;
import java.util.List; import java.util.List;
public interface SshService { public interface SshService {
void connectToAllServer(); void connectToAllServer();
Session connectToServer(SshServer sshServer) throws JSchException; Session connectToServer(SshServer sshServer) throws JSchException;
void disconnectSessions(); void disconnectSessions();
SendMessage getStatusSessions(Long chatId); SendMessage getStatusSessions(Long chatId);
List<SshServer> getSshServers();
} }

View File

@ -73,4 +73,9 @@ public class SshServiceImpl implements SshService{
message.setChatId(chatId); message.setChatId(chatId);
return message; return message;
} }
@Override
public List<SshServer> getSshServers() {
return sshServers;
}
} }

View File

@ -8,7 +8,8 @@ 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 org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.ldeloff.servermonitorbot.model.TelegramBot; import ru.ldeloff.servermonitorbot.model.TelegramBot;
import ru.ldeloff.servermonitorbot.util.TelegramBotMenu; import ru.ldeloff.servermonitorbot.service.ui.TelegramBotKeyboard;
import ru.ldeloff.servermonitorbot.service.ui.uname.UnameChatButton;
@Service @Service
@Slf4j @Slf4j
@ -16,14 +17,16 @@ public class TelegramBotService extends TelegramLongPollingBot {
final TelegramBot telegramBot; final TelegramBot telegramBot;
final SshService sshService; final SshService sshService;
final TelegramBotMenu telegramBotMenu; final TelegramBotKeyboard telegramBotKeyboard;
final UnameChatButton unameChatButton;
@Autowired @Autowired
public TelegramBotService(TelegramBot telegramBot, SshService sshService, TelegramBotMenu telegramBotMenu) { public TelegramBotService(TelegramBot telegramBot, SshService sshService, TelegramBotKeyboard telegramBotKeyboard, UnameChatButton unameChatButton) {
this.telegramBot = telegramBot; this.telegramBot = telegramBot;
this.sshService = sshService; this.sshService = sshService;
this.telegramBotMenu = telegramBotMenu; this.telegramBotKeyboard = telegramBotKeyboard;
this.unameChatButton = unameChatButton;
} }
@Override @Override
@ -37,7 +40,7 @@ public class TelegramBotService extends TelegramLongPollingBot {
case "/start": case "/start":
message.setText("Привет: " + update.getMessage().getChat().getUserName()); message.setText("Привет: " + update.getMessage().getChat().getUserName());
message.setChatId(chatId); message.setChatId(chatId);
message.setReplyMarkup(telegramBotMenu.initKeyboard()); message.setReplyMarkup(telegramBotKeyboard.initKeyboardButtons());
sendMessage(message); sendMessage(message);
log.info("Получена команда /start"); log.info("Получена команда /start");
break; break;
@ -45,23 +48,20 @@ public class TelegramBotService extends TelegramLongPollingBot {
sendMessage(sshService.getStatusSessions(chatId)); sendMessage(sshService.getStatusSessions(chatId));
log.info("Получена команда 'Статус'"); log.info("Получена команда 'Статус'");
break; break;
case "uname":
sendMessage(unameChatButton.initMenu(chatId));
log.info("Получена команда 'uname'");
break;
default: default:
message.setText("unknown command:" + messageText); message.setText("unknown command:" + messageText);
message.setChatId(chatId); message.setChatId(chatId);
message.setReplyMarkup(telegramBotMenu.initKeyboard()); message.setReplyMarkup(telegramBotKeyboard.initKeyboardButtons());
sendMessage(message); sendMessage(message);
log.warn("Неизвестная команда:" + messageText); log.warn("Неизвестная команда:" + messageText);
} }
} }
} else if (update.hasCallbackQuery()) { } else if (update.hasCallbackQuery()) {
try { System.out.println(update.getCallbackQuery().getData()); // TODO
SendMessage message = new SendMessage();
message.setText(update.getCallbackQuery().getData());
message.setChatId(update.getCallbackQuery().getMessage().getChatId());
execute(message);
} catch (TelegramApiException e) {
e.printStackTrace();
}
} }
} }

View File

@ -0,0 +1,39 @@
package ru.ldeloff.servermonitorbot.service.ui;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import java.util.ArrayList;
import java.util.List;
@Service
public class TelegramBotKeyboard {
private final String STATUS = "Статус";
private final String UNAME = "uname";
public ReplyKeyboardMarkup initKeyboardButtons()
{
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
replyKeyboardMarkup.setResizeKeyboard(true); //подгоняем размер
replyKeyboardMarkup.setOneTimeKeyboard(false); //скрываем после использования
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
KeyboardRow keyboardRow = new KeyboardRow();
keyboardRows.add(keyboardRow);
keyboardRow.add(new KeyboardButton(STATUS));
keyboardRow.add(new KeyboardButton(UNAME));
replyKeyboardMarkup.setKeyboard(keyboardRows);
return replyKeyboardMarkup;
}
}

View File

@ -0,0 +1,54 @@
package ru.ldeloff.servermonitorbot.service.ui.uname;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import ru.ldeloff.servermonitorbot.model.SshServer;
import ru.ldeloff.servermonitorbot.service.SshService;
import java.util.ArrayList;
import java.util.List;
@Service
public class UnameChatButton {
private final SshService sshService;
@Autowired
public UnameChatButton(SshService sshService) {
this.sshService = sshService;
}
public SendMessage initMenu(long chatId) {
SendMessage message = new SendMessage();
message.setChatId(chatId);
message.setText("Сервер:");
List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
sshService.getSshServers().forEach(sshServer -> {
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText(sshServer.getName());
inlineKeyboardButton.setCallbackData("uname: " + sshServer.getName());
keyboardRow.add(inlineKeyboardButton);
keyboard.add(keyboardRow);
});
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText("Все сервера");
inlineKeyboardButton.setCallbackData("uname: all");
keyboardRow.add(inlineKeyboardButton);
keyboard.add(keyboardRow);
inlineKeyboardMarkup.setKeyboard(keyboard);
message.setReplyMarkup(inlineKeyboardMarkup);
return message;
}
}

View File

@ -1,61 +0,0 @@
package ru.ldeloff.servermonitorbot.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class TelegramBotMenu {
private final String STATUS = "Статус";
public ReplyKeyboardMarkup initKeyboard()
{
//Создаем объект будущей клавиатуры и выставляем нужные настройки
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
replyKeyboardMarkup.setResizeKeyboard(true); //подгоняем размер
replyKeyboardMarkup.setOneTimeKeyboard(false); //скрываем после использования
//Создаем список с рядами кнопок
ArrayList<KeyboardRow> keyboardRows = new ArrayList<>();
//Создаем один ряд кнопок и добавляем его в список
KeyboardRow keyboardRow = new KeyboardRow();
keyboardRows.add(keyboardRow);
//Добавляем одну кнопку с текстом "Просвяти" наш ряд
keyboardRow.add(new KeyboardButton(STATUS));
//добавляем лист с одним рядом кнопок в главный объект
replyKeyboardMarkup.setKeyboard(keyboardRows);
return replyKeyboardMarkup;
}
public SendMessage mainMenu(long chatId) {
SendMessage message = new SendMessage();
message.setChatId(chatId);
message.setText("Choose an option:");
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText(STATUS);
inlineKeyboardButton.setCallbackData("Button \"" + STATUS + "\" has been pressed");
keyboardRow.add(inlineKeyboardButton);
List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
keyboard.add(keyboardRow);
inlineKeyboardMarkup.setKeyboard(keyboard);
message.setReplyMarkup(inlineKeyboardMarkup);
return message;
}
}