Уведомление о перезапуске
Hedgehog_server_CI/ServerMonitorBot/pipeline/pr-master This commit looks good Details
Hedgehog_server_CI/ServerMonitorBot/pipeline/head This commit looks good Details

pull/39/head
L_DelOff 2023-12-17 16:41:24 +03:00
parent 25b73feb48
commit 21f4a71c63
5 changed files with 32 additions and 3 deletions

View File

@ -7,19 +7,25 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
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;
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
import ru.ldeloff.servermonitorbot.service.user.UserService;
@Slf4j
@Component
@NoArgsConstructor
public class StartBot implements ApplicationRunner {
private TelegramBotController telegramBot;
private UserService userService;
@Autowired
public StartBot(TelegramBotController telegramBot) {
public StartBot(TelegramBotController telegramBot, UserService userService) {
this.telegramBot = telegramBot;
this.userService = userService;
}
@Override
@ -27,10 +33,18 @@ public class StartBot implements ApplicationRunner {
try {
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
botsApi.registerBot(telegramBot);
userService.getAllUsers().forEach(this::SendInitMessage);
log.info("Бот запущен");
} catch (TelegramApiException e) {
e.printStackTrace();
//e.printStackTrace();
log.error(e.getMessage());
}
}
private void SendInitMessage(User user) {
SendMessage answer = new SendMessage();
answer.setChatId(user.getTelegramId());
answer.setText("Бот перезапущен");
CommandTemplate.sendMessage(answer, telegramBot);
}
}

View File

@ -1,10 +1,15 @@
package ru.ldeloff.servermonitorbot.repository;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import ru.ldeloff.servermonitorbot.model.User;
import java.util.List;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User getByTelegramId(long id);
@NotNull List<User> findAll();
}

View File

@ -39,6 +39,7 @@ public abstract class CommandTemplate {
sendMessage(actionForNotAuth(user, answer), bot);
}
}
protected void logSuccess(User user, String message) {
log.info("Получена команда '" + message + "' от " + user.getLogin()
+ " (" + user.getTelegramId() + "). OK");
@ -76,7 +77,7 @@ public abstract class CommandTemplate {
return "";
}
private void sendMessage(SendMessage message, TelegramBotController bot) {
public static void sendMessage(SendMessage message, TelegramBotController bot) {
try {
bot.execute(message);
} catch (TelegramApiException e) {

View File

@ -2,8 +2,12 @@ package ru.ldeloff.servermonitorbot.service.user;
import ru.ldeloff.servermonitorbot.model.User;
import java.util.List;
public interface UserService {
User saveOrUpdateUser(User user);
User getByTelegramId(Long id);
List<User> getAllUsers();
}

View File

@ -6,6 +6,8 @@ import org.springframework.stereotype.Service;
import ru.ldeloff.servermonitorbot.model.User;
import ru.ldeloff.servermonitorbot.repository.UserRepository;
import java.util.List;
@Service
@RequiredArgsConstructor
@Slf4j
@ -26,4 +28,7 @@ public class UserServiceImpl implements UserService {
public User getByTelegramId(Long id) {
return userRepository.getByTelegramId(id);
}
@Override
public List<User> getAllUsers() {return userRepository.findAll(); }
}