使用电报机器人发送 pdf 文件 api
Send a pdf file using telegram bot api
我正在尝试使用文件的 url 和方法 "sendDocument" 发送 pdf 文件,问题是我无法直接访问该文件,因为它存储在服务器上.我尝试使用此 post 中提供的答案:
有效,但文件发送为 "file.doc"。如果我将扩展名更改为 pdf,它就是正确的文件。我需要做任何额外的步骤来发送具有正确名称和扩展名的文件,还是有其他方法可以实现我需要的?
编辑: 我用来获取 pdf 的代码看起来与 post 我提供了:
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,
);
})
您可以使用此代码指定文件名和文件类型:
const fileOptions = {
// Explicitly specify the file name.
filename: 'mypdf.pdf',
// Explicitly specify the MIME type.
contentType: 'application/pdf',
};
全功能:
getImage("https://your.url/yourfile.pdf", function(err, data){
if(err){
throw new Error(err);
}
const fileOptions = {
// Explicitly specify the file name.
filename: 'mypdf.pdf',
// Explicitly specify the MIME type.
contentType: 'application/pdf',
};
bot.sendDocument(msg.chat.id, data, {}, fileOptions);
});
注意:您必须提供一个空对象({})
来代替其他电报查询选项,如果您没有查询选项来指定。例如,
// 错误!
// 'fileOptions' 将作为额外的 Telegram 查询选项!!!
bot.sendAudio(chatId, data, fileOptions);
// 正确!
bot.sendAudio(chatId, data, {}, fileOptions);
更多信息在这里:
https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files
找到解决方案。我正在使用远程机器人 api(抱歉没有提到那个细节,但我不知道,我没有做这个项目)。
我使用以下行发送文件:
bot.sendDocument(chat_id, data, {fileName: 'file.pdf'});
我正在尝试使用文件的 url 和方法 "sendDocument" 发送 pdf 文件,问题是我无法直接访问该文件,因为它存储在服务器上.我尝试使用此 post 中提供的答案:
有效,但文件发送为 "file.doc"。如果我将扩展名更改为 pdf,它就是正确的文件。我需要做任何额外的步骤来发送具有正确名称和扩展名的文件,还是有其他方法可以实现我需要的?
编辑: 我用来获取 pdf 的代码看起来与 post 我提供了:
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,
);
})
您可以使用此代码指定文件名和文件类型:
const fileOptions = {
// Explicitly specify the file name.
filename: 'mypdf.pdf',
// Explicitly specify the MIME type.
contentType: 'application/pdf',
};
全功能:
getImage("https://your.url/yourfile.pdf", function(err, data){
if(err){
throw new Error(err);
}
const fileOptions = {
// Explicitly specify the file name.
filename: 'mypdf.pdf',
// Explicitly specify the MIME type.
contentType: 'application/pdf',
};
bot.sendDocument(msg.chat.id, data, {}, fileOptions);
});
注意:您必须提供一个空对象({})
来代替其他电报查询选项,如果您没有查询选项来指定。例如,
// 错误! // 'fileOptions' 将作为额外的 Telegram 查询选项!!!
bot.sendAudio(chatId, data, fileOptions);
// 正确!
bot.sendAudio(chatId, data, {}, fileOptions);
更多信息在这里: https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files
找到解决方案。我正在使用远程机器人 api(抱歉没有提到那个细节,但我不知道,我没有做这个项目)。
我使用以下行发送文件:
bot.sendDocument(chat_id, data, {fileName: 'file.pdf'});