mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-22 06:45:37 +01:00
Add commands_repl
This commit is contained in:
parent
1989dcbe04
commit
c39586ef10
7 changed files with 62 additions and 35 deletions
16
README.md
16
README.md
|
@ -156,19 +156,15 @@ async fn answer(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<(
|
||||||
Ok(())
|
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]
|
#[tokio::main]
|
||||||
async fn 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">
|
<div align="center">
|
||||||
|
|
|
@ -2,8 +2,6 @@ use std::str::FromStr;
|
||||||
|
|
||||||
use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommand};
|
use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommand};
|
||||||
|
|
||||||
use futures::future;
|
|
||||||
|
|
||||||
// Derive BotCommand to parse text with a command into this enumeration.
|
// Derive BotCommand to parse text with a command into this enumeration.
|
||||||
//
|
//
|
||||||
// 1. rename = "lowercase" turns all the commands into lowercase letters.
|
// 1. rename = "lowercase" turns all the commands into lowercase letters.
|
||||||
|
@ -130,16 +128,6 @@ async fn action(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<(
|
||||||
Ok(())
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
run().await;
|
run().await;
|
||||||
|
@ -151,5 +139,5 @@ async fn run() {
|
||||||
|
|
||||||
let bot = Bot::from_env();
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,14 +25,6 @@ async fn answer(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<(
|
||||||
Ok(())
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
run().await;
|
run().await;
|
||||||
|
@ -44,5 +36,5 @@ async fn run() {
|
||||||
|
|
||||||
let bot = Bot::from_env();
|
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;
|
||||||
}
|
}
|
||||||
|
|
47
src/dispatching/commands_repl.rs
Normal file
47
src/dispatching/commands_repl.rs
Normal 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;
|
||||||
|
}
|
|
@ -43,6 +43,7 @@
|
||||||
//! [`tokio::sync::mpsc::UnboundedReceiver`]: https://docs.rs/tokio/0.2.11/tokio/sync/mpsc/struct.UnboundedReceiver.html
|
//! [`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
|
//! [examples/dialogue_bot]: https://github.com/teloxide/teloxide/tree/master/examples/dialogue_bot
|
||||||
|
|
||||||
|
mod commands_repl;
|
||||||
pub mod dialogue;
|
pub mod dialogue;
|
||||||
mod dispatcher;
|
mod dispatcher;
|
||||||
mod dispatcher_handler;
|
mod dispatcher_handler;
|
||||||
|
@ -51,6 +52,7 @@ mod repl;
|
||||||
pub mod update_listeners;
|
pub mod update_listeners;
|
||||||
mod update_with_cx;
|
mod update_with_cx;
|
||||||
|
|
||||||
|
pub use commands_repl::commands_repl;
|
||||||
pub use dispatcher::Dispatcher;
|
pub use dispatcher::Dispatcher;
|
||||||
pub use dispatcher_handler::DispatcherHandler;
|
pub use dispatcher_handler::DispatcherHandler;
|
||||||
pub use dispatcher_handler_rx_ext::DispatcherHandlerRxExt;
|
pub use dispatcher_handler_rx_ext::DispatcherHandlerRxExt;
|
||||||
|
|
|
@ -13,11 +13,13 @@ use std::{future::Future, sync::Arc};
|
||||||
/// Used mostly for testing and demonstrative purposes.
|
/// Used mostly for testing and demonstrative purposes.
|
||||||
///
|
///
|
||||||
/// # Caution
|
/// # Caution
|
||||||
/// **DO NOT** use this function together with [`Dispatcher`], because Telegram
|
/// **DO NOT** use this function together with [`Dispatcher`] and
|
||||||
/// disallow multiple requests at the same time from the same bot.
|
/// [`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
|
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
||||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||||
|
/// [`commands_repl`]: crate::dispatching::commands_repl
|
||||||
pub async fn repl<H, Fut>(bot: Bot, handler: H)
|
pub async fn repl<H, Fut>(bot: Bot, handler: H)
|
||||||
where
|
where
|
||||||
H: Fn(UpdateWithCx<Message>) -> Fut + Send + Sync + 'static,
|
H: Fn(UpdateWithCx<Message>) -> Fut + Send + Sync + 'static,
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
pub use bot::{Bot, BotBuilder};
|
pub use bot::{Bot, BotBuilder};
|
||||||
pub use dispatching::repl;
|
pub use dispatching::{commands_repl, repl};
|
||||||
pub use errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError};
|
pub use errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError};
|
||||||
|
|
||||||
mod errors;
|
mod errors;
|
||||||
|
|
Loading…
Add table
Reference in a new issue