Answer the question
In order to leave comments, you need to log in
What annotations to use in Spring to reload an application?
Good afternoon. Please tell me if I'm doing everything right.
The task is to initialize all telegram bots when the component is initialized and end all sessions when the application is turned off.
import io.secret.support.channel.AddonApplication;
import io.secret.support.channel.bot.TelegramBot;
import io.secret.support.channel.dao.ProjectBotRepository;
import io.secret.support.channel.entity.BotProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotSession;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
@Component
public class TelegramBotsAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(TelegramBotsAutoConfiguration.class);
private ProjectBotRepository projectBotRepository;
private List<BotSession> sessions = new ArrayList<>();
static {
ApiContextInitializer.init();
}
@Autowired
public TelegramBotsAutoConfiguration(ProjectBotRepository projectBotRepository) {
log.info("Initialization of TelegramBotsAutoConfiguration");
this.projectBotRepository = projectBotRepository;
}
@PostConstruct
public void init() throws TelegramApiRequestException {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
for(BotProject botProject: projectBotRepository.findAll()) {
log.info("Bot ", botProject.getBotToken());
telegramBotsApi.registerBot(new TelegramBot(botProject.getBotToken()));
}
}
@PreDestroy
public void stop() {
sessions.forEach(session -> {
if (session != null) {
session.stop();
}
});
}
}
org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error getting updates
at org.telegram.telegrambots.meta.api.methods.updates.GetUpdates.deserializeResponse(GetUpdates.java:118) ~[telegrambots-meta-4.6.jar:na]
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.getUpdatesFromServer(DefaultBotSession.java:263) ~[telegrambots-4.6.jar:na]
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.run(DefaultBotSession.java:194) ~[telegrambots-4.6.jar:na]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question