Javascript将blob发送到telegram

Javascript send blob to telegram

本文关键字:telegram blob Javascript      更新时间:2023-09-26

我想用电报发送一份文件,我写了代码:

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);

电报响应:

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

Telegram期望文件所在的参数名为document,而不是file

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