witnessmenow / Universal-Arduino-Telegram-Bot

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

Send documents: bug resolved in sendMultipartFormDataToTelegram function (ESP32) #233

Open sfekilou opened 3 years ago

sfekilou commented 3 years ago

I wanted to use the function sendMultipartFormDataToTelegram to send a text file. So I made this call to the function: String response=bot->sendMultipartFormDataToTelegram("sendDocument","document","mesures.txt",X,Chat_id,size,isMoreDataAvailable,getNextByte,nullptr,nullptr); I didn't know what to put in the X chain. I tried with "image/png" instead of X: String response = bot-> sendMultipartFormDataToTelegram ("sendDocument","document","mesures.txt","image/png",chat_id,size,isMoreDataAvailable,getNextByte,nullptr,nullptr); Then, on executing, the function blocked. While debugging, I noticed that the function sendMultipartFormDataToTelegram of UniversalTelegramBot.cpp was blocking at the level of the call to client->println(F("")); I replaced this line by: client->print(F("\r\n")); (note: "print" instead of "println" !). It works now, I received my file. And it also works with "" instead of "image/png".

Note: you must add these functions in your main code:

File myFile; //on global memory

bool isMoreDataAvailable()
{
  return(myFile.available());
}

byte getNextByte()
{
  return((byte)myFile.read());
}

and open file before calling sendMultipartFormDataToTelegram function:

int size;
myFile=FFat.open("/mesures.txt","r"); //in my case the file is on the FFAT
size=myFile.size();
String response=bot->sendMultipartFormDataToTelegram("sendDocument", "document","mesures.txt","",chat_id,size,isMoreDataAvailable,getNextByte, nullptr, nullptr);
tofurind commented 2 years ago

I can confirm that! I was unable to send files on an ESP32 (sdk v3.3.5-1-g85c43024c). Sending a file gives response: [E][ssl_client.cpp:36] _handle_error(): [send_ssl_data():301]: (-80) UNKNOWN ERROR CODE (0050)

Solution (found here: https://www.gitmemory.com/issue/espressif/arduino-esp32/4666/808756058 ): Replace "client->println(F(""));" by "client->println();" in UniversalTelegramBot.cpp, because sending an empty string seems not to work as expected.

Now the ssl error is gone and files are actually sent.

tofurind commented 2 years ago

Update: Solved by #236. @sfekilou please use latest lib version and close this issue

cheers