A telegram bot api for rust.
toml
telegrambot = "0.1"
```rust use futures::future::Future;
use telegrambot::api::SendMessage; use telegrambot::TelegramBot;
fn main() { let token = env::var("TELEGRAMBOTTOKEN").unwrap(); let cfg = Config::builder(token) .proxy("http://127.0.0.1:1081") .build() .unwrap();
TelegramBot::new(cfg).unwrap() .ontext(|(api, vtm)| { let firstname = vtm.message.from.unwrap().firstname; println!("<{}>: {}", firstname, vtm.text); telegrambot::spawn( api.sendmessage(&SendMessage::new(vtm.message.chat.id(), format!("Hi, {}! You just wrote '{}'", firstname, vtm.text))) .map(|| {}) .maperr(|_| {}) ); }) .start() .unwrap(); } ```
```rust use futures::future::Future;
use telegrambot::api::SendMessage; use telegrambot::TelegramBot;
fn main() { let token = env::var("TELEGRAMBOTTOKEN").unwrap(); let cfg = Config::builder(token) .proxy("http://127.0.0.1:1081") .build() .unwrap();
TelegramBot::new(cfg).unwrap() .oncommand("/list", |(api, vcm)| { telegrambot::spawn( api.sendmessage(&SendMessage::new(vcm.message.chat.id(), format!("Call list command"))) .map(|| {}) .maperr(|| {}) ); }) .oncommand("/page", |(api, vcm)| { telegrambot::spawn( api.sendmessage(&SendMessage::new(vcm.message.chat.id(), format!("Call page command, and arguments for this command: {:?}", vcm.args))) .map(|| {}) .maperr(|| {}) ); }) .start() .unwrap(); } ```
Support for parameter analysis. Some example:
/page 1 10
args: ["1", "10"]
/start "Just do it" 'from now on'
args: ["Just do it", "from now on"]
/run 'Just do it'
args: ["Just do it"]
More example you can see examples
Thanks to telegram-rs/telegram-bot project, telegram is a modification to telegram-rs/telegram-bot