shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
151 stars 35 forks source link

CTBot with wifi manager #127

Closed mjtrini closed 1 month ago

mjtrini commented 1 month ago

Hi Stefano,

I want to thank you again for all your hard work. I noticed using this wifi manager: (https://github.com/alanswx/ESPAsyncWiFiManager), the telegram on my nodemcu esp8266 cannot connect. The wifi manager connects successfully but CTBot says connection is not ok.

Is there a way to use CTBot with the wifi mamager?

Much thanks for any assistance. Michael.

shurillu commented 1 month ago

Hi Michael, the wifiConnect()member function simply establish a wifi connection that can be done in several way. In other words, if you have already established a wifi connection, you don't need to call it. So, if you already have established a wifi connection with ESPAsyncWiFiManager, all you have to do is setting your bot token (once, in the setup block) and check for incoming new messages/send messages (tipically in the loop block). Obviously, the wifi connection must have an internet access ;-)

If that cant solve your issue, please leave me your code as I can give a look.

Cheers

Stefano

mjtrini commented 1 month ago

Hi Stefano,

Thanks for the rapid reply. I added the ESPAsyncWiFiManager basic example into your example code and the same issue of telegram not connecting yet the ESPAsyncWiFiManager connects successfully. Below is the sketch:

if defined(ESP8266)

include //https://github.com/esp8266/Arduino

else

include

endif

//needed for library

include

include //https://github.com/tzapu/WiFiManager

AsyncWebServer server(80); DNSServer dns;

include "CTBot.h"

CTBot myBot; CTBotReplyKeyboard myKbd; // reply keyboard object helper bool isKeyboardActive; // store if the reply keyboard is shown

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

void setup() { // initialize the Serial Serial.begin(115200); Serial.println("Starting TelegramBot..."); AsyncWiFiManager wifiManager(&server,&dns); wifiManager.autoConnect("AutoConnectAP"); Serial.println("connected...yeey :)"); // connect the ESP8266 to the desired access point //myBot.wifiConnect(ssid, pass); delay(5000); // 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");

// reply keyboard customization // add a button that send a message with "Simple button" text myKbd.addButton("Simple button"); // add another button that send the user contact myKbd.addButton("Contact request", CTBotKeyboardButtonContact); // add another button that send the user position (location) myKbd.addButton("Location request", CTBotKeyboardButtonLocation); // add a new empty button row myKbd.addRow(); // add a button that send a message with "Hide replyKeyboard" text // (it will be used to hide the reply keyboard) myKbd.addButton("Hide replyKeyboard"); // resize the keyboard to fit only the needed space myKbd.enableResize(); isKeyboardActive = false; }

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 reply keyboard --> show it myBot.sendMessage(msg.sender.id, "Reply Keyboard enable. You can send a simple text, your contact, your location or hide the keyboard", myKbd); isKeyboardActive = true; } // check if the reply keyboard is active else if (isKeyboardActive) { // is active -> manage the text messages sent by pressing the reply keyboard buttons if (msg.text.equalsIgnoreCase("Hide replyKeyboard")) { // sent the "hide keyboard" message --> hide the reply keyboard myBot.removeReplyKeyboard(msg.sender.id, "Reply keyboard removed"); isKeyboardActive = false; } else { // print every others messages received myBot.sendMessage(msg.sender.id, msg.text); } } else { // the user write anything else and the reply keyboard is not active --> show a hint message myBot.sendMessage(msg.sender.id, "Try 'show keyboard'"); } } else if (msg.messageType == CTBotMessageLocation) { // received a location message --> send a message with the location coordinates myBot.sendMessage(msg.sender.id, "Longitude: " + (String)msg.location.longitude + "\nLatitude: " + (String)msg.location.latitude);

} else if (msg.messageType == CTBotMessageContact) {
  // received a contact message --> send a message with the contact information
  myBot.sendMessage(msg.sender.id, "Name: " + (String)msg.contact.firstName +
    "\nSurname: " + (String)msg.contact.lastName +
    "\nPhone: " + (String)msg.contact.phoneNumber +
    "\nID: " + (String)msg.contact.id +
    "\nvCard: " + (String)msg.contact.vCard);
}

} // wait 500 milliseconds delay(500); }

The response from the serial monitor is:

Starting TelegramBot... WM: WM: AutoConnect Try No.: WM: 0 WM: Connecting as wifi client... WM: Using last saved values, should be faster WM: Connection result: WM: 3 WM: IP Address: *WM: 192.168.1.94 connected...yeey :)

testConnection NOK

shurillu commented 1 month ago

Hello Michael, I found the issue: the ESP8266 platform needs to synch time in order to verify the SSL certificate; this was done inside the wifiConnect() member function. So, when an user defined wifi connection function is used (i.e. WiFiManager), the time is not synchronized, so the certificarion verification failed, thus all the Telegram Server calls fail. I drafted a new release (2.1.14) that fix this issue. Could try it?

Thanks!

Stefano

mjtrini commented 1 month ago

Hi Stefano!

Much thanks for your efforts. I am currently out of the country and far away from my esp8266 equipment. I will be back by Sunday. I'm excited to test the revised library.

The new library with site certificate expires on a regular basis? If yes, will the certificate need to be replaced in a future revision of your ctbot?

Much thanks, Michael.

shurillu commented 1 month ago

Hi Michael, the site certificate expire issue was solved in the 2.1.12 so, no more fingerprint updates are needed.

Stefano

mjtrini commented 1 month ago

Stefano!

You are a genius! It works. Just for my information, when is the site certificate expected to expire?

Also, how can I make a small contribution as a thank you for your efforts?

Regards, Michael.

shurillu commented 1 month ago

Hi Michael, now the certificate is no more validated by fingerprint, thus the procedure does not need updates, as long as the Telegram SSL certificate is valid.

Actually using the library and reporting bugs, upgrades suggestions etc. are the best contributions ;-)

Regards,

Stefano

mjtrini commented 1 month ago

Stefano,

You Sir are a genius and a gentleman. If you ever visit Trinidad and Tobago, let me know!

Have a fantastic day my friend. I'm closing the issue now.

Michael.