我正在使用node.js telegraf模块创建一个电报机器人。
我正在使用下面的代码。
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);
Run Code Online (Sandbox Code Playgroud)
    您可以使用 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 */)
                });
            })
})
Run Code Online (Sandbox Code Playgroud)
如何获取为机器人发送的图像的文件 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
});
Run Code Online (Sandbox Code Playgroud)
不要忘记安装 axios asnpm install axios --save并要求它为const axios = require('axios')
注意:不要公开发布文件链接,因为它会暴露您的机器人令牌。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           11448 次  |  
        
|   最近记录:  |