Crate mobot

source ·
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:

  • Client is the main entry point to the Telegram API. It is used to send requests to the Telegram API.

  • 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.

  • Handlers are functions that handle events. They are registered with the Router, 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 of Handler calls. They are used to send responses to the Telegram API. See: chat::Action and query::Action.

  • Events are the events that the bot receives. They are passed to Handlers, and can be used to determine what action to take. See chat::Event and query::Event.

  • State is the user-defined state of the bot. It is passed to Handlers, as a generic parameter and can be used to store information about the bot. State must implement the Default and Clone traits. Default is used to initialize the state of a new chat session, and Clone is used while passing the state to the handlers. States are typically wrapped in an std::sync::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 mobot::*;

#[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