cotestatnt / AsyncTelegram2

Powerful, flexible and secure Arduino Telegram BOT library. Hardware independent, it can be used with any MCU capable of handling an SSL connection.
MIT License
83 stars 25 forks source link

Feature request: send a text or CSV file as attachment #96

Closed henkjannl closed 1 year ago

henkjannl commented 1 year ago

Hi,

I was looking for a way to submit a feature request but I cannot find it, so sorry for posting this as an issue.

I would like to be able to send a logfile from my room thermostat (https://github.com/henkjannl/RoomThermostat) to my computer over Telegram. How difficult would it be for me to add a function similar to sendPhotoByFile to get this done? Can anyone provide hints on how to approach this?

Help is appreciated.

HenkJan

cotestatnt commented 1 year ago

Hi @henkjannl . This feature should be already implemented, but I've never tried it to be honest. I need to check only which type of content is necessary to set in the headers of request because at the moment CSV files are not included.

As soon as I have some free time I try to add this small change and prepare an example

henkjannl commented 1 year ago

Thanks for the fast reply. I'll try if ZIP or PDF works, or try to add a similar combination. Keep you posted! In the Telegram Desktop version it is possible to send CSV files, so there should be a possibility in the Telegram API.

cotestatnt commented 1 year ago

Good morning @henkjannl I've just added the feature requested. You need to update to the library release 2.1.8, because some code changes was needed in source code.

I've prepared also a new example with some cases of use.

henkjannl commented 1 year ago

Wow that's excellent thank you. I'm not able to test now since I have quite a heavy flu. Really grateful.

henkjannl commented 1 year ago

Works wonderfully. Many thanks @cotestatnt!! Very much appreciated

henkjannl commented 1 year ago

Hi, Sending CSV files works great. Thanks for implementing!

I have an additional question. I have implemented a temperature logger with an inline keyboard with two keys: one to send the logfile and the other to clear the logfile.

The first callback routine is implemented like this:

void onSendLog(const TBMessage &queryMsg){
  File file = SPIFFS.open(LOG_FILE, FILE_WRITE);
  file.print("\"Time\",\"Temperature\"\n");

  struct tm * localTime;
  for (auto const& datapoint : TemperatureLog) {
      localTime = localtime(& (datapoint.first) );
      file.printf("%04d-%02d-%02d %02d:%02d:%02d,%.6f\n", 
        localTime->tm_year+1900, localTime->tm_mon+1, localTime->tm_mday,
        localTime->tm_hour, localTime->tm_min, localTime->tm_sec, 
        datapoint.second ); 
  }

  file.close();

  file = SPIFFS.open(LOG_FILE, "r");
  if (file) {
    myBot.sendDocument(queryMsg, file, file.size(), AsyncTelegram2::DocumentType::CSV, file.name());
    file.close();
    myBot.sendMessage(queryMsg, "Log file sent", inlineKeyboard);          
  }
  else
    Serial.println("Can't open the file. Upload \"data\" folder to filesystem");
};

Steps are:

However, instead of sending the "Log file sent" notification, the document in step 2 is sometimes sent a second time. But the "Log file sent" message is never sent.

Do I need to clear a buffer or wait for something before I can send a message after sending a document? Or do I need to make a buffer larger?

cotestatnt commented 1 year ago

Hi @henkjannl! I'm sorry for big delay, but I was very busy in this last months. I tried to simulate a sketch similar to yours and I also encountered the same malfunction.

After some debugging, I noticed that after sending a stream (like a file from the filesystem), the connection was always closed by the server because I forgot to put the "Connection: keep-alive" header in the HTTPS request. This introduces the need to restart the connection, which takes a few seconds and therefore sometimes the message to be sent was not processed correctly.

I corrected the request by inserting the header in the new release and now the second message is always sent as it should.

henkjannl commented 1 year ago

Hi Tolentino, Thanks very much for solving the issue. I will download and try the update this weekend. I'm using your library in various projects now and it performs really well. Thanks! Kind regards, HenkJan

henkjannl commented 1 year ago

Sorry for not closing the issue, it works like a charm. Thanks!!