I am using node.js telegraf module to create a telegram bot.
我正在使用node.js Telegraf模块来创建一个电报机器人。
I am using the code below.
我正在使用下面的代码。
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);
更多回答
@StefanBecker Since it's a new user it might be worth to adress him the subtle way: Welcome to SO ;) Please read this article on how to ask a good question. This would include a proper description of what you are trying to achieve, your code (or the relevant snippets) as well as your efforts showing what you have tried so far and possible error messages. It is also advisable to provide a full MCVE..
@Stefan Becker因为它是一个新用户,所以可能值得以微妙的方式添加他的地址:欢迎来到So;)请阅读这篇关于如何提出好问题的文章。这将包括对您试图实现的目标、您的代码(或相关代码片段)以及您所做的努力的适当描述,其中显示了到目前为止您已经尝试过的内容和可能的错误消息。还建议提供完整的MCVE。
You can use axios to store the images. Assuming you have the file id as fileId
and the context as ctx
. I am storing the image at the path ${config.basePath}/public/images/profiles/${ctx.update.message.from.id}.jpg
您可以使用AXIOS来存储图像。假设您的文件ID为fileID,上下文为ctx。我将图像存储在Path${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 */)
});
})
})
How do I get the file id of the image sent for the 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
});
Don't forget to install axios as npm install axios --save
and require it as const axios = require('axios')
不要忘了以NPM的身份安装axios安装axios--将其保存为const axios=Required(‘axios’)
N.B. Don't publish the file links publicly as it exposes your bot token.
注意:不要公开发布文件链接,因为这会暴露您的机器人令牌。
$ npm install needle
var needle = require('needle');
...
needle.get(`https://api.telegram.org/bot1234:ABCD/getFile?file_id=${picture}`, function(error, response) {
if (!error && response.statusCode == 200)
console.log(response.body.result);
});
If you want to download the highest quality photo you can use this command for the high quality file id
let fileId = ctx.message.photo.pop().file_id;
如果你想下载最高质量的照片,你可以使用这个命令来获得高质量的文件id let fileID=ctx.Message.Photo.op().
When the user sends an image, the content of ctx.message
is as follows:
当用户发送图片时,ctx.Message的内容如下:
{
message_id: 111,
from: {...},
...
photo: [
{
file_id: 'XXXX',
file_unique_id: 'YYYY',
file_size: 1234,
width: 43,
height: 90
},
...
{
file_id: 'XXXX',
file_unique_id: 'YYYY',
file_size: 64202,
width: 606,
height: 1280
}
]
}
The last element of photo
array is the highest resolution
const { Telegraf } = require("telegraf");
const { message } = require("telegraf/filters");
const https = require("https");
const fs = require("fs");
bot.on(message("photo"), async (ctx) => {
let imageId = ctx.message.photo.pop().file_id;
ctx.telegram.getFileLink(imageId).then((link) => {
https.get(link, (response) =>
response.pipe(fs.createWriteStream(`path/img/${imageId}.jpeg`))
);
});
});
Use pop()
to get the last element, What we need from this element is file_id
更多回答
我是一名优秀的程序员,十分优秀!