Universal-Arduino-Telegram-Bot icon indicating copy to clipboard operation
Universal-Arduino-Telegram-Bot copied to clipboard

Longpoll - non blocking version

Open fmarzocca opened this issue 5 years ago • 28 comments

My ESP will receive no more than 5 or 6 commands from Telegram in 24 hours, but in such a case, I need a fast response, action and notification.

I have some doubt about using Longpoll. Who can give me Pros & Cons on Longpoll? In addition, would Long poll in some way delay the execution of other commands in the loop?

I am asking this question because I have noticed that when I set longPoll = 60, my device does not answer anymore to OTA, due to a timeout.

bot.longPoll = 60;

void loop() {
 server.handleClient();
 ArduinoOTA.handle();
 timeClient.update();
 
 if (millis() > Bot_lasttime + Bot_mtbs)  {
   int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   while (numNewMessages) {
     handleNewMessages(numNewMessages);
     numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   }
   Bot_lasttime = millis();
 }
}

fmarzocca avatar Feb 13 '20 18:02 fmarzocca

I explain it in this video

https://youtu.be/-IC-Z78aTOs

Basically it's good if the trigger for your code to activate is telegram and not something external like a sensor or button

On Thu, 13 Feb 2020, 18:25 Fabio Marzocca, [email protected] wrote:

My ESP will receive no more than 5 or 6 commands from Telegram in 24 hours, but in such a case, I need a fast response, action and notification.

I have some doubt about using Longpoll. Who can give me Pros & Cons on Longpoll?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/issues/143?email_source=notifications&email_token=AAL5PQR64HQGDCYFXXI7XRLRCWGCXA5CNFSM4KUYKL3KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4INLHCMA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAL5PQSNJLAFFB4SXL36GYDRCWGCXANCNFSM4KUYKL3A .

witnessmenow avatar Feb 13 '20 19:02 witnessmenow

Thank you Brian, the problem is that my application is activated by:

  • a telegram command
  • a pin interrupt
  • a http post

and I am noticing that everything is slowed down (until timeout) if I use longPoll. On the other hand, without a longPoll, my ESP8266 is constantly sending/receiving bits (the blue led is almost always on). (I have edited my first post)

fmarzocca avatar Feb 13 '20 20:02 fmarzocca

Longpool is implemented using busy wait in this library. getUpdates() blocks for {longPoll} seconds. Thus it is not usable in projects where you need to do some tasks in loop(). getUpdates() should just open connection and check if bytes are available in loop() without blocking. I modified library this way, getUpdates() can be called with any frequency without blocking. There still will be blocking when bot has to send responce. Or there is connection timeout. To fully fix this, bot has to be based on asynctcp library.

Attached is modified library. esp8266firmware.zip

RomanLut avatar Feb 20 '20 19:02 RomanLut

@RomanLut , do you mean that with your modified library I could set longPoll freely, without blocking other activities in the loop (such as ArduinoOTA.handle() or server.handleClient()?

fmarzocca avatar Feb 26 '20 20:02 fmarzocca

Do not change longPool memeber, it is private now and set to 50 seconds. Just call getUpdates() with any frequency without blocking. There still will be blocking when you send messages.

BTW I have tried to implement bot based on ESPAsyncTCP, but it does not work because TLS 1.2 is not supported by ESPAsyncTCP. api.telegram.org requires TLS 1.2 now.

RomanLut avatar Feb 26 '20 21:02 RomanLut

OK. I will give it a try and report back here. Should I have just to replace the .cpp and .h library files with yours?

fmarzocca avatar Feb 27 '20 08:02 fmarzocca

I got this compiler error:

undefined reference to UniversalTelegramBot::sendMessage(String, String, String, bool)'`

fmarzocca avatar Feb 27 '20 09:02 fmarzocca

Please double check that you use files from archive above. sendMessage() is properly declared(): .h bool sendMessage(String chat_id, String text, String parse_mode = "", bool disable_notification = false); .cpp: bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String parse_mode, bool disable_notification)

RomanLut avatar Feb 27 '20 09:02 RomanLut

Yes, I have looked at your code and I have seen it correctly declares the function. The problem is that the compiler looks like still using the old library! I have replaced .cpp and .h files with yours in /libraries/UniversalTelegramBot/src

fmarzocca avatar Feb 27 '20 09:02 fmarzocca

Fixed. I had to refresh the libry cache

fmarzocca avatar Feb 27 '20 09:02 fmarzocca

Tested. THIS IS GREAT! Finally I have get rid of all the huge network traffic, and the longPoll is not blocking my other functions in the loop!

Thank you very much, you should issue a Pull Request or fork this library with yours! Great job, @RomanLut !

