6 releases

0.8.0 Nov 14, 2020
0.7.0 Mar 21, 2020
0.6.3 Jul 17, 2019
0.6.1 Feb 17, 2018
0.6.0 Jan 9, 2018

#41 in #telegram-api

Download history 174/week @ 2024-08-28 152/week @ 2024-09-04 142/week @ 2024-09-11 177/week @ 2024-09-18 197/week @ 2024-09-25 136/week @ 2024-10-02 28/week @ 2024-10-09 104/week @ 2024-10-16 112/week @ 2024-10-23 137/week @ 2024-10-30 56/week @ 2024-11-06 108/week @ 2024-11-13 113/week @ 2024-11-20 168/week @ 2024-11-27 206/week @ 2024-12-04 486/week @ 2024-12-11

982 downloads per month
Used in 6 crates (2 directly)

MIT license

210KB
5.5K SLoC

Rust Telegram Bot Library

Build Status Tests Tests License Crates.io

Documentation: Latest crates.io version master

A library for writing your own Telegram bots. More information here. Official API here.

Example

Here is a simple example (see example/simple.rs):

use std::env;

use futures::StreamExt;
use telegram_bot::*;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let token = env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set");
    let api = Api::new(token);

    // Fetch new updates via long poll method
    let mut stream = api.stream();
    while let Some(update) = stream.next().await {
        // If the received update contains a new message...
        let update = update?;
        if let UpdateKind::Message(message) = update.kind {
            if let MessageKind::Text { ref data, .. } = message.kind {
                // Print received text message to stdout.
                println!("<{}>: {}", &message.from.first_name, data);

                // Answer message with "Hi".
                api.send(message.text_reply(format!(
                    "Hi, {}! You just wrote '{}'",
                    &message.from.first_name, data
                )))
                .await?;
            }
        }
    }
    Ok(())
}

You can find a bigger examples in the examples.

Usage

This library is available via crates.io. In order to use it, just add this to your Cargo.toml:

telegram-bot = "0.7"

The library allows you to do E2E-testing of your bot easily: just specify TELEGRAM_API_URL environment variable to point to your fake Telegram test server. A lot of diagnostic information can be collected with tracing framework, see example/tracing.rs).

Collaboration

Yes please! Every type of contribution is welcome: Create issues, hack some code or make suggestions. Don't know where to start? Good first issues are tagged with up for grab.

Dependencies

~1.1–2MB
~43K SLoC