# Problem initializing the Telegram bot (SOLVED)

**URL:** <https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270>\
**Category:** General Guidance\
**Created:** [August 8, 2023, 1:39pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270 "2023-08-08T13:39:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 1:39pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/1 "2023-08-08T13:39:49Z")

</div>

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:

```arduino
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 ?

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [August 8, 2023, 1:55pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/2 "2023-08-08T13:55:28Z")

</div>

Use hard coding in Setup.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 2:16pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/3 "2023-08-08T14:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [August 8, 2023, 2:34pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/4 "2023-08-08T14:34:42Z")

</div>

Instead of using a string variable use characters.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 2:37pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/5 "2023-08-08T14:37:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [August 8, 2023, 2:56pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/6 "2023-08-08T14:56:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 3:54pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/7 "2023-08-08T15:54:05Z")

</div>

I can't understand the Railroader colleague's suggestion

But can anyone comment ?

Meanwhile I keep looking for solutions on the internet

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 6:30pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/8 "2023-08-08T18:30:56Z")

</div>

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

```arduino
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";

```

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 6:35pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/9 "2023-08-08T18:35:55Z")

</div>

They are also global Strings

```arduino
xyz
ztw
uhx
tpp
CHAT_ID
BOTtoken

```

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 7:43pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/10 "2023-08-08T19:43:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 9:54pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/11 "2023-08-08T21:54:41Z")

</div>

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.

```arduino
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 ?

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 8, 2023, 11:04pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/12 "2023-08-08T23:04:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaveEvans](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/daveevans/32/340502_2.png) [@DaveEvans](https://forum.arduino.cc/u/DaveEvans)\
**Post date:** [August 9, 2023, 5:05am UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/13 "2023-08-09T05:05:10Z")

</div>

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

```arduino
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.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 9, 2023, 11:10am UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/14 "2023-08-09T11:10:51Z")

</div>

> [@DaveEvans](#):
>
> Beyond my pay grade

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

WiFiClientSecure.h

```arduino
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.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 9, 2023, 11:17am UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/15 "2023-08-09T11:17:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 9, 2023, 5:02pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/16 "2023-08-09T17:02:36Z")

</div>

I think I managed to solve

---

<div class="post-metadata">

**Author:** ![DaveEvans](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/daveevans/32/340502_2.png) [@DaveEvans](https://forum.arduino.cc/u/DaveEvans)\
**Post date:** [August 9, 2023, 6:10pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/17 "2023-08-09T18:10:26Z")

</div>

Please share your solution

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 9, 2023, 6:33pm UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/18 "2023-08-09T18:33:42Z")

</div>

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:

```arduino
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.

---

<div class="post-metadata">

**Author:** ![sergioarduino](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sergioarduino](https://forum.arduino.cc/u/sergioarduino)\
**Post date:** [August 10, 2023, 11:19am UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/19 "2023-08-10T11:19:00Z")

</div>

I bring more information

I'm using this library:

> **[GitHub - GyverLibs/FastBot: Многофункциональная быстрая библиотека для Телеграм...](https://github.com/GyverLibs/FastBot/tree/main)**
>
> main

Does anyone have any ideas ?

---

<div class="post-metadata">

**Author:** ![Anthony\_P](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/anthony_p/32/775266_2.png) [@Anthony\_P](https://forum.arduino.cc/u/Anthony_P)\
**Post date:** [August 10, 2023, 11:36am UTC](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270/20 "2023-08-10T11:36:50Z")

</div>

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

[Next page](https://forum.arduino.cc/t/problem-initializing-the-telegram-bot-solved/1156270.md?page=2)
