Function teloxide::repls::repl

source ·
pub async fn repl<R, H, Args>(bot: R, handler: H)where
    R: Requester + Send + Sync + Clone + 'static,
    <R as Requester>::GetUpdates: Send,
    H: Injectable<DependencyMap, ResponseResult<()>, Args> + Send + Sync + 'static,
Available on crate feature ctrlc_handler only.
Expand description

A REPL for messages.

REPLs are meant only for simple bots and rapid prototyping. If you need to supply dependencies or describe more complex dispatch logic, please use Dispatcher. See also: “Dispatching or REPLs?”.

All errors from the handler and update listener will be logged.

Signature

Don’t be scared by many trait bounds in the signature, in essence they require:

  1. bot is a bot, client for the Telegram bot API. It is represented via the Requester trait.
  2. handler is an async function that takes arguments from DependencyMap (see below) and returns ResponseResult.

Handler arguments

teloxide provides the following types to the handler:

Each of these types can be accepted as a handler parameter. Note that they aren’t all required at the same time: e.g., you can take only the bot and the message without Me.

Stopping

To stop a REPL, simply press Ctrl+C in the terminal where you run the program. Note that graceful shutdown may take some time (we plan to improve this, see #711).

Caution

DO NOT use this function together with Dispatcher and other REPLs, because Telegram disallow multiple requests at the same time from the same bot.

Examples found in repository?
examples/throw_dice.rs (lines 12-15)
6
7
8
9
10
11
12
13
14
15
16
17
async fn main() {
    pretty_env_logger::init();
    log::info!("Starting throw dice bot...");

    let bot = Bot::from_env();

    teloxide::repl(bot, |bot: Bot, msg: Message| async move {
        bot.send_dice(msg.chat.id).await?;
        Ok(())
    })
    .await;
}