shurillu / CTBot

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

maybe is my ESP32 not working good, "Welcome null. Try LIGHT ON or LIGHT OFF." #88

Open konig87nikkinakki opened 2 years ago

konig87nikkinakki commented 2 years ago

Hi Shurillu, sorry for boring you. i use NODEMCU esp8266 and ESP32. i have a problem: only with ESP32 when i send a message to bot, it never receive and never goes into if (myBot.getNewMessage(msg)) { [....] }

while it worked with the same sketch on 12th august,and the same esp32. i had old version of Library, so i tried to upgrade but the error still remains. Only with ESP32. i ve bought it from AZ-DELIVERY, and i dont have another one to test. but i m pretty sure it's a Library or Telegram problem, even because esp32 send messages without any problem. So i thought i modified my sketch without remembering, and then i tried with sketch from library "LightBot". when i loaded to my ESP32 it read past messages that i previously sent to my bot, and then it answered JUST ONCE "Welcome null. Try LIGHT ON or LIGHT OFF."
if i try to write commands LIGHT ON or other random text, it doesnt answer. It called me "NULL" so i m afraid it doesnt recognize. can someone please test with a esp32 and if working please tell me: -> version of library -> version of JSON library -> version of Aduino IDE -> exact name of shield on ide (i used ESP32 DEV MODULE and DOIT ESP32 DEVKIT V2 with same results) thank you so much as always i can begin again from the start, with simple sketch that sends your message back. it works with nodemcu 8266 but seems not working with ESP32. I m not using nodemcu because it reset and restart every 22 minutes: i have to understand if it does always or only if i m using CTBOT.H, but this will be another problem to solve later.

shurillu commented 2 years ago

Hello konig87nikkinakki, thank you for using the library and for this feedback. The issue you pointed out is triggered because the examples are not updated to the last library architecture. In the previously versions of the library, the CTBot::getNewMessage() method return true if there is a new message. Now in the new releases, the CTBot::getNewMessage() method return a CTBotMessageType data type that could be:

   CTBotMessageNoData: an error has occurred
   CTBotMessageText  : the received message is a text
   CTBotMessageQuery : the received message is a query (from inline keyboards)

