Add commands_repl

This commit is contained in:
Temirkhan Myrzamadi 2020-07-30 20:07:46 +06:00
parent 1989dcbe04
commit c39586ef10
7 changed files with 62 additions and 35 deletions

View file

@ -156,19 +156,15 @@ async fn answer(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<(
Ok(())
}
async fn handle_commands(rx: DispatcherHandlerRx<Message>) {
rx.commands::<Command, &str>(panic!("Insert here your bot's name"))
.for_each_concurrent(None, |(cx, command)| async move {
answer(cx, command).await.log_on_error().await;
})
.await;
}
#[tokio::main]
async fn main() {
// Setup is omitted...
}
teloxide::enable_logging!();
log::info!("Starting simple_commands_bot...");
let bot = Bot::from_env();
teloxide::commands_repl(bot, panic!("Insert here your bot's name"), answer).await;
}
```
<div align="center">

View file

@ -2,8 +2,6 @@ use std::str::FromStr;
use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommand};
use futures::future;
// Derive BotCommand to parse text with a command into this enumeration.
//
// 1. rename = "lowercase" turns all the commands into lowercase letters.
@ -130,16 +128,6 @@ async fn action(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<(
Ok(())
}
async fn handle_commands(rx: DispatcherHandlerRx<Message>) {
rx.filter(|cx| future::ready(cx.update.chat.is_group()))
.commands::<Command, &str>(panic!("Insert here your bot's name"))
// Execute all incoming commands concurrently:
.for_each_concurrent(None, |(cx, command)| async move {
action(cx, command).await.log_on_error().await;
})
.await;
}
#[tokio::main]
async fn main() {
run().await;
@ -151,5 +139,5 @@ async fn run() {
let bot = Bot::from_env();
Dispatcher::new(bot).messages_handler(handle_commands).dispatch().await
teloxide::commands_repl(bot, panic!("Insert here your bot's name"), action).await;
}

View file

@ -25,14 +25,6 @@ async fn answer(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<(
Ok(())
}
async fn handle_commands(rx: DispatcherHandlerRx<Message>) {
rx.commands::<Command, &str>(panic!("Insert here your bot's name"))
.for_each_concurrent(None, |(cx, command)| async move {
answer(cx, command).await.log_on_error().await;
})
.await;
}
#[tokio::main]
async fn main() {
run().await;
@ -44,5 +36,5 @@ async fn run() {
let bot = Bot::from_env();
Dispatcher::new(bot).messages_handler(handle_commands).dispatch().await;
teloxide::commands_repl(bot, panic!("Insert here your bot's name"), answer).await;
}

View file

@ -0,0 +1,47 @@
use crate::{
dispatching::{Dispatcher, DispatcherHandlerRx, DispatcherHandlerRxExt, UpdateWithCx},
error_handlers::OnError,
requests::ResponseResult,
types::Message,
utils::command::BotCommand,
Bot,
};
use futures::StreamExt;
use std::{future::Future, sync::Arc};
/// A [REPL] for commands.
///
/// Used mostly for testing and demonstrative purposes.
///
/// # Caution
/// **DO NOT** use this function together with [`Dispatcher`] and [`repl`],
/// because Telegram disallow multiple requests at the same time from the same
/// bot.
///
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
/// [`Dispatcher`]: crate::dispatching::Dispatcher
/// [`repl`]: crate::dispatching::repl
pub async fn commands_repl<Cmd, H, Fut>(bot: Bot, bot_name: &'static str, handler: H)
where
Cmd: BotCommand + Send + 'static,
H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ResponseResult<()>> + Send + 'static,
{
let handler = Arc::new(handler);
Dispatcher::new(bot)
.messages_handler(move |rx: DispatcherHandlerRx<Message>| {
rx.commands::<Cmd, &'static str>(bot_name).for_each_concurrent(
None,
move |(cx, cmd)| {
let handler = Arc::clone(&handler);
async move {
handler(cx, cmd).await.log_on_error().await;
}
},
)
})
.dispatch()
.await;
}

View file

@ -43,6 +43,7 @@
//! [`tokio::sync::mpsc::UnboundedReceiver`]: https://docs.rs/tokio/0.2.11/tokio/sync/mpsc/struct.UnboundedReceiver.html
//! [examples/dialogue_bot]: https://github.com/teloxide/teloxide/tree/master/examples/dialogue_bot
mod commands_repl;
pub mod dialogue;
mod dispatcher;
mod dispatcher_handler;
@ -51,6 +52,7 @@ mod repl;
pub mod update_listeners;
mod update_with_cx;
pub use commands_repl::commands_repl;
pub use dispatcher::Dispatcher;
pub use dispatcher_handler::DispatcherHandler;
pub use dispatcher_handler_rx_ext::DispatcherHandlerRxExt;

View file

@ -13,11 +13,13 @@ use std::{future::Future, sync::Arc};
/// Used mostly for testing and demonstrative purposes.
///
/// # Caution
/// **DO NOT** use this function together with [`Dispatcher`], because Telegram
/// disallow multiple requests at the same time from the same bot.
/// **DO NOT** use this function together with [`Dispatcher`] and
/// [`commands_repl`], because Telegram disallow multiple requests at the same
/// time from the same bot.
///
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
/// [`Dispatcher`]: crate::dispatching::Dispatcher
/// [`commands_repl`]: crate::dispatching::commands_repl
pub async fn repl<H, Fut>(bot: Bot, handler: H)
where
H: Fn(UpdateWithCx<Message>) -> Fut + Send + Sync + 'static,

View file

@ -42,7 +42,7 @@
#![forbid(unsafe_code)]
pub use bot::{Bot, BotBuilder};
pub use dispatching::repl;
pub use dispatching::{commands_repl, repl};
pub use errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError};
mod errors;