mobot
is a telegram chat bot written in Rust. Uses a native implementation of the
Telegram Bot API. Docs at https://docs.rs/mobot.
MIT Licensed. Copyright 2023 Mohit Muthanna Cheppudira.
Bot that replies with "Hello world!" to every message. Working example in src/bin/hello.rs
.
```rust use mobot::*;
async fn main() { let client = Client::new(std::env::var("TELEGRAM_TOKEN").unwrap().into()); let mut router = Router::new(client);
router.add_chat_handler(|_, _: ()| async move {
Ok(chat::Action::ReplyText("Hello world!".into()))
}).await;
router.start().await;
} ```
This bot demonstrates state managenent in your bots. Working example in src/bin/ping.rs
.
``rust
/// Every Telegram chat session has a unique ID. This is used to identify the
/// chat that the bot is currently in.
///
/// The
ChatStateis a simple counter that is incremented every time a message
/// is received. Every chat session has its own
ChatState. The
Routerkeeps
/// track of the
ChatState` for each chat session.
struct ChatState { counter: usize, }
/// This is our chat handler. We simply increment the counter and reply with a
/// message containing the counter.
async fn handlechatevent(
e: chat::Event,
state: Arc
match e.message {
chat::MessageEvent::New(message) => {
state.counter += 1;
Ok(chat::Action::ReplyText(format!(
"pong({}): {}",
state.counter,
message.text.unwrap_or_default()
)))
}
_ => anyhow::bail!("Unhandled update"),
}
}
async fn main() { mobot::init_logger(); info!("Starting pingbot...");
// The `Client` is the main entry point to the Telegram API. It is used to
// send requests to the Telegram API.
let client = Client::new(env::var("TELEGRAM_TOKEN").unwrap().into());
// The `Router` is the main entry point to the bot. It is used to register
// handlers for different types of events, and keeps track of the state of
// the bot, passing it to the right handler.
let mut router = Router::new(client);
// We add a helper handler that logs all incoming messages.
router.add_chat_handler(chat::log_handler).await;
// We add our own handler that responds to messages.
router.add_chat_handler(handle_chat_event).await;
// Start the chat router -- this blocks forever.
router.start().await;
} ```
Bot that returns server uptime. Working example in src/bin/uptime.rs
.
```rust /// The state of the chat. This is a simple counter that is incremented every /// time a message is received.
struct ChatState { counter: usize, }
/// Get the uptime of the system.
async fn getuptime() -> anyhow::Result
/// The handler for the chat. This is a simple function that takes a chat::Event
/// and returns a chat::Action
. It also receives the current ChatState
for the
/// chat ID.
async fn handlechatevent(e: chat::Event, state: Arc
match e.message {
// Ignore the chat message, just return the uptime.
chat::MessageEvent::New(_) => {
state.counter += 1;
Ok(chat::Action::ReplyText(format!(
"uptime({}): {}",
state.counter,
get_uptime()
.await
.or(Err(anyhow!("Failed to get uptime")))?
)))
}
_ => anyhow::bail!("Unhandled update"),
}
}
async fn main() { mobot::init_logger(); info!("Starting uptimebot...");
let client = Client::new(env::var("TELEGRAM_TOKEN").unwrap().into());
let mut router = Router::new(client);
router.add_chat_handler(chat::log_handler).await;
router.add_chat_handler(handle_chat_event).await;
router.start().await;
} ```
Set your telegram API token and then run bin/hello/rs
.
``` export TELEGRAM_TOKEN=...
RUST_LOG=debug cargo run hello ```
Need OpenSSL and pkg-config.
sudo apt-get install pkg-config libssl-dev