A full-featured Telegram Bot API client
toml
[dependencies]
tgbot = "0.7.0"
Long polling:
```rust norun use std::env; use tgbot::{Api, Config, UpdateHandler, asynctrait}; use tgbot::longpoll::LongPoll; use tgbot::methods::SendMessage; use tgbot::types::{Update, UpdateKind};
struct Handler { api: Api, }
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.gettext() { let api = self.api.clone(); let chatid = message.getchatid(); let method = SendMessage::new(chat_id, text.data.clone()); api.execute(method).await.unwrap(); } } } }
async fn main() { let token = env::var("TGBOTTOKEN").expect("TGBOTTOKEN is not set"); let api = Api::new(Config::new(token)).expect("Failed to create API"); LongPoll::new(api.clone(), Handler { api }).run().await; } ```
Webhook:
```rust norun use tgbot::{types::Update, asynctrait, webhook, UpdateHandler};
struct Handler;
impl UpdateHandler for Handler { async fn handle(&mut self, update: Update) { println!("got an update: {:?}\n", update); } }
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:
sh
cp sample.env .env
Don't forget to change value of TGBOT_TOKEN
and other variables if required.
See CHANGELOG.md
See CODEOFCONDUCT.md.
The MIT License (MIT)