#telegram #bot #chat #api

pw-telegram-bot-raw-fork

Telegram Bot API types

3 releases

0.9.2 May 29, 2022
0.9.1 May 29, 2022
0.9.0 Apr 14, 2022

#662 in Web programming

Download history 17/week @ 2022-11-28 31/week @ 2022-12-05 43/week @ 2022-12-12 40/week @ 2022-12-19 25/week @ 2022-12-26 10/week @ 2023-01-02 30/week @ 2023-01-09 15/week @ 2023-01-16 38/week @ 2023-01-23 51/week @ 2023-01-30 15/week @ 2023-02-06 42/week @ 2023-02-13 40/week @ 2023-02-20 22/week @ 2023-02-27 40/week @ 2023-03-06 27/week @ 2023-03-13

146 downloads per month
Used in 4 crates (via pw-telegram-bot-fork)

MIT license

220KB
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.4–2.2MB
~49K SLoC