mobot
is a telegram chat bot written in Rust. Uses a native implementation of the
Telegram Bot API.
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(chat::Error::Failed("Failed to get uptime".into())))?
)))
}
_ => Err(chat::Error::Failed("Unhandled update".into()).into()),
}
}
async fn main() { mogram::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);
router.add_chat_handler(handle_chat_event);
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