Добавлена кнопка uname, пока не полный функционал
parent
26d9f83917
commit
7b18162d30
|
@ -9,7 +9,6 @@ import org.springframework.stereotype.Component;
|
|||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
||||
import ru.ldeloff.servermonitorbot.service.TelegramBotService;
|
||||
|
||||
@Slf4j
|
||||
|
|
|
@ -7,10 +7,11 @@ import ru.ldeloff.servermonitorbot.model.SshServer;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface SshService {
|
||||
void connectToAllServer();
|
||||
Session connectToServer(SshServer sshServer) throws JSchException;
|
||||
void disconnectSessions();
|
||||
SendMessage getStatusSessions(Long chatId);
|
||||
|
||||
List<SshServer> getSshServers();
|
||||
}
|
||||
|
|
|
@ -73,4 +73,9 @@ public class SshServiceImpl implements SshService{
|
|||
message.setChatId(chatId);
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshServer> getSshServers() {
|
||||
return sshServers;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.exceptions.TelegramApiException;
|
||||
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
|
||||
@Slf4j
|
||||
|
@ -16,14 +17,16 @@ public class TelegramBotService extends TelegramLongPollingBot {
|
|||
|
||||
final TelegramBot telegramBot;
|
||||
final SshService sshService;
|
||||
final TelegramBotMenu telegramBotMenu;
|
||||
final TelegramBotKeyboard telegramBotKeyboard;
|
||||
final UnameChatButton unameChatButton;
|
||||
|
||||
|
||||
@Autowired
|
||||
public TelegramBotService(TelegramBot telegramBot, SshService sshService, TelegramBotMenu telegramBotMenu) {
|
||||
public TelegramBotService(TelegramBot telegramBot, SshService sshService, TelegramBotKeyboard telegramBotKeyboard, UnameChatButton unameChatButton) {
|
||||
this.telegramBot = telegramBot;
|
||||
this.sshService = sshService;
|
||||
this.telegramBotMenu = telegramBotMenu;
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
this.unameChatButton = unameChatButton;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -37,7 +40,7 @@ public class TelegramBotService extends TelegramLongPollingBot {
|
|||
case "/start":
|
||||
message.setText("Привет: " + update.getMessage().getChat().getUserName());
|
||||
message.setChatId(chatId);
|
||||
message.setReplyMarkup(telegramBotMenu.initKeyboard());
|
||||
message.setReplyMarkup(telegramBotKeyboard.initKeyboardButtons());
|
||||
sendMessage(message);
|
||||
log.info("Получена команда /start");
|
||||
break;
|
||||
|
@ -45,23 +48,20 @@ public class TelegramBotService extends TelegramLongPollingBot {
|
|||
sendMessage(sshService.getStatusSessions(chatId));
|
||||
log.info("Получена команда 'Статус'");
|
||||
break;
|
||||
case "uname":
|
||||
sendMessage(unameChatButton.initMenu(chatId));
|
||||
log.info("Получена команда 'uname'");
|
||||
break;
|
||||
default:
|
||||
message.setText("unknown command:" + messageText);
|
||||
message.setChatId(chatId);
|
||||
message.setReplyMarkup(telegramBotMenu.initKeyboard());
|
||||
message.setReplyMarkup(telegramBotKeyboard.initKeyboardButtons());
|
||||
sendMessage(message);
|
||||
log.warn("Неизвестная команда:" + messageText);
|
||||
}
|
||||
}
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
try {
|
||||
SendMessage message = new SendMessage();
|
||||
message.setText(update.getCallbackQuery().getData());
|
||||
message.setChatId(update.getCallbackQuery().getMessage().getChatId());
|
||||
execute(message);
|
||||
} catch (TelegramApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(update.getCallbackQuery().getData()); // TODO
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue