yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.45k stars 1.53k forks source link

How i can download media answered with a command #867

Closed ianmsfvenom closed 2 years ago

ianmsfvenom commented 3 years ago

I'm trying to make a command that when I reply to an image with it, I want the bot to download the answered image. I'm new to this telegram bot api and only know the basic commands

danielperez9430 commented 2 years ago

This is a little fast example:

bot.onText(/\/download/, async (msg) => {
    // Check if the user reply to a message
    if (msg.reply_to_message) {
        if (msg.reply_to_message.photo) {
            // Get the photo file_id
            let photoId = msg.reply_to_message.photo[msg.reply_to_message.photo.length - 1].file_id

            // Download the photo (file_id, folder path)
            bot.downloadFile(photoId, "./download/")
        }
        else if (msg.reply_to_message.sticker) {
            // Get the sticker file_id
            let stickerId = msg.reply_to_message.sticker.file_id
            // Get the sticker file
            bot.downloadFile(stickerId, "./download/")
        }
        else if (msg.reply_to_message.video) {
            // Get the video file_id
            let videoId = msg.reply_to_message.video.file_id

            // Download the video
            bot.downloadFile(videoId, "./download/")
        }
        else if (msg.reply_to_message.audio) {
            // Get the audio file_id
            let audioId = msg.reply_to_message.audio.file_id

            // Download the audio
            bot.downloadFile(audioId, "./download/")
        }
        else if (msg.reply_to_message.voice) {
            // Get the voiceId file_id
            let voiceId = msg.reply_to_message.voice.file_id

            // Get the voice file
            bot.downloadFile(voiceId, "./download/")
        }
        else if (msg.reply_to_message.document) {
            // Get the document file_id
            let documentId = msg.reply_to_message.document.file_id

            // Get the document file
            bot.downloadFile(documentId, "./download/")
        }
    } else {
        bot.sendMessage(msg.chat.id, "Reply to a message to download it")
    }
})