fix
parent
d23910af9e
commit
68d0fb2ced
|
@ -0,0 +1,33 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.view.TelegramBotKeyboard;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Order(3)
|
||||
public class ReloadNotification implements ApplicationRunner {
|
||||
|
||||
private final TelegramBotKeyboard telegramBotKeyboard;
|
||||
private final MessagingService messagingService;
|
||||
private final UserService userService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
userService.getAllUsers().forEach(user -> {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
sendMessage.setChatId(user.getTelegramId());
|
||||
sendMessage.setText("Бот перезапущен");
|
||||
messagingService.send(telegramBotKeyboard.uiForm(sendMessage));
|
||||
});
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ import ru.ldeloff.servermonitorbot.service.SshService;
|
|||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Order(3)
|
||||
@Order(4)
|
||||
public class SshConnect implements ApplicationRunner {
|
||||
|
||||
final SshService sshService;
|
||||
|
|
|
@ -1,54 +1,33 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.user.User;
|
||||
import ru.ldeloff.servermonitorbot.service.MessagingService;
|
||||
import ru.ldeloff.servermonitorbot.service.UserService;
|
||||
import ru.ldeloff.servermonitorbot.view.TelegramBotKeyboard;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@NoArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
@Order(1)
|
||||
public class StartBot implements ApplicationRunner {
|
||||
private TelegramBotController telegramBot;
|
||||
private TelegramBotKeyboard telegramBotKeyboard;
|
||||
private UserService userService;
|
||||
private MessagingService messagingService;
|
||||
private final TelegramBotController telegramBot;
|
||||
|
||||
@Autowired
|
||||
public StartBot(TelegramBotController telegramBot, TelegramBotKeyboard telegramBotKeyboard, UserService userService, MessagingService messagingService) {
|
||||
this.telegramBot = telegramBot;
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
this.userService = userService;
|
||||
}
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
try {
|
||||
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
|
||||
botsApi.registerBot(telegramBot);
|
||||
userService.getAllUsers().forEach(this::sendInitMessage);
|
||||
log.info("Бот запущен");
|
||||
} catch (TelegramApiException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
private void sendInitMessage(User user) {
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText("Бот перезапущен");
|
||||
messagingService.send(telegramBotKeyboard.uiForm(answer));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package ru.ldeloff.servermonitorbot.service;
|
|||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
|
||||
public interface MessagingService {
|
||||
void sendMessageToAll(String message);
|
||||
|
||||
void sendMessageToAdmins(String message);
|
||||
|
||||
void send(SendMessage message);
|
||||
|
|
|
@ -17,6 +17,16 @@ public class MessagingServiceImpl implements MessagingService {
|
|||
private final UserService userService;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void sendMessageToAll(String message) {
|
||||
userService.getAllUsers().forEach(user -> {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
sendMessage.setChatId(user.getTelegramId());
|
||||
sendMessage.setText(message);
|
||||
send(sendMessage);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessageToAdmins(String message) {
|
||||
userService.getAllAdmins().forEach(user -> {
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
spring:
|
||||
application:
|
||||
name: ServerMonitorBot
|
||||
datasource:
|
||||
url: jdbc:postgresql://db:5432/servermonitorbot
|
||||
username: servermonitorbot
|
||||
password: servermonitorbot
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: none
|
||||
flyway:
|
||||
locations: classpath:db/migration
|
||||
bot:
|
||||
name: "ServerMonitorBot"
|
||||
token: "token"
|
||||
|
@ -17,7 +8,7 @@ bot:
|
|||
- telegramId: 123456789
|
||||
role: admin
|
||||
ssh:
|
||||
checkConnectionDelay: 60000
|
||||
checkConnectionDelay: 300000
|
||||
timeout: 5000
|
||||
servers:
|
||||
-
|
||||
|
|
Loading…
Reference in New Issue