tgbot 0.7.0

A Telegram Bot library
Documentation

TGBOT

A full-featured Telegram Bot API client

CI Coverage Version Downloads Release Documentation Master Documentation Telegram Chat License

Installation

[dependencies]
tgbot = "0.7.0"

Example

Long polling:

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

struct Handler {
    api: Api,
}

#[async_trait]
impl UpdateHandler for Handler {
    async fn handle(&mut self, update: Update) {
        println!("got an update: {:?}\n", update);
        if let UpdateKind::Message(message) = update.kind {
            if let Some(text) = message.get_text() {
                let api = self.api.clone();
                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 tgbot::{types::Update, async_trait, webhook, UpdateHandler};

struct Handler;

#[async_trait]
impl UpdateHandler for Handler {
    async fn handle(&mut self, update: Update) {
        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)