CTBot: getNewMessage is taking too long

Hi,

First of all, thank you for the library.

Second, I had implemented your inlineKeyboard function to my sketch. I am building a temperature and humidity controller using wemos D1R2, BME280 sensor, I2C 1602 LCD and 2ch relays. Before I implemented CTBot, I tested my sketch and were able to pull data from the sensor every 1 second. However, after implementing, each iteration lasted about 4 seconds. I used your inlineKeyboard examples and it also show that each iteration lasted about 4 seconds. May I know what did I do wrong?

Thank you in advance

#include "CTBot.h"

#define LIGHT_ON_CALLBACK  "lightON"  // callback data sent when "LIGHT ON" button is pressed
#define LIGHT_OFF_CALLBACK "lightOFF" // callback data sent when "LIGHT OFF" button is pressed

CTBot myBot;
CTBotInlineKeyboard myKbd;  // custom inline keyboard object helper

String ssid = "";     // REPLACE mySSID WITH YOUR WIFI SSID
String pass = ""; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "";   // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
uint8_t led = D5;            // the onboard ESP8266 LED.    
                            // If you have a NodeMCU you can use the BUILTIN_LED pin
                            // (replace 2 with BUILTIN_LED) 
                            // ATTENTION: this led use inverted logic

void setup() {
  // initialize the Serial
  Serial.begin(115200);
  Serial.println("Starting TelegramBot...");

  // connect the ESP8266 to the desired access point
  myBot.wifiConnect(ssid, pass);

  // set the telegram bot token
  myBot.setTelegramToken(token);

  // check if all things are ok
  if (myBot.testConnection())
    Serial.println("\ntestConnection OK");
  else
    Serial.println("\ntestConnection NOK");

  // set the pin connected to the LED to act as output pin
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH); // turn off the led (inverted logic!)

  // inline keyboard customization
  // add a query button to the first row of the inline keyboard
  myKbd.addButton("LIGHT ON", LIGHT_ON_CALLBACK, CTBotKeyboardButtonQuery);
  // add another query button to the first row of the inline keyboard
  myKbd.addButton("LIGHT OFF", LIGHT_OFF_CALLBACK, CTBotKeyboardButtonQuery);
  // add a new empty button row
  myKbd.addRow();
  // add a URL button to the second row of the inline keyboard
  myKbd.addButton("see docs", "https://github.com/shurillu/CTBot", CTBotKeyboardButtonURL);
}

void loop() {
  // a variable to store telegram message data
  TBMessage msg;

  // if there is an incoming message...
  if (myBot.getNewMessage(msg)) 
  {
    // check what kind of message I received
    if (msg.messageType == CTBotMessageText) 
    {
      // received a text message
      if (msg.text.equalsIgnoreCase("show keyboard")) 
      {
        // the user is asking to show the inline keyboard --> show it
        myBot.sendMessage(msg.sender.id, "Inline Keyboard", myKbd);
      }
      else 
      {
        // the user write anithing else --> show a hint message
        myBot.sendMessage(msg.sender.id, "Try 'show keyboard'");
      }
    } 
    else if (msg.messageType == CTBotMessageQuery)
    {
      // received a callback query message
      if (msg.callbackQueryData.equals(LIGHT_ON_CALLBACK))
      {
        // pushed "LIGHT ON" button...
        digitalWrite(led, LOW); // ...turn on the LED (inverted logic!)
        // terminate the callback with an alert message
        myBot.endQuery(msg.callbackQueryID, "Light on", true);
      } 
      else if (msg.callbackQueryData.equals(LIGHT_OFF_CALLBACK)) 
      {
        // pushed "LIGHT OFF" button...
        digitalWrite(led, HIGH); // ...turn off the LED (inverted logic!)
        // terminate the callback with a popup message
        myBot.endQuery(msg.callbackQueryID, "Light off");
      }
    }
  }
  // wait 500 milliseconds
  delay(500);
  Serial.println("Test");
} 

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Hello Alexander, very good point thank you! As you said, the timer must be cleared AFTER the Telegram response. Talking about your second question, I never encountered a long response delay by Telegram server so I never implemented something to “mitigate” these connection delays… thank you for reporting me this issue. So I added a timeout (should be in ms - documentation is very confusing) ad you can modify it by editing CTBotDefines.h - CTBOT_CONNECTION_TIMEOUT. Now is set to 2000 ms. If you want to try it please download the master branch and let me know, as I can draft a new release with these changes. Actually I can’t simulate a connection timeout.

Cheers,

Stefano