rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.68k stars 1.18k forks source link

TelegramApiRequestException: Error getting updates #508

Closed aarystan closed 5 years ago

aarystan commented 6 years ago

Everything is working: getting update, sending message. But in log periodically prints this :

ERROR 52828 --- [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.0.1.jar:na]
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.getUpdatesFromServer(DefaultBotSession.java:256) ~[telegrambots-4.0.1.jar:na]
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.run(DefaultBotSession.java:185) ~[telegrambots-4.0.1.jar:na]

I have no any ran instances. What's wrong?

    public static void main(String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi botApi = new TelegramBotsApi();
        try {
            botApi.registerBot(new SuperBot());
        } catch (TelegramApiException e) {
            LOGGER.error("TelegramApiException " + e.getMessage(), e);
        }

        SpringApplication.run(Application.class, args);
    }
    @Override
    public void onUpdateReceived(Update update) {
        // We check if the update has a message and the message has text
        if (update.hasMessage() && update.getMessage().hasText()) {
            SendMessage message = new SendMessage() // Create a SendMessage object with mandatory fields
                    .setChatId(update.getMessage().getChatId())
                    .setText(update.getMessage().getText());
            try {
                execute(message); // Call method to send the message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }
sirojovpolot commented 6 years ago

image

delete java

Bitrons commented 5 years ago

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>
aarystan commented 5 years ago

@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

Bitrons commented 5 years ago

@aarystan thanks anyway.

JavaStream commented 4 years ago

There was such an error when another second copy of this bot was launched.

SanzharIo commented 4 years ago

Hi, has anybody solution of this problem?

jiaweiloo commented 4 years ago

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 ?

Chase22 commented 4 years ago

@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) {}
Chijuk commented 3 years ago

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

excentr0 commented 3 years ago

Hello. So, all we have to do is something this: @AfterBotRegistration public void afterRegistration(BotSession session) { session.stop(); }?

Chijuk commented 3 years ago

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.

vokuznetsov commented 3 years ago

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);
        }
    }
}
lilhovhann commented 3 years ago

There was such an error when another second copy of this bot was launched. You are right

Vaisero commented 11 months ago

I FOUND THE SOLUTION!!!! TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class); try { var session = telegramBotsApi.registerBot(bot); session.stop();