23 releases (15 breaking)

Uses new Rust 2021

0.16.0 Jan 1, 2022
0.15.0 Dec 27, 2021
0.14.0 Sep 6, 2021
0.13.0 Apr 7, 2021
0.1.0 Dec 23, 2018

#3 in #telegram

Download history 20/week @ 2021-10-06 42/week @ 2021-10-13 59/week @ 2021-10-20 84/week @ 2021-10-27 45/week @ 2021-11-03 15/week @ 2021-11-10 13/week @ 2021-11-17 16/week @ 2021-11-24 52/week @ 2021-12-01 32/week @ 2021-12-08 21/week @ 2021-12-15 21/week @ 2021-12-22 57/week @ 2021-12-29 73/week @ 2022-01-05 113/week @ 2022-01-12 47/week @ 2022-01-19

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

MIT license

1MB
21K SLoC

TGBOT

A full-featured Telegram Bot API client

CI Coverage Version Downloads Release Documentation Master Documentation Telegram Chat License

Installation

[dependencies]
tgbot = "0.16.0"

Versioning

This project adheres to ZeroVer

Example

Long polling:

use futures_util::future::BoxFuture;
use std::env;
use tgbot::{Api, Config, UpdateHandler};
use tgbot::longpoll::LongPoll;
use tgbot::methods::SendMessage;
use tgbot::types::{Update, UpdateKind};

struct Handler {
    api: Api,
}

impl UpdateHandler for Handler {
    type Future = BoxFuture<'static, ()>;

    fn handle(&self, update: Update) -> Self::Future {
        println!("got an update: {:?}\n", update);
        let api = self.api.clone();
        Box::pin(async move {
            if let UpdateKind::Message(message) = update.kind {
                if let Some(text) = message.get_text() {
                    let chat_id = message.get_chat_id();
                    let method = SendMessage::new(chat_id, text.data.clone());
                    api.execute(method).await.unwrap();
                }
            }
        })
    }
}

#[tokio::main]
async fn main() {
    let token = env::var("TGBOT_TOKEN").expect("TGBOT_TOKEN is not set");
    let api = Api::new(Config::new(token)).expect("Failed to create API");
    LongPoll::new(api.clone(), Handler { api }).run().await;
}

Webhook:

use futures_util::future::BoxFuture;
use tgbot::{UpdateHandler, types::Update, webhook};

struct Handler;

impl UpdateHandler for Handler {
    type Future = BoxFuture<'static, ()>;

    fn handle(&self, update: Update) -> Self::Future {
        Box::pin(async move {
            println!("got an update: {:?}\n", update);
        })
    }
}

#[tokio::main]
async fn main() {
    webhook::run_server(([127, 0, 0, 1], 8080), "/", Handler).await.unwrap();
}

See more examples in examples directory.

In order to run an example you need to create a .env file:

cp sample.env .env

Don't forget to change value of TGBOT_TOKEN and other variables if required.

Changelog

See CHANGELOG.md

Code of Conduct

See CODE_OF_CONDUCT.md.

LICENSE

The MIT License (MIT)

Dependencies

~8–12MB
~272K SLoC