25 个版本

0.7.5 2021年12月2日
0.7.4 2021年10月7日
0.7.1 2021年9月11日
0.7.0 2021年7月15日
0.5.3 2019年6月9日

#2086 in 网页编程

Download history 24/week @ 2024-03-27 29/week @ 2024-04-03 1/week @ 2024-05-29 1/week @ 2024-06-26 68/week @ 2024-07-03

69 每月下载量

MIT 许可证

10MB
3K SLoC

rutebot

Crates.io doc.rs License MIT

Rust Telegram Bot. 提供了 Rust 编程语言对 Telegram Bot API 的绑定。

有关详细信息,请参阅 文档

示例

一个简单的问候机器人。对所有消息回复文本 "Hello %USERNAME%"

您可以使用以下命令运行此示例: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(())
}

依赖项

~6–20MB
~290K SLoC