Closed aarystan closed 5 years ago
delete java
Hi. Have you find the solution ??? I have same problem, and I don't know how to fix it. Here my code:
@SpringBootApplication
public class Bitrix24telegrambotApplication {
public static void main(String[] args) {
ApiContextInitializer.init();
SpringApplication.run(Bitrix24telegrambotApplication.class, args);
}
}
@Component
public class TelegramBot extends TelegramLongPollingBot {
@PostConstruct
public void registerBot(){
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(new TelegramBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
@Override
public void onUpdateReceived(Update update) {
String messageText = "";
.......code......
}
}
2018-10-23 16:21:42.912 ERROR 6932 --- [gram Connection] Telegram Bots Api : BOTSESSION
org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error getting updates
at org.telegram.telegrambots.meta.api.methods.updates.GetUpdates.deserializeResponse(GetUpdates.java:119) ~[telegrambots-meta-4.1.jar:na]
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.getUpdatesFromServer(DefaultBotSession.java:256) ~[telegrambots-4.1.jar:na]
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.run(DefaultBotSession.java:185) ~[telegrambots-4.1.jar:na]
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-spring-boot-starter</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
@Bitrons Oh, i am sorry late response.
I have used Spring Boot and this problem show when i recompile source code without restart (with spring-dev-tools) App tries start new instance of bot when recompile code. Solution: kill bot instance programmatically before recompile each time or restart app
@aarystan thanks anyway.
There was such an error when another second copy of this bot was launched.
Hi, has anybody solution of this problem?
I tried PreDestroy but can't,
@Component public class MyChatBot extends TelegramLongPollingBot { @PreDestroy public void preDestroy() { DefaultBotSession dbs = ApiContext.getInstance(DefaultBotSession.class); System.out.println("session: " + dbs.isRunning()); dbs.stop(); }
Anybody have a solution in terminating this ?
@jiaweiloo You can inject the bot session by using a method annotated with @AfterBotRegistration and the bot session as a parameter.
The method will be called after the bot got registered and the bot session will be injected into this method
@AfterBotRegistration
public void afterRegistration(BotSession session) {}
Just like @Chase22 wrote:
inject BotSession
object with @AfterBotRegistration
annotation and than you can shoutdown session with its stop()
method. Also works in @PreDestroy
in class with @AfterBotRegistration
annotation
Hello.
So, all we have to do is something this:
@AfterBotRegistration public void afterRegistration(BotSession session) { session.stop(); }
?
If your purpose to stop bot right after it start you can do something like that.
Otherwise, for example, you can get BotSession
object inside @AfterBotRegistration
and close session later in appropriate way. For example, inside @PreDestroy
.
I got this error because I registered bot using @EventListener(ApplicationReadyEvent.class)
annotation.
When I had changed it to @PostConstruct
exception disappeared.
@Slf4j
@Configuration
public class TelegramBotConfig {
@PostConstruct
public void start() {
try {
log.info("Instantiate Telegram Bots API...");
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
log.info("Register Telegram Bots API...");
botsApi.registerBot(new TelegramBot());
} catch (TelegramApiException e) {
log.error("Exception instantiate Telegram Bot!", e);
}
}
}
There was such an error when another second copy of this bot was launched. You are right
I FOUND THE SOLUTION!!!!
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class); try { var session = telegramBotsApi.registerBot(bot); session.stop();
Everything is working: getting update, sending message. But in log periodically prints this :
I have no any ran instances. What's wrong?