我有一个字符串,我想通过电报机器人发送,但不是作为消息(它很长)而是作为文件发送。但是,我在创建此文件并将其上传到 Telegram 时遇到了一些问题(因为我需要使用 API 文档https://core.telegram.org/bots/api#sending-files 中指定的 multipart/form-data 发布文件)。受https://stackoverflow.com/a/22858914/4869973 的启发,我尝试了以下操作:
var file = new Blob([enc_data], {type: 'text/plain'});
var formData = new FormData();
formData.append('chat_id', '<id>');
formData.append('document', file);
var request = new XMLHttpRequest();
request.open('POST', 'https://api.telegram.org/bot<token>/sendDocument');
request.send(FormData);
但我只得到一个通用错误POST https://api.telegram.org/bot<token>/sendDocument 400 (Bad Request) 我从未使用过 XMLHttpRequest 所以我可能搞砸了它的使用,但我找不到任何解决方案。欢迎使用替代方案(可能使用纯 js)。
MM们
相关分类