Node.js 如何下载使用telegraf模块发送到电报机器人的文件或照片?
我正在使用node.js模块创建一个电报机器人 我正在使用下面的代码Node.js 如何下载使用telegraf模块发送到电报机器人的文件或照片?,node.js,telegraf,Node.js,Telegraf,我正在使用node.js模块创建一个电报机器人 我正在使用下面的代码 var picture = (context)ctx.message.photo[0].file_id; var photo = `https://api.telegram.org/bot1234-ABCD/getFile?file_id=${picture}`; console.log(photo.file_path); $npm安装针 var针=需要(“针”); ... 针头(`https://api.telegra
var picture = (context)ctx.message.photo[0].file_id;
var photo = `https://api.telegram.org/bot1234-ABCD/getFile?file_id=${picture}`;
console.log(photo.file_path);
$npm安装针
var针=需要(“针”);
...
针头(`https://api.telegram.org/bot1234:ABCD/getFile?file_id=${picture}`,函数(错误,响应){
如果(!error&&response.statusCode==200)
console.log(response.body.result);
});
$npm安装针
var针=需要(“针”);
...
针头(`https://api.telegram.org/bot1234:ABCD/getFile?file_id=${picture}`,函数(错误,响应){
如果(!error&&response.statusCode==200)
console.log(response.body.result);
});
您可以使用axios存储图像。假设文件id为fileId
,上下文为ctx
。我将图像存储在路径${config.basePath}/public/images/profiles/${ctx.update.message.from.id}.jpg
ctx.telegram.getFileLink(fileId).then(url => {
axios({url, responseType: 'stream'}).then(response => {
return new Promise((resolve, reject) => {
response.data.pipe(fs.createWriteStream(`${config.basePath}/public/images/profiles/${ctx.update.message.from.id}.jpg`))
.on('finish', () => /* File is saved. */)
.on('error', e => /* An error has occured */)
});
})
})
如何获取为bot发送的图像的文件id
bot.on('message', ctx => {
const files = ctx.update.message.photo;
// telegram stores the photos for different sizes
// here is a sample files result
// [ { file_id:
// 'AgADBAADgbAxG768CFDXChKUnJnzU5jjLBsABAEAAwIAA20AA_PFBwABFgQ',
// file_size: 29997,
// width: 320,
// height: 297 },
// { file_id:
// 'AgADBAADgbAxG768CFDXChKUnJnzU5jjLBsABAEAAwIAA3gAA_HFBwABFgQ',
// file_size: 80278,
// width: 580,
// height: 538 } ]
// })
// I am getting the bigger image
fileId = files[1].file_id
// Proceed downloading
});
不要忘记将axios安装为npm安装axios——将其保存为const axios=require('axios')
N.B.不要公开发布文件链接,因为它会公开您的bot令牌 您可以使用axios存储图像。假设文件id为fileId
,上下文为ctx
。我将图像存储在路径${config.basePath}/public/images/profiles/${ctx.update.message.from.id}.jpg
ctx.telegram.getFileLink(fileId).then(url => {
axios({url, responseType: 'stream'}).then(response => {
return new Promise((resolve, reject) => {
response.data.pipe(fs.createWriteStream(`${config.basePath}/public/images/profiles/${ctx.update.message.from.id}.jpg`))
.on('finish', () => /* File is saved. */)
.on('error', e => /* An error has occured */)
});
})
})
如何获取为bot发送的图像的文件id
bot.on('message', ctx => {
const files = ctx.update.message.photo;
// telegram stores the photos for different sizes
// here is a sample files result
// [ { file_id:
// 'AgADBAADgbAxG768CFDXChKUnJnzU5jjLBsABAEAAwIAA20AA_PFBwABFgQ',
// file_size: 29997,
// width: 320,
// height: 297 },
// { file_id:
// 'AgADBAADgbAxG768CFDXChKUnJnzU5jjLBsABAEAAwIAA3gAA_HFBwABFgQ',
// file_size: 80278,
// width: 580,
// height: 538 } ]
// })
// I am getting the bigger image
fileId = files[1].file_id
// Proceed downloading
});
不要忘记将axios安装为npm安装axios——将其保存为const axios=require('axios')
N.B.不要公开发布文件链接,因为它会公开您的bot令牌 不是a,f.ex什么是ctx?您收到了什么错误消息?@StefanBecker因为是新用户,所以可能值得用一种微妙的方式称呼他:欢迎使用SO;)请在网上阅读这篇文章。这将包括对您试图实现的目标、代码(或相关代码段)的正确描述,以及您迄今为止所做的努力,以及可能的错误消息。还建议提供完整的..而不是a,f.ex什么是ctx
?您收到了什么错误消息?@StefanBecker因为是新用户,所以可能值得用一种微妙的方式称呼他:欢迎使用SO;)请在网上阅读这篇文章。这将包括对您试图实现的目标、代码(或相关代码段)的正确描述,以及您迄今为止所做的努力,以及可能的错误消息。还建议提供完整的。。