Search code examples
javatelegram-bottelegram-webhook

JAVA Telegram bots api Error getting updates: Conflict: terminated by other long poll or webhook


I am using Java Telegram Bot API with Spring framework, I had a method in my HomeController and I had a class that handle all of the incoming messages from users. I got these errors in my spring log, then I got duplicated response from telegram bot API .whats is the problem?

  @PostConstruct
    public void initBots() {
        ApiContextInitializer.init();
        TelegramBotsApi botsApi = new TelegramBotsApi();
        BotService botService = new BotService();
        try {
            botsApi.registerBot(botService);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

[abrsystem1_bot Telegram Connection] org.telegram.telegrambots.logging.BotLogger.severe BOTSESSION org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error getting updates: [409] Conflict: terminated by other long poll or webhook at org.telegram.telegrambots.api.methods.updates.GetUpdates.deserializeResponse(GetUpdates.java:119) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.getUpdatesFromServer(DefaultBotSession.java:255) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.run(DefaultBotSession.java:186)

@Override
public void onUpdateReceived(Update update) {
    try {

        if (update.hasMessage() && update.getMessage().hasText()) {

            String message_text = update.getMessage().getText();
            String wellcome_text = "برای ثبت نام در سایت  شماره تلفن همراه خود را به اشتراک بگذارید";

            long chat_id = update.getMessage().getChatId();
            if (message_text.equals("/start")) {

                try {
                    SendMessage message = new SendMessage()
                            .setChatId(chat_id)
                            .setText(wellcome_text);

                    ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
                    List<KeyboardRow> keyboard = new ArrayList<KeyboardRow>();
                    KeyboardRow row = new KeyboardRow();
                    row.add((new KeyboardButton().setText("اشتراک شماره موبایل").setRequestContact(true)));
                    keyboard.add(row);
                    keyboardMarkup.setKeyboard(keyboard);
                    message.setReplyMarkup(keyboardMarkup);

                    try {
                        execute(message);
                    } catch (TelegramApiException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else if (message_text.equals("تایید مشخصات کاربری")) {
                SendMessage sendMessage = new SendMessage();
                sendMessage.setChatId(chat_id).setText("اطلاعات مورد تایید قرار گرفت");

                try {
                    execute(sendMessage);
                    removeMarker(chat_id);
                    showContactInfo(chat_id, update);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else if (message_text.equals("تغییر مشخصات")) {

            } else {
                showUnknownCommand(chat_id);
            }
        } else if (update.getMessage().getContact() != null && update.getMessage().getChat() != null) {

            long chat_id = update.getMessage().getChatId();
            showContactInfo(chat_id, update);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution

  • I finally solved my problem after a day!when i was debugging my project with intellij idea on my computer , i created many instances for debug so i got multiple response from same chat id in telegram bot.so boring problem....