Crates.io | rutebot |
lib.rs | rutebot |
version | 0.7.5 |
source | src |
created_at | 2019-04-16 18:37:19.469586 |
updated_at | 2021-12-02 13:08:43.26725 |
description | Telegram bot api bindings |
homepage | |
repository | https://github.com/Leonqn/rutebot |
max_upload_size | |
id | 128433 |
size | 11,053,887 |
Rust Telegram Bot. A framework offering Telegram Bot API bindings for the Rust programming language.
For details see the docs.
A simple greetings bot. Replies to all messages with text "Hello %USERNAME%"
You can run the following example with cargo run --example simplebot
.
use std::env;
use futures_util::StreamExt;
use rutebot::client::Rutebot;
use rutebot::requests::{SendMessage};
use rutebot::responses::Update;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let token_env = env::var_os("TELEGRAM_TOKEN")
.expect("Please specify your bot's token in the TELEGRAM_TOKEN environment variable.");
let token = token_env.to_string_lossy();
let rutebot = Rutebot::new(token);
let mut updates_stream = Box::pin(rutebot.incoming_updates(None, None));
while let Some(update) = updates_stream.next().await.transpose()? {
let create_reply_request = |update: Update| {
let message = update.message?;
let response_message = format!("Hello {}", message.from?.first_name);
let reply =
SendMessage::new_reply(message.chat.id, &response_message, message.message_id);
Some(rutebot.prepare_api_request(reply))
};
if let Some(reply) = create_reply_request(update) {
tokio::spawn(reply.send());
}
}
Ok(())
}