witnessmenow / Universal-Arduino-Telegram-Bot

Use Telegram on your Arduino (ESP8266 or Wifi-101 boards)
MIT License
1.09k stars 302 forks source link

EEPROM updateToken() #224

Open g-Romano opened 3 years ago

g-Romano commented 3 years ago

Hi I've already read a tip to solve my problem here, but I'm not very experienced and can't apply it. I load the botToken value from the EEPROM into the setup(). But I have to set the botToken value before executing the program. The suggestion was to use update token method. I have implemented it as you can see in the setup(), void updateToken(String botToken);. But that doesn't work, could you help me? Thank you very much in advance


#include <ESP8266WiFi.h>
  [...]
  String defaultChatId;
  String botToken;
  WiFiClientSecure client;
  UniversalTelegramBot bot(botToken, client);
  [...]

void setup()
{
  [...]
  for (int i = 96; i < 160; ++i)
  {
    botToken += char(EEPROM.read(i));
  }
  Serial.print("TOKEN: ");
  Serial.println(botToken);
  botToken = botToken.c_str();
  void updateToken(String botToken);

  for (int i = 160; i < 176; ++i)
  {
    defaultChatId += char(EEPROM.read(i));
  }
  Serial.print("ChatId: ");
  Serial.println(defaultChatId);
  defaultChatId = defaultChatId.c_str();
  [...]
}

void handleNewMessages(int numNewMessages) {
  for (int i=0; i<numNewMessages; i++) {
  [...]
  }
}

void loop() {
  if ((WiFi.status() == WL_CONNECTED))
  {
      long now = millis();
      if(now >= checkTelegramDueTime) {
        //Serial.println("---- Checking Telegram -----");
        int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        while(numNewMessages)
        {
          Serial.println("Bot recieved a message");
          handleNewMessages(numNewMessages);
          numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        }
        checkTelegramDueTime = now + checkTelegramDelay;
      }
      now = millis();
      [...]
  }
  else {}
}
[...]```
mmerttccolakk commented 2 years ago

you can use this bot.updateToken(String(bot_token));