CTBot icon indicating copy to clipboard operation
CTBot copied to clipboard

Doesn't send a message if getNewMessage() hasn't been run just before

Open jekyll2014 opened this issue 5 years ago • 6 comments

In my IoT project I've ran into a problem (a bug?) - I can't send a message if getNewMessage() hasn't been ran right before sendMessage(). Even if I'm calling getNewMessage() in another branch of a program inside one loop() cycle sendMessage() doesn't work. It seems like there is a dependancy between getNewMessage() and sendMessage() which is not obvious... Or it's a Telegram limitation/feature to reject sender after timeout or if unreceived message history is not empty...

  • my current project code (too big but you can search TELEGRAM_ENABLE definition to see only related parts): https://github.com/jekyll2014/ESP8266_IOT_sensor/blob/master/ESP8266_IOT_sensor.ino I can try to make a simplified example to demonstrate.

  • older version which didn't work because of the issue described: https://github.com/jekyll2014/ESP8266_IOT_sensor/commit/648859d6e9ddcda88deaa46e4829eede852507da#diff-4aa979edc4317dd7feddabcb4343d609

P.S. You library works well with ArduinoJson v 5.13.5. Grazie!

jekyll2014 avatar Mar 04 '19 09:03 jekyll2014

Hello Jekyll, have you tried a very simple sketch to check it? I loaded on NodeMCU this very simple sketch, enabling the CTBOT_DEBUG_MODE in CTBot.h

#include "CTBot.h"
CTBot myBot;
void setup() {
	Serial.begin(115200); // initialize the serial
	delay(1000);
	myBot.wifiConnect("mySSD", "myPassword"); // connect to the WiFi Network
	myBot.setTelegramToken("myToken"); // set the telegram bot token
}
void loop() {
	myBot.sendMessage(myTelegramID, "Ping");
	delay(2000); // wait two sec between two messages
}

With this sketch running, the serial output is:

Connecting Wifi: mySSID
...
WiFi connected
IP address: 172.20.10.9

Connected using DNS
SendMessage JSON: {
  "ok": true,
  "result": {
    "message_id": XXXX,
    "from": {
      "id": MyBotID,
      "is_bot": true,
      "first_name": "MyBotName",
      "username": "MyBotUsername"
    },
    "chat": {
      "id": myTelegramID,
      "first_name": "MyName",
      "username": "MyUsername",
      "type": "private"
    },
    "date": 1551695027,
    "text": "Ping"
  }
}

... <the same JSON every two seconds>

As you can see checking the sketch and the serial log, I sent messages without running a CTBot::getNewMessage(). Obviuosly, I got a "Ping" message on my account every 2 sec.

Does this sketch reproduce the pattern that generate the error that you encounter? If yes, please check if the sketch provided work fine and let me know.

Regards,

Stefano

shurillu avatar Mar 04 '19 10:03 shurillu

Ciao Stefano! Your example works perfect. I'll investigate what was wrong with my old version code. Anyway - thank you very much for this library! It's great! Grazie mille!

P.S. Do you plan to update to ArduinoJSON v6 ?

jekyll2014 avatar Mar 04 '19 19:03 jekyll2014

Hello Andrey, maybe, are you using negative IDs when launching a CTBot::sendMessage()? They are tipically found in channelIDs and groupIDs... If yes, there was an issue #18: I published a release to fix it (1.3.1).

Anyway, let me know if you find the bug as we can close this issue (and because I'm curious about that!).

Speaking about ArduinoJson, I saw that Benoit Blanchon has released a non-beta 6.x.x version (6.9.1) of his library, so soon I'll add it. The main problem is making the CTBot library compatible with the two versions (the 5.x.x and the 6.x.x).

A presto!

Stefano

shurillu avatar Mar 05 '19 07:03 shurillu

Ciao Stefano, It wasn't ID overflow definitely - I've been following your groupIDs fix from the very beginning and tested it. I hope to make some tests at weekend and report if there will be any interesting result... P.S. I'm buffering outcoming messages in my project and it seems that sendMessage() reply sometimes is not reliable - some messages are doubled or even tripled... Will test it also and report an issue if confirmed. Buon giorno!

jekyll2014 avatar Mar 06 '19 06:03 jekyll2014

Ciao Stefano, Scusate... I've been a bit busy but finally cut my code to a testable version - you can see it at https://github.com/jekyll2014/ESP8266_IOT_sensor/blob/master/testTelegram.rar It's buffering outgoing messages and it seems that sometimes messages are doubled because arduino client thinks message wasn't sent.

jekyll2014 avatar Mar 14 '19 21:03 jekyll2014

Hello Andrey, Telegram bots are intended as "computer to human" interaction. In other words, they are not intended as "binary comunication", so the library too. You can still use the bots for sending binary data (images, audio data, etc) but actually the library implements only text messages, position (location) messages and inline keyboard messages.

Check https://core.telegram.org/bots/api

Keep in mind that text messages are sent by HTTP/S GET protocol inside a query string, so they must be coded to be HTTP/S protocol compliant. If you want to send binary data as a message, I suggest you to use, for example, Base64 coding sheme

https://en.wikipedia.org/wiki/Base64

Finally, as I said, Telegram is a human to human istant messaging system so bots have to comply this protocol. In other words, a human don't send five message per seconds (for example) so the bots too. If you want to send binary data with high frequency, maybe the Telegram platform is not the best solution for you (have you ever seen for example MQTT protocol or Blynk platform?).

A presto,

Stefano

shurillu avatar Jun 04 '19 08:06 shurillu