rubenlagus / TelegramBots

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

Need to have possibility to check status of connection in long polling mode #299

Open dburnazyan opened 7 years ago

dburnazyan commented 7 years ago

When I use Long Polling Bot I need to have possibility check if all work fine: is bot is connected to server or something goes wrong.

Clevero commented 7 years ago

class BotSession has a method called isRunning(): boolean https://github.com/rubenlagus/TelegramBots/blob/383b48cddd70af742782493d2e32d53f8fadcd4b/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/BotSession.java#L27

dburnazyan commented 7 years ago

But this method returns true even if connection is lost

dburnazyan commented 7 years ago

method run() of ReaderThread class looping until 'running' is true

https://github.com/rubenlagus/TelegramBots/blob/383b48cddd70af742782493d2e32d53f8fadcd4b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java#L178

running becomes false only when stop method of DefaultBotSession class is calling

So when telegram server is not accessible ReaderThread continues looping and just generates logs. But isRunning() still returns true because ReaderThread has't changed parameter 'running' to false

dburnazyan commented 7 years ago

I suppose that there should be another method like 'isConnectionFine' or something like this and when httpclient request throws some exception while execution this method should return false.

Chase22 commented 5 years ago

The DefaultBotSession has a try-catch where the actual request is executed

https://github.com/rubenlagus/TelegramBots/blob/383b48cddd70af742782493d2e32d53f8fadcd4b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java#L196

We could add either just a boolean if that try-catch was sucessfull, add a callback if it didn't or make an enum with a few more statuses depending on why it failed.

genjiezio commented 3 years ago

I'd like to add a new "isConnectMethod" to return a boolean value about connection status, and won't set isrunning to false directly.