openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.86k stars 3.56k forks source link

[telegram] Telegram binding does not use Long Polling #7043

Closed nerk closed 3 years ago

nerk commented 4 years ago

Expected Behavior

The telegram bot api should be queried for updates by using Long Polling (if WebHooks are not available).

Current Behavior

Currently, the binding uses ordinary polling and checks for updates every 100ms.

Possible Solution

When setting the listener for updates, the timeout for the GetUpdates() request must be specified explicitly as follows in TelegramHandler.java:

154        TelegramBot localBot = bot = new TelegramBot.Builder(botToken).okHttpClient(botLibClient).build();
155         localBot.setUpdatesListener(updates -> {
156             cancelThingOnlineStatusJob();
157             updateStatus(ThingStatus.ONLINE);
158             for (Update update : updates) {
         ........
239             }
240         }, new GetUpdates().timeout(60));

The current code does not specify the second parameter for setUpdatesListener(...), which results in generation of a default GetUpdates() object with a timeout set to 0. Timeout here is the actual time to wait for a response before returning. This is the waiting time for Long Polling.

Since my ISP seems to terminate a pending request after 50 seconds anyway, specifying 60 seconds here is plenty in my case.

It would be great if this would be configurable from the Paper UI. Maybe this also makes sense for the connection and read timeouts.

Steps to Reproduce (for Bugs)

Execute

tcpdump -i eth0 -A port 443

(check eth0 for correctness) and you will see lots of polling at 149.154.167.220 (telegram bot api)

Context

Regular polling results in huge amounts of idle traffic, which is particularly bad when using a mobile connection to the internet. In my case, the idle traffic was about 40-50 MB per day. This could be much worse if your mobile provider accounts for a minimum data packet length of 100k instead of 10k, for example.

Your Environment

EvilPingu commented 3 years ago

Hi,

Does anyone already deal with this issue? If not, I'll look at that.

EvilPingu

J-N-K commented 3 years ago

TTBOMK no one works on that. Feel free to take it. Greatly appreciated.