bytesnake / telebot

Write Telegram bots in Rust with Tokio and Futures
Other
212 stars 33 forks source link

How to receive a file? #59

Closed thomasfire closed 5 years ago

thomasfire commented 5 years ago

I found nothing about how to receive a file on event, like bot.unknown_cmd() or bot.new_cmd(). Is there any opportunity to receive a file from a user?

bytesnake commented 5 years ago

You can download a file following these steps:

see here -> https://stackoverflow.com/questions/31096358/how-do-i-download-a-file-or-photo-that-was-sent-to-my-telegram-bot

bytesnake commented 5 years ago

If you can get this working, please submit the example for anyone else :) Thanks!

thomasfire commented 5 years ago

I solved it, but the example is rather complicated, see https://github.com/thomasfire/teleprint/blob/ed53c584101d8f49cb79bf4dfd052b94e2506334/src/bot.rs at the bottom where get_stream is.

bytesnake commented 5 years ago

Yeah you don't have to call the Telegram Bot api directly for getting the download link, for example you could do:

bot.get_stream()
    .filter_map(|(bot, update)|) update.message.map(|x| (bot, x)))
    .and_then(|(bot, document)| {
        bot.get_file(document.file_id).send()
    })
    .and_then(|res| {
        FavouriteDownloader::download(res.file_path.unwrap())
    })

I haven't tested it, but the general concept is like this. You can filter at the beginning for your relevant information, then issue a get_file call to Telegram and finally call your downloader to begin an HTTP download process.

bytesnake commented 5 years ago

Please report if you still need help, thanks!