Problem initializing the Telegram bot (SOLVED)

Don't waste your time. Read only post#21 and post#109 solved brilliantly by programmer Anthony_P
.
.
.
.
.
.
Greetings,

I'm testing a telegram BOT with a simple code that just configures it.

Right at the beginning, it asks for the initialization line that is like this:

String BOTtoken = "";

UniversalTelegramBot bot(BOTtoken, clientTCP);

But I intend that BOTtoken be a global String because I will inform the code later, sending it through .txt file

I'm using a espcam that takes a picture and sends it to telegram.

But it doesn't work because Telegram responds as unauthorized access because when initializing the code, the String BOTtoken is empty first.

It will only be 'filled' when the code goes to setup() because there it will read the String from a file stored in SPIFFS.

It is not a question of analyzing a code, but a theoretical question. How to inform at initialization a value of a String that will only be read in setup() ?

So what's the way to resolve this? Anybody know ?

Use hard coding in Setup.

What is hard coding ? ... I will search on google

Instead of using a string variable use characters.

Can you post an example ? Or any link that has an example ?

"characters", "abcdef",.....

I can't understand the Railroader colleague's suggestion

But can anyone comment ?

Meanwhile I keep looking for solutions on the internet

Can at least understand the problem after dozens of tests.

It's not what I wrote in post#1

The problem is the following:

If I try to read the fields strings[4] and strings[5] of the code snippet below, coming from inside the CONFIG.txt file that is in SPIFFS where i have 6 fields separated by comma, I can't connect to the Telegram BOT.

But if I enter the values ​​(see just below), then it works.

So my little knowledge tells me that the read is not bringing a String. Therefore I tried converting using String(strings[4]);

But it did not work.

Does anyone have any...try this ?

*dados is a global String

if(SPIFFS.exists("/CONFIG.txt"))  {
    myFile = SPIFFS.open("/CONFIG.txt", "r");
    if (myFile) {  
      while (myFile.available() > 0) {
         dados = myFile.readStringUntil('\n'); 
      }
      myFile.close();
     }    
    
    dados.toCharArray(array, 200);
    byte index = 0;
    ptr = strtok(array, ",");
    while (ptr != NULL)  {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
    }
   
    for (int n = 0; n < index; n++)   {
//      Serial.println(strings[n]);
    }

    xyz = strings[0];
    ztw = strings[1];
    uhx = strings[2];
    tpp = strings[3];
//    CHAT_ID = String(strings[4]);
//    BOTtoken = String(strings[5]);
    
    CHAT_ID = "123456";
    BOTtoken = "78910:BBHyfEJzNqtpB2F2NwXT1lDSKbu4Q1f3Cmo";

They are also global Strings

xyz
ztw
uhx
tpp
CHAT_ID
BOTtoken

What is the secret ?

If I type the values ​​of the two Strings, it works.

If I read the values ​​of the two Strings, it doesn't work

The values ​​are the same on the serial monitor.

Not knowing what was happening, I created a second .txt file called BOT and saved it inside SPIFFS. In it I only put the BOT Telegram code, note that I fill the String BOTtoken with it. And this String is declared as a global String.

if(SPIFFS.exists("/BOT.txt"))  {
    myFile = SPIFFS.open("/BOT.txt", "r");
    if (myFile) {  
      while (myFile.available()) {
         BOTtoken = myFile.readStringUntil('\n'); 
      }
      myFile.close();
     }    
   Serial.print("BOTtoken: ");Serial.println(BOTtoken);
  }

It still didn't work. But if I type doing BOTtoken = "ABSCDF:EFGHIJKKLMNOP";
it works like a beauty.

I've counted the letters several times. All the same. Where's the secret to it ?

I give up. All day stuck on this. Tomorrow I will try with another library, FastBot.

If I succeed I'll post it here.

Goodbye

Your problem with UniversalTelegramBot may be that it is expecting a reference to a constant string object:

UniversalTelegramBot(const String& token, Client &client)

That's not what you're giving it. Not sure there's a solution with that library for what you want to do. Maybe mod it so that it is just looking for a string. Beyond my pay grade.

Haven't looked at FastBot.

The BOTtoken String is used here, and the error I see on the serial monitor is 401.

WiFiClientSecure.h

clientTCP.println("POST /bot"+BOTtoken+"/sendPhoto HTTP/1.1");
    clientTCP.println("Host: " + String(myDomain));
    clientTCP.println("Content-Length: " + String(totalLen));
    clientTCP.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
    clientTCP.println();
    clientTCP.print(head);

If I type in setup() String BOTtoken = "DJAHSDG:HFJHJKSDHFSJKAHJFASKHJKSDLAHSLD";

It works

BOTtoken is declared as a global String.

But if I read it from SPIFFS it doesn't work, it gives this 401 error.

I already put: Serial.println("POST /bot"+BOTtoken+"/sendPhoto HTTP/1.1");

I noticed that the String read from SPIFFS is the same. I don't understand, it was supposed to work.

For anyone reading now.

I got an example code that sends a photo to a Telegram BOT (espcam). I'm just wanting to inform the BOTToken String by the WebServer doing UPLOAD of the .txt file. The code reads in setup() and assigns the value to the BOTtoken global String. It doesn't work, it gives a 401 error. But if I type the value of the String BOTtoken, it works.

I think I managed to solve

Please share your solution

It was 1 minute joy. But I managed to at least know exactly what my problem is, it took me 1 day and a half.

What I'm getting when reading from the spiffs .txt file is not a String, despite having exactly the same characters.

Look:

myFile = SPIFFS.open("/CONFIG.txt", "r");
    if (myFile) {  
      while (myFile.available()) {
         dados = myFile.readStringUntil('\n'); 
//         dados = myFile.readString(); 
      }
      myFile.close();
     }    
    
    dados.toCharArray(array, 200);
    byte index = 0;
    ptr = strtok(array, ",");
    while (ptr != NULL)  {
      campos[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
    }
   
    for (int n = 0; n < index; n++)   {
//      Serial.println(campos[n]);
    }

    ABC = campos[0];
    DEF = campos[1];
    GHI = campos[2];
    JKL = campos[3];
    CHAT_ID = campos[4];
//    BOT_TOKEN = campos[5];

Only the BOT_TOKEN matters.

It only works if I type the BOT_TOKEN outside this code snippet, like this:

BOT_TOKEN = "AVSBCFDVFBD..etc";

In this conversion, BOT_TOKEN = campos[5];
It's not giving me a String, and I already tried it like this:
BOT_TOKEN = String(campos[5]);

Nothing done.

Do you realize that I don't know how to do it? I know nothing. But I'm stubborn.

I bring more information

I'm using this library:

Does anyone have any ideas ?

Hello!

You've already posted a lot of posts and I'm a bit lost in all of this

Can you please, explain in juste a few sentences, what you would like to do?

Passing the ID_Bot from a file and so on... explain it all so we can help you