rubenlagus / TelegramBots

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

How to make sure a message has been sent? What does execute(SendMessage msg) return? #659

Open stormstricker opened 5 years ago

stormstricker commented 5 years ago

Sometimes there are connection issues, so execute() method throws an exception. A message might or might not get sent. I tried including another call to send message method in the catch block, however, if a message managed to get delivered successfully, despite the exception, it results in a duplicate message being sent. Is there a way to check if the message has been sent? execute() doesn't seem to return a boolean, only type T.

gekoramy commented 5 years ago

https://core.telegram.org/bots/api#sendmessage

On success, the sent Message is returned

stormstricker commented 5 years ago

https://core.telegram.org/bots/api#sendmessage

On success, the sent Message is returned

How would I check it using the telegrambots library though? SendMessage m = execute(sendMessage); gives a Incompatible types. Required SendMessage but 'execute' was inferred to T: no instance(s) of type variable(s) exist so that Message conforms to SendMessage error.

gekoramy commented 5 years ago

https://core.telegram.org/bots/api#sendmessage https://core.telegram.org/bots/api#message

Message and SendMessage are different things

Message msg = execute(new SendMessage(...));

stormstricker commented 5 years ago

https://core.telegram.org/bots/api#sendmessage https://core.telegram.org/bots/api#message

Message and SendMessage are different things

Message msg = execute(new SendMessage(...));

So would this check be correct?

org.telegram.telegrambots.meta.api.objects.Message m = null;
try {
      m = execute(sendMessage);
} 
catch (Exception e) {
    if (m==null)  {
       //send again
    }
}
gekoramy commented 5 years ago

I would say that if execute(...) throws an exception, the SendMessage is not sent Could you share the code where the opposite happens?

stormstricker commented 5 years ago

I would say that if execute(...) throws an exception, the SendMessage is not sent Could you share the code where the opposite happens?

The code is just the one above. An execute inside try-catch block. I connect to Telegram through VPN so connection timeouts is a common thing. I've found that even checking if a m==null doesn't prevent from sending duplicate messages - it seems that the first execute sends a message, there happens connection timeout exception, m is somehow still null, so I send the message again, only to later receive too duplicate messages. Without checkout if m is null, duplicate messages after connection timeout exception are even more common.

stormstricker commented 5 years ago

I wonder, is execute method asynchronous?

forrestrunning commented 4 years ago

Extending the question above - since the send() calls use Apache commons, would it be possible to set the timeout for calls globally? I am having unexpected waits when I send photos using sendPicture(URL) signature, hence the need.

rubenlagus commented 4 years ago

@forrestrunning Just override your custom implementation of BotOptions and should be done:

https://github.com/rubenlagus/TelegramBots/blob/master/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultBotOptions.java#L18