【发布时间】:2019-02-25 21:04:20
【问题描述】:
我正在尝试使用文件的 url 并使用“sendDocument”方法发送 pdf 文件,问题是由于存储文件的服务器,我无法直接访问该文件。我尝试使用这篇文章中提供的答案: readFileSync from an URL for Twitter media - node.js
它可以工作,但文件作为“file.doc”发送。如果我将扩展名更改为 pdf,则它是正确的文件。我需要做任何额外的步骤来发送具有正确名称和扩展名的文件,还是有其他方法可以实现我所需要的?
编辑: 我用来获取 pdf 的代码看起来与我提供的帖子的答案中的代码一模一样:
function getImage(url, callback) {
https.get(url, res => {
// Initialise an array
const bufs = [];
// Add the data to the buffer collection
res.on('data', function (chunk) {
bufs.push(chunk)
});
// This signifies the end of a request
res.on('end', function () {
// We can join all of the 'chunks' of the image together
const data = Buffer.concat(bufs);
// Then we can call our callback.
callback(null, data);
});
})
// Inform the callback of the error.
.on('error', callback);
}
要发送文件我使用这样的东西:
getImage(url, function(err, data){
if(err){
throw new Error(err);
}
bot.sendDocument(
msg.chat.id,
data,
);
})
【问题讨论】:
-
你能添加你的示例代码吗!?
-
嗨,我在帖子中添加了一些代码
标签: javascript node.js telegram-bot