witnessmenow / Universal-Arduino-Telegram-Bot

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

Recieving SSL - An invalid SSL record was received #242

Open kahlenberg opened 3 years ago

kahlenberg commented 3 years ago

Hi, I changed the exp32-cam example, I can receive text messages but not photo. When I send a /photo message, in serial console I receive SSL - An invalid SSL record was received error. I think it is about the length of the messages but I could not find any hit. Can you have a look ? Thanks.


String BOTtoken = "XX--XX--XX";
WiFiClientSecure secureClientTCP;
UniversalTelegramBot bot(BOTtoken, secureClientTCP);

void setup () {
// WiFi settings
// Serial settings
  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  ESP_LOGI(TAG, "Number of messages: %d", numNewMessages);
  while (numNewMessages) {
    ESP_LOGI(TAG, "Got response....%d", numNewMessages);
    handleNewMessages(numNewMessages);
    numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  }

  ESP_LOGI(TAG, "Sleeping..");
  esp_deep_sleep_start();
}

void loop () {}

String sendPhotoTelegram() {
  const char* myDomain = "api.telegram.org";
  String getAll = "";
  String getBody = "";

  camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();
  if (!fb) {
    ESP_LOGE(TAG, "Camera capture failed");
    delay(1000);
    ESP.restart();
    return "Camera capture failed";
  }

  ESP_LOGI(TAG, "Connect to %s\n", myDomain);

  if (secureClientTCP.connect(myDomain, 443)) {
    ESP_LOGI(TAG, "Connection successful");
    uint16_t imageLen = fb->len;

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

    uint8_t *fbBuf = fb->buf;
    size_t fbLen = fb->len;
    for (size_t n = 0; n < fbLen; n = n + 1024) {
      if (n + 1024 < fbLen) {
        secureClientTCP.write(fbBuf, 1024);
        fbBuf += 1024;
      }
      else if (fbLen % 1024 > 0) {
        size_t remainder = fbLen % 1024;
        secureClientTCP.write(fbBuf, remainder);
      }
    }

    esp_camera_fb_return(fb);

    int waitTime = 10000;   // timeout 10 seconds
    long startTimer = millis();
    boolean state = false;

    while ((startTimer + waitTime) > millis()) {
      Serial.print(".");
      delay(100);
      while (secureClientTCP.available()) {
        char c = secureClientTCP.read();

        if (state == true) getBody += String(c);
        if (c == '\n') {
          if (getAll.length() == 0) state = true;
          getAll = "";
        }
        else if (c != '\r')
          getAll += String(c);
        startTimer = millis();
      }
      if (getBody.length() > 0) break;
    }
    secureClientTCP.stop();
    ESP_LOGI(TAG, "%s", getBody.c_str());
  }
  else {
    getBody = "Connection to api.telegram.org failed.";
    ESP_LOGI(TAG, "%s", getBody.c_str());
  }
  return getBody;
}

void handleNewMessages(int numNewMessages) {
  ESP_LOGI(TAG, "Handle New Messages: %d\n", numNewMessages);

  for (int i = 0; i < numNewMessages; i++) {

    // Print the received message
    String text = bot.messages[i].text;
    String fromName = bot.messages[i].from_name;

    if (text == "/photo") {
      ESP_LOGI(TAG, "New photo request. Preparing photo");
      sendPhotoTelegram();
    }
}