Universal-Arduino-Telegram-Bot
Universal-Arduino-Telegram-Bot copied to clipboard
Longpoll - non blocking version
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();
}
}
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 .
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)
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 , 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()?
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.
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?
I got this compiler error:
undefined reference to UniversalTelegramBot::sendMessage(String, String, String, bool)'`
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)
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
Fixed. I had to refresh the libry cache
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 !
What is the use of disable_notification in sendMessage()?
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.
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
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.
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?
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).
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.
@RomanLut I noticed a little delay in responses (about 4-5 seconds). Is it normal or there is a way to expedite it?
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.
Ok. Could ESP32 be an option to keep 2 connections alive?
I do not have experience with ESP32.
@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?
@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
I'm not sure what the current status of this is, is it still worth looking into?
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 .
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.
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