pengrad / java-telegram-bot-api

Telegram Bot API for Java
https://core.telegram.org/bots
Apache License 2.0
1.79k stars 372 forks source link

TelegramBot.setUpdatesListener(UpdatesListener, GetUpdates) #105

Closed theskylineguy closed 7 years ago

theskylineguy commented 7 years ago

Hi,

I am having some problems here: when I run the application it prints out the same first Update in an infinite loop, acting like it doesn't confirm it. I'm pretty sure it's an offset issue, though I don't understand how to increment its value like you explained in your documentation, since I am using the UpdatesListener with the getUpdates request and not just the second one - using this because I need to filter only some kind of Updates. Could you clarify me how to achieve this?

Here's part of the code:

public final class Main
{
    public static void main (String[] args)
    {
        final TelegramBot bot = TelegramBotAdapter.build ("<TOKEN>");

        final UpdatesListener updateListener = new UpdatesListener ()
        {
            @Override
            public int process (List<Update> updates)
            {
                Update lastUpdate = updates.get (updates.size() - 1);

                for (Update update : updates)
                {
                    out.println (update);
                }

                return UpdateManager.confirm (lastUpdate);
            }
        };

        bot.setUpdatesListener (updateListener, new GetUpdates().offset(0).limit(10).timeout(30).allowedUpdates("message", "callback_query", "channel_post"));
    }
}

public class UpdateManager
{
    public static int confirm (Update update)
    {
        return (update.updateId().intValue() + 1);
    }
}

Thanks in advance.

pengrad commented 7 years ago

Hi, Just returning return UpdatesListener.CONFIRMED_UPDATES_ALL; will work for you?

https://github.com/pengrad/java-telegram-bot-api#updates-listener

theskylineguy commented 7 years ago

I already tried this, but it doesn't seem to work for me. What else can I do?

pengrad commented 7 years ago

I'm afraid it can happens because allowedUpdates. Try to test Api directly, or use this one https://rapidapi.com/package/TelegramBot/functions/getUpdates

And return here if it will work for you

theskylineguy commented 7 years ago

I get the right Updates both on https://api.telegram.org/botTOKEN/getUpdates and https://rapidapi.com/package/TelegramBot/functions/getUpdates.

The problem is not in the Telegram API, but how to confirm incoming Updates via the GetUpdates request in setUpdatesListener method.

pengrad commented 7 years ago

I dont handle allowedUpdates. Will release 3.1.1 today with fix

pengrad commented 7 years ago

Btw, returning CONFIRMED_UPDATES_ALL or updates.get(updates.size() - 1).updateId() (don't increment it by yourself) works fine for me now

theskylineguy commented 7 years ago

Can you post a snippet of any bot of yours code for comparison?

pengrad commented 7 years ago

https://github.com/pengrad/java-telegram-bot-api/blob/master/library/src/test/java/com/pengrad/telegrambot/TestGetUpdatesListener.java

pengrad commented 7 years ago

I fixed it in 3.2.0 together with Sticker updates. Use updates.get(updates.size() - 1).updateId() or CONFIRMED_UPDATES_ALL