Skip to content

Add local TBA file downloading#1173

Merged
shdwchn10 merged 5 commits intoteloxide:masterfrom
LasterAlex:add-local-file-downloading
Mar 18, 2026
Merged

Add local TBA file downloading#1173
shdwchn10 merged 5 commits intoteloxide:masterfrom
LasterAlex:add-local-file-downloading

Conversation

@LasterAlex
Copy link
Copy Markdown
Collaborator

Inspired by https://t.me/teloxide/22547

Made it just because it was quick and I could, if it seems useful - would be glad to see this merged, but it's not very important

Code snippet to test this:

use std::str::FromStr;

use teloxide::{
    dispatching::{UpdateFilterExt, UpdateHandler},
    net::Download,
    prelude::*,
};

type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;

async fn download_document(bot: Bot, message: Message) -> HandlerResult {
    if let Some(document) = message.document() {
        let file = bot.get_file(document.file.id.clone()).await?;
        let mut dest = tokio::fs::File::create("test.txt").await?;
        bot.download_file(&file.path, &mut dest).await?;

        assert!(tokio::fs::read_to_string("test.txt").await.is_ok());

        bot.send_message(message.chat.id, "Downloaded!").await?;

        tokio::fs::remove_file("test.txt").await?;
    } else {
        bot.send_message(message.chat.id, "Not a document").await?;
    }
    Ok(())
}

fn handler_tree() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
    dptree::entry().branch(Update::filter_message().endpoint(download_document))
}

#[tokio::main]
async fn main() {
    let bot = Bot::from_env().set_api_url(reqwest::Url::from_str("http://localhost:8081").unwrap());

    Dispatcher::builder(bot, handler_tree())
        .enable_ctrlc_handler()
        .build()
        .dispatch()
        .await;
}

Cargo.toml:

[dependencies]
teloxide = { git = "https://github.com/LasterAlex/teloxide", branch = "add-local-file-downloading"}
tokio = { version = "1.38", features = ["rt-multi-thread", "macros"] }
reqwest = "0.12.7"

Just send a .txt file to this bot

@teloxidebot
Copy link
Copy Markdown
Collaborator

r? @hirrolot

(teloxidebot has picked a reviewer for you, use r? to override)

@teloxidebot teloxidebot added C-core crate: teloxide-core S-waiting-on-review Status: Awaiting review from the assignee labels Sep 22, 2024
@LasterAlex LasterAlex changed the title Added local TBA file downloading Add local TBA file downloading Sep 22, 2024
@shdwchn10 shdwchn10 self-requested a review February 11, 2025 08:54
Copy link
Copy Markdown
Member

@shdwchn10 shdwchn10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small tweaks.

P.S. Rebase it so PR can be merged into the master

Comment thread crates/teloxide-core/src/bot/download.rs Outdated
Comment thread crates/teloxide-core/src/bot/download.rs Outdated
@shdwchn10 shdwchn10 removed the S-waiting-on-review Status: Awaiting review from the assignee label Feb 11, 2025
@LasterAlex LasterAlex force-pushed the add-local-file-downloading branch from a45780c to 0bfcd9d Compare March 16, 2026 20:50
@LasterAlex
Copy link
Copy Markdown
Collaborator Author

Missed it, lol, all done now

@LasterAlex LasterAlex requested a review from shdwchn10 March 16, 2026 20:51
@LasterAlex
Copy link
Copy Markdown
Collaborator Author

The hell...

@LasterAlex LasterAlex force-pushed the add-local-file-downloading branch from 0bfcd9d to fb70bfe Compare March 16, 2026 20:58
@teloxidebot teloxidebot added the A-tba-errors Area: representation of telegram bot API errors label Mar 16, 2026
@LasterAlex
Copy link
Copy Markdown
Collaborator Author

Finally, missed a breaking change

Copy link
Copy Markdown
Member

@shdwchn10 shdwchn10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick

Comment thread crates/teloxide-core/CHANGELOG.md Outdated
Comment thread CHANGELOG.md Outdated
LasterAlex and others added 2 commits March 18, 2026 15:43
Co-authored-by: Andrey Brusnik <dev@shdwchn.io>
Co-authored-by: Andrey Brusnik <dev@shdwchn.io>
@LasterAlex LasterAlex requested a review from shdwchn10 March 18, 2026 13:43
Copy link
Copy Markdown
Member

@shdwchn10 shdwchn10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks

@shdwchn10 shdwchn10 enabled auto-merge March 18, 2026 13:44
@shdwchn10 shdwchn10 added this pull request to the merge queue Mar 18, 2026
Merged via the queue into teloxide:master with commit c7e1438 Mar 18, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tba-errors Area: representation of telegram bot API errors C-core crate: teloxide-core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants