Skip to content

Conversation

@LasterAlex
Copy link
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
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
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

destination: &'dst mut (dyn AsyncWrite + Unpin + Send),
) -> Self::Fut<'dst> {
let is_localhost = match &self.api_url.host_str() {
Some(host) => ["localhost", "127.0.0.1"].contains(host),
Copy link
Member

Choose a reason for hiding this comment

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

Add ::1 address too

{
let mut src_file = File::open(path).await?;

let mut buffer = [0; 1024];
Copy link
Member

Choose a reason for hiding this comment

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

1K buffer is too small, it would be faster with 128 * 1024 buffer

@shdwchn10 shdwchn10 removed the S-waiting-on-review Status: Awaiting review from the assignee label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-core crate: teloxide-core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants