Search code examples
javascripttelegramtelegram-bot

Javascript send blob to telegram


I want to send a document in a telegram, i wrote the code:

function send(file) {
    var formData = new FormData();
    formData.append('file', file, "2.txt");

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://api.telegram.org/bot' + token + '/sendDocument?chat_id=' + id, true);

    xhr.send(formData);
}
var blob = new Blob(['hello'], {type: 'plain/text'});
console.log(blob)
blob.lastModifiedDate = new Date();
blob.name = '2.txt';
send(blob);

Telegramm response:

{"ok":false,"error_code":400,"description":"[Error]: Bad Request: there is no document in request"}

Solution

  • The parameter Telegram is expecting the file to be in is named document, not file.

    function send(file) {
      var formData = new FormData();
      formData.append('document', file, '2.txt');
    
      // the rest of your code
    }