fmarzocca avatar Feb 27 '20 09:02 fmarzocca

What is the use of disable_notification in sendMessage()?

fmarzocca avatar Feb 27 '20 09:02 fmarzocca

You can pass true to disable notification sound. I am sending log messages silently and alarm messages with notification sound, which I set to alarm sound in chat options in telegram app on phone.

RomanLut avatar Feb 27 '20 09:02 RomanLut

You can pass true to disable notification sound. I am sending log messages silently and alarm messages with notification sound, which I set to alarm sound in chat options in telegram app on phone.

Thanks

fmarzocca avatar Feb 27 '20 10:02 fmarzocca

Note that this update does not solve all problems. If you call sendMessage(), it will block for <5 seconds and next getUpdates() will block to <5 seconds once. Worse, if you have connection problems, each getUpdates() will block for 5-50 seconds because client->connect() will block while trying to establish connection to api.telegram.org. Also note issue https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/issues/38 If you send a message with long text (1300 bytes), library will fall into endless update cycle generating huge traffic. There is a fix which is not merged.

RomanLut avatar Feb 27 '20 10:02 RomanLut

Ok, I can accept that short block (<5sec) for my purposes. I hope not to have connection problems (the extender AP is close to the device) and I don't need to send long messages. How can I subscribe or be notified of library's updates? Do you have a github repo?

fmarzocca avatar Feb 27 '20 10:02 fmarzocca

No, I do not have repo and there will be no more updates from me because I will not be using this library (I can not accept any blocking in my device).

RomanLut avatar Feb 27 '20 10:02 RomanLut

No, I do not have repo and there will be no more updates from me because I will not be using this library (I can not accept any blocking in my device).

Maybe Brian @witnessmenow can include your mods into the official library... There is no other solution to avoid a short block, unless remove the longPoll and accept 30/40 GB of junk network activity per month. I had a very good experience with Raspberry and library telepot, but that's another story.

fmarzocca avatar Feb 27 '20 10:02 fmarzocca

@RomanLut I noticed a little delay in responses (about 4-5 seconds). Is it normal or there is a way to expedite it?

fmarzocca avatar Feb 29 '20 09:02 fmarzocca

Library maintains single connection because esp8266 does not have enough memory for two secured connections. If you call sendMessage(), library closes connection opened for longpool getUpdates(), reopens connection and sends message. Secure connection handshaking requires ~2-5 seconds on esp8266. I do no think something can be improved here.

RomanLut avatar Feb 29 '20 11:02 RomanLut

Ok. Could ESP32 be an option to keep 2 connections alive?

fmarzocca avatar Feb 29 '20 12:02 fmarzocca

I do not have experience with ESP32.

RomanLut avatar Feb 29 '20 19:02 RomanLut

@RomanLut It's a month that I am using your modified library in my device. Once a week it happens that the device gets blocked: it is pingable, but it does not respond to Telegram or send messages. I have tried with FW 2.6.1 and 2.5.3, but it's the same. I have to recycle power. Do you have any idea?

fmarzocca avatar Mar 31 '20 07:03 fmarzocca

@RomanLut since today Telegram servers has changed something and the library is not working anymore. I have seen that Brian release v.1.20. Do you have modified your code too? Thanks

fmarzocca avatar Jun 07 '20 18:06 fmarzocca

I'm not sure what the current status of this is, is it still worth looking into?

witnessmenow avatar Sep 30 '20 23:09 witnessmenow

Yes, I believe it is. I am currently working with Roman's patch.

On Thu, Oct 1, 2020 at 1:27 AM Brian Lough [email protected] wrote:

I'm not sure what the current status of this is, is it still worth looking into?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/issues/143#issuecomment-701698894, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACBCQ3Q64FNMULC523REJCTSIO5FNANCNFSM4KUYKL3A .

fmarzocca avatar Oct 01 '20 06:10 fmarzocca

I'm not sure what the current status of this is, is it still worth looking into?

@witnessmenow Brian, is there a chance to embed this mod into the latest library update? The library is generating 25GB of traffic/month with LongPoll=0 and I need to use a non-blocking LongPoll.

fmarzocca avatar Jan 01 '21 10:01 fmarzocca

that would be the best, as the roman patch doesn't work with the latest release 1.3.0 and I don't want to lose the benefits of the latest version:

C:\Users\user\Documents\Arduino\libraries\Universal-Arduino-Telegram-Bot-master\src\UniversalTelegramBot.cpp:797:20: error: 'keyboardBuffer' was not declared in this scope DynamicJsonBuffer keyboardBuffer; Thank you

droidprova avatar Feb 07 '21 10:02 droidprova