33 releases

0.13.0 Aug 15, 2024
0.12.2 Feb 15, 2023
0.12.0 Jan 17, 2023
0.11.3 Nov 28, 2022
0.2.0 Feb 25, 2020

#155 in Web programming

Download history 2769/week @ 2024-07-20 3212/week @ 2024-07-27 2362/week @ 2024-08-03 3605/week @ 2024-08-10 2990/week @ 2024-08-17 3675/week @ 2024-08-24 3434/week @ 2024-08-31 2946/week @ 2024-09-07 3177/week @ 2024-09-14 3592/week @ 2024-09-21 3656/week @ 2024-09-28 2615/week @ 2024-10-05 2860/week @ 2024-10-12 3427/week @ 2024-10-19 2872/week @ 2024-10-26 2785/week @ 2024-11-02

12,356 downloads per month
Used in 53 crates (46 directly)

MIT license

2MB
35K SLoC

Rust 30K SLoC // 0.0% comments Rusty Object Notation 4.5K SLoC // 0.0% comments SQL 8 SLoC

See README.md at the root of the repository.


lib.rs:

A full-featured framework that empowers you to easily build Telegram bots using Rust. It handles all the difficult stuff so you can focus only on your business logic. Currently, version 7.0 of Telegram Bot API is supported.

For a high-level overview, see our GitHub repository.

[examples/throw_dice.rs]

use teloxide::prelude::*;

pretty_env_logger::init();
log::info!("Starting throw dice bot...");

let bot = Bot::from_env();

teloxide::repl(bot, |bot: Bot, msg: Message| async move {
    bot.send_dice(msg.chat.id).await?;
    Ok(())
})
.await;

Working with Updates and Messages

There is a great number of update kinds and message kinds to work with! Usually it's essential to filter specific ones and process them in handler functions. Teloxide provides some filter methods for Update and Message types in UpdateFilterExt and MessageFilterExt traits respectively. In addition to filtering, these methods will inject the appropriate type into your handler functions. For instance, if you use Update::filter_message, the instance of the Message will be available as a parameter for your handler functions. Similarly the use of Message::filter_text will inject a String into the context.

Moreover, filter_map function can inject some dependencies according to the schema flow. More in the example below!

Here is a quick example (filter text message and inject it's text into the handler function):

use teloxide::{prelude::*, types::User};

pub type Error = Box<dyn std::error::Error + Send + Sync>;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let bot = Bot::from_env();
    let schema = Update::filter_message()
        /*
           Inject the `User` object representing the author of an incoming
           message into every successive handler function (1)
        */
        .filter_map(|update: Update| update.from().cloned())
        .branch(
            /*
               Use filter_text method of MessageFilterExt to accept
               only textual messages. Others will be ignored by this handler (2)
            */
            Message::filter_text().endpoint(process_text_message),
        );

    Dispatcher::builder(bot, schema).build().dispatch().await;
    Ok(())
}

/// Replies to the user's text messages
async fn process_text_message(bot: Bot, user: User, message_text: String) -> Result<(), Error> {
    /*
       The id of a chat with a user is the same as his telegram_id
       from the bot's perspective.

       Injected dependencies:
       - Bot is provided by the Dispatcher::dispatch
       - User is provided by the (1)
       - String is provided by the (2)
    */
    bot.send_message(user.id, format!("Hi! You sent: {message_text}"));
    Ok(())
}

Dependencies

~10–30MB
~484K SLoC