as you can see in the CTBot.h header file. So, the example must be modified: the row 50 from

    if (myBot.getNewMessage(msg) {

to

    if (myBot.getNewMessage(msg)== CTBotMessageText) {

Please, could try these changes? Let me know if this fix your issue, as I can modify all the examples.

Thanks, cheers

Stefano

konig87nikkinakki commented 2 years ago

Sorry but i downgraded to 2.1.6 and seeeems to work good. i ve read that msg.sender.id is no more a String but a int64_t or something like that. Question:

  if (myBot.getNewMessage(msg)) {

    myBot.sendMessage(msg.sender.id, "Il sistema funziona, il tuo ID è " + (String)msg.sender.id +
                              "  \n");
         }

with new version of library does it work to show my sender id? if it change (String)msg.sender.id it gives me error. i ve seen in your example something like that

   // String reply; 
  //  reply = (String)"Il sistema funziona " + SOMETHING_LIKE_USERNAME + (String)". Try LIGHT ON or LIGHT OFF.";    
  //  myBot.sendMessage(msg.sender.id, reply);

but it writes me "NULL" .

and then last question: what s new in new library? if i stay with 2.1.6 what problems could i have?

shurillu commented 2 years ago

There are a lot of updates that you need so is better to use the latest version. So try this:

You could try the CTBot v 3.0.0 in the v3.0.0 branch.

About your question, the 2.1.7 can handle int64 user IDs (https://github.com/shurillu/CTBot/releases/tag/2.1.7)

Cheers

Stefano

shurillu commented 2 years ago

Regarding the IDs, they are been always in a int format (now int64). If you want to convert an int64 to a string, you can include the Utilities.h header file (it is included in the library): inside there is a function called int64ToAscii()

// convert an int64 value to an ASCII string
// params
//   value: the int64 value
// returns
//   the ASCII string of the converted value 
String int64ToAscii(int64_t value);

Hoping it can help, cheers

Stefano

konig87nikkinakki commented 2 years ago

Thank you so much. in few hours i ll upgrade again to latest version and i ll try. Using 2.1.6 library i usually do something like that:

**SETUP:**
String ssid  = "MY SSID"    ; // REPLACE mySSID WITH YOUR WIFI SSID
String pass  = "MY WPA KEY"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "MY TOKEN"   ; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
long destinatario=123456789;  //my user id (sender id) that previously i ve found with some other bot online

    myBot.sendMessage(destinatario, "ESP32 è stato spento e riacceso, IP: " + (String)ilmioip[0] + "." + (String)ilmioip[1] + "." + (String)ilmioip[2] + "." + (String)ilmioip[3] + "\n");
    delay(100);
------

**LOOP:**
  if (myBot.getNewMessage(msg))
   {
    Serial.println("è arrivato un nuovo messaggio!");
    //myBot.sendMessage(msg.sender.id, "Il sistema funziona, il tuo ID è " + (String)msg.sender.id + "  \n");

    if ((msg.text.equalsIgnoreCase("COMANDI")) || (msg.text.equalsIgnoreCase("AIUTO")) || (msg.text.equalsIgnoreCase("TEST")))  {   
      myBot.sendMessage(msg.sender.id, "i comandi sono: ACCENDI, RESET, SPEGNI. Il tuo ID è " + (String)msg.sender.id + "  \n"); 
}

and it works fine! if i install latest library, should i convert int64 to ascii? when something doesnt work i m afraid it's my hardware problem so i m never sure where to find solutions. Thank you again, i ll install in another pc the configuration and versions that you suggested me in previous Post. i ll let you know

royaljelly2018 commented 2 years ago

unsubscribe

-VK

On Sat, Sep 4, 2021 at 1:23 AM konig87nikkinakki @.***> wrote:

Hi Shurillu, sorry for boring you. i use NODEMCU esp8266 and ESP32. i have a problem: only with ESP32 when i send a message to bot, it never receive and never goes into if (myBot.getNewMessage(msg)) { [....] }

while it worked with the same sketch on 12th august,and the same esp32. i had old version of Library, so i tried to upgrade but the error still remains. Only with ESP32. i ve bought it from AZ-DELIVERY, and i dont have another one to test. but i m pretty sure it's a Library or Telegram problem, even because esp32 send messages without any problem. So i thought i modified my sketch without remembering, and then i tried with sketch from library "LightBot". when i loaded to my ESP32 it read past messages that i previously sent to my bot, and then it answered JUST ONCE "Welcome null. Try LIGHT ON or LIGHT OFF." if i try to write commands LIGHT ON or other random text, it doesnt answer. It called me "NULL" so i m afraid it doesnt recognize. can someone please test with a esp32 and if working please tell me: -> version of library -> version of JSON library -> version of Aduino IDE -> exact name of shield on ide (i used ESP32 DEV MODULE and DOIT ESP32 DEVKIT V2 with same results) thank you so much as always i can begin again from the start, with simple sketch that sends your message back. it works with nodemcu 8266 but seems not working with ESP32. I m not using nodemcu because it reset and restart every 22 minutes: i have to understand if it does always or only if i m using CTBOT.H, but this will be another problem to solve later.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/shurillu/CTBot/issues/88, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJUWTNCGQ6KRYNDN5JZIWTTUAFKI5ANCNFSM5DMT3ZWQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

konig87nikkinakki commented 2 years ago

Hi shurillu, i tested with new library 2.1.7 and if (myBot.getNewMessage(msg)== CTBotMessageText) { well, it does work pretty good: at begin, it didnt, i had to reset several time ESP32 so if someone is reading with the same problem, reset a lot and power off several times. now it's working and i have last question: how to adapt to new library this code?

  if (myBot.getNewMessage(msg)== CTBotMessageText) //new mode to read new messages since ver2.1.7
   {
    myBot.sendMessage(msg.sender.id, "System is ON, received message, and sender id is " + (String)msg.sender.id + "  \n");
    Serial.println("new message is coming");
    }

i m asking you because it gives me error exit status 1 call of overloaded 'String(int64_t&)' is ambiguous

i had to remove otherwise it didnt run. thank you so much, if you need help to rebuild examples i can help.

shurillu commented 2 years ago

Hello konig87nikkinakki,

the Serial class does not have a costructor for int64 data type so when you call (String)msg.sender.id, the class can't handle the call bacause it doesn't know how to manage int64 (int?? long?? char?? unsigned?? etc.). You need a routine to convert int64 data type to strings. A way to do that is using the helper function that I pointed out in a previous post in this thread - int64ToAscii defined in the Utilities.h that comes inside the CTBot library.

Here the example:

#include "CTBot.h"
#include "Utilities.h" // <----- needed for the int64ToAscii() helper function
CTBot myBot;

String ssid = "MySSID";     // REPLACE mySSID WITH YOUR WIFI SSID
String pass = "MyPassword"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "MyToken";   // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN

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");
}

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

    // if there is an incoming text message...
    if (CTBotMessageText == myBot.getNewMessage(msg) ) {

        // generate the message for the sender
        myBot.sendMessage(msg.sender.id,
            "ID: " + int64ToAscii(msg.sender.id) +       // convert the ID (int64) to String
            "\nfirstName: " + msg.sender.firstName +
            "\nlastName: " + msg.sender.lastName +
            "\nusername: " + msg.sender.username +
            "\nMessage: " + msg.text +
            "\nChat ID: " + int64ToAscii(msg.group.id) + // convert the group (chat) ID (int64) to String
            "\nChat title: " + msg.group.title);
    }
    // wait 500 milliseconds
    delay(500);
}

As you can see, everytime I need to convert an int64 to a String (in this case for concatenating multiple strings) I call the helper function int64ToAscii.

Hoping this can help, cheers

Stefano