Expand description
mobot is a Telegram Bot framework for Rust.
It supports the full Telegram Bot API, and provides a simple framework around managing routing and state for your bot.
Framework
The key components of the framework are:
-
Clientis the main entry point to the Telegram API. It is used to send requests to the Telegram API. -
Routeris 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. -
Handlers are functions that handle events. They are registered with theRouter, and are called when an event is received.
Right now there are two types of handlers: chat::Handler and query::Handler. The
former is used to handle messages sent to the bot, and the latter is used
to handle inline queries.
Each Handler is passed an Event and a State, and returns an
[Action].
-
Actions are the result ofHandlercalls. They are used to send responses to the Telegram API. See:chat::Actionandquery::Action. -
Events are the events that the bot receives. They are passed to [Handler]s, and can be used to determine what action to take. Seechat::Eventandquery::Event. -
Stateis the user-defined state of the bot. It is passed to [Handler]s, as a generic parameter and can be used to store information about the bot.Statemust implement theDefaultandClonetraits.Defaultis used to initialize the state of a new chat session, andCloneis used while passing the state to the handlers.States are typically wrapped in an [Arc], so that they can be shared between threads.
Example
In the example below we create a bot that replies to every message with the text “Hello world!”.
use mogram::*;
#[tokio::main]
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()))
});
router.start().await;
}Re-exports
Modules
Functions
- This method initializes
env_loggerfrom the environment, defaulting toinfolevel logging.