How to Send a String as a File via a Telegram Bot

Attempting to send a long text as a file using a Telegram bot triggers a multipart upload error (400). How can I fix this?

const textData = "Example long content...";
const payload = new FormData();
payload.append("targetChat", "YOUR_CHAT_ID");
payload.append("fileDoc", new Blob([textData], {type: "text/plain"}));

fetch("https://api.telegram.org/bot<token>/sendDocument", {
  method: "POST",
  body: payload
});

hey, try adding a filename as a third arg to your append, like payload.append(‘fileDoc’, new Blob([textData], {type:‘text/plain’}), ‘data.txt’). it worked for me. cheers

In my experience the multipart upload error was often related to metadata being insufficient for the file upload. I solved a similar problem by creating a File instance explicitly instead of using a Blob. Using File not only attaches a proper filename but also ensures the metadata is fully set, which can help Telegram’s API correctly handle the upload. For example, creating a File with new File([textData], ‘output.txt’, { type: ‘text/plain’ }) resolved the issue in a project I worked on. Additionally, verifying that the text size does not exceed any Telegram limits can also prevent such errors.