mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 09:49:07 +01:00
Merge branch 'dev' into teloxide-handler
This commit is contained in:
commit
9bb2a85aba
24 changed files with 335 additions and 276 deletions
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
|
@ -6,6 +6,9 @@ on:
|
|||
|
||||
name: Continuous integration
|
||||
|
||||
env:
|
||||
RUSTFLAGS: "--cfg CI_REDIS"
|
||||
|
||||
jobs:
|
||||
style:
|
||||
runs-on: ubuntu-latest
|
||||
|
@ -55,10 +58,13 @@ jobs:
|
|||
|
||||
include:
|
||||
- rust: stable
|
||||
toolchain: stable
|
||||
features: "--features full"
|
||||
- rust: beta
|
||||
toolchain: beta
|
||||
features: "--features full"
|
||||
- rust: nightly
|
||||
toolchain: nightly-2022-01-17
|
||||
features: "--all-features"
|
||||
|
||||
steps:
|
||||
|
@ -67,7 +73,7 @@ jobs:
|
|||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: ${{ matrix.rust }}
|
||||
toolchain: ${{ matrix.toolchain }}
|
||||
override: true
|
||||
|
||||
- name: build
|
||||
|
|
20
CHANGELOG.md
20
CHANGELOG.md
|
@ -6,20 +6,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## unreleased
|
||||
|
||||
### Removed
|
||||
|
||||
- The old dispatching system and related stuff: `dispatching`, `utils::UpState`, `prelude`, `repls2`, `crate::{dialogues_repl, dialogues_repl_with_listener}`, and `#[teloxide(subtransition)]` [**BC**].
|
||||
|
||||
### Added
|
||||
|
||||
- The new API for dialogue handlers: `teloxide::handler!` ([issue 567](https://github.com/teloxide/teloxide/issues/567)).
|
||||
|
||||
### Removed
|
||||
|
||||
- The old dispatching system and related stuff: `dispatching`, `utils::UpState`, `prelude`, `repls2`, `crate::{dialogues_repl, dialogues_repl_with_listener}`, and `#[teloxide(...)]`.
|
||||
|
||||
### Changed
|
||||
|
||||
- Rename `dispatching2` => `dispatching`.
|
||||
- Rename `prelude2` => `prelude`.
|
||||
- Move `update_listeners`, `stop_token`, `IdleShutdownError`, and `ShutdownToken` from the old `dispatching` to the new `dispatching`.
|
||||
- Replace `crate::{commands_repl, commands_repl_with_listener, repl, repl_with_listener}` with those of the new `dispatching`.
|
||||
- Rename `dispatching2` => `dispatching` [**BC**].
|
||||
- Rename `prelude2` => `prelude` [**BC**].
|
||||
- Move `update_listeners`, `stop_token`, `IdleShutdownError`, and `ShutdownToken` from the old `dispatching` to the new `dispatching` (previously `dispatching2`).
|
||||
- Replace `crate::{commands_repl, commands_repl_with_listener, repl, repl_with_listener}` with those of the new `dispatching` [**BC**].
|
||||
- `UpdateListener::StopToken` is now always `Send` [**BC**].
|
||||
- Rename `BotCommand` trait to `BotCommands` [**BC**].
|
||||
- `BotCommands::descriptions` now returns `CommandDescriptions` instead of `String` [**BC**].
|
||||
|
||||
## 0.7.2 - 2022-03-23
|
||||
|
||||
|
@ -37,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
- Log `UpdateKind::Error` in `teloxide::dispatching2::Dispatcher`.
|
||||
- Don't warn about unhandled updates in `repls2` ([issue 557](https://github.com/teloxide/teloxide/issues/557)).
|
||||
- `parse_command` and `parse_command_with_prefix` now ignores case of the bot username
|
||||
|
||||
## 0.7.1 - 2022-03-09
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ full = [
|
|||
|
||||
[dependencies]
|
||||
teloxide-core = { version = "0.4", default-features = false }
|
||||
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros.git", rev = "144eb73aaf39145bf8f6b57eec5c76730961c2f1", optional = true }
|
||||
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros.git", rev = "dfba097c7146ba6244a36001d703e04b771baa05", optional = true }
|
||||
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
|
|
@ -125,7 +125,7 @@ Commands are strongly typed and defined declaratively, similar to how we define
|
|||
([Full](examples/simple_commands.rs))
|
||||
|
||||
```rust,no_run
|
||||
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||
use teloxide::{prelude::*, utils::command::BotCommands};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
|
@ -139,7 +139,7 @@ async fn main() {
|
|||
teloxide::commands_repl(bot, answer, Command::ty()).await;
|
||||
}
|
||||
|
||||
#[derive(BotCommand, Clone)]
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||
enum Command {
|
||||
#[command(description = "display this text.")]
|
||||
|
@ -156,7 +156,9 @@ async fn answer(
|
|||
command: Command,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match command {
|
||||
Command::Help => bot.send_message(message.chat.id, Command::descriptions()).await?,
|
||||
Command::Help => {
|
||||
bot.send_message(message.chat.id, Command::descriptions().to_string()).await?
|
||||
}
|
||||
Command::Username(username) => {
|
||||
bot.send_message(message.chat.id, format!("Your username is @{}.", username)).await?
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use std::{error::Error, str::FromStr};
|
||||
|
||||
use chrono::Duration;
|
||||
use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommand};
|
||||
use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommands};
|
||||
|
||||
// Derive BotCommand to parse text with a command into this enumeration.
|
||||
// Derive BotCommands to parse text with a command into this enumeration.
|
||||
//
|
||||
// 1. rename = "lowercase" turns all the commands into lowercase letters.
|
||||
// 2. `description = "..."` specifies a text before all the commands.
|
||||
|
@ -12,7 +12,7 @@ use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommand};
|
|||
// your commands in this format:
|
||||
// %GENERAL-DESCRIPTION%
|
||||
// %PREFIX%%COMMAND% - %DESCRIPTION%
|
||||
#[derive(BotCommand, Clone)]
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(
|
||||
rename = "lowercase",
|
||||
description = "Use commands in format /%command% %num% %unit%",
|
||||
|
@ -72,7 +72,7 @@ async fn action(
|
|||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match command {
|
||||
Command::Help => {
|
||||
bot.send_message(msg.chat.id, Command::descriptions()).await?;
|
||||
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
|
||||
}
|
||||
Command::Kick => kick_user(bot, msg).await?,
|
||||
Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?,
|
||||
|
|
|
@ -6,10 +6,10 @@ use teloxide::{
|
|||
InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputMessageContent,
|
||||
InputMessageContentText,
|
||||
},
|
||||
utils::command::BotCommand,
|
||||
utils::command::BotCommands,
|
||||
};
|
||||
|
||||
#[derive(BotCommand)]
|
||||
#[derive(BotCommands)]
|
||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||
enum Command {
|
||||
#[command(description = "Display this text")]
|
||||
|
@ -31,9 +31,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
|
||||
|
||||
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
|
||||
|
||||
log::info!("Closing bot... Goodbye!");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -66,10 +63,10 @@ async fn message_handler(
|
|||
bot: AutoSend<Bot>,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
if let Some(text) = m.text() {
|
||||
match BotCommand::parse(text, "buttons") {
|
||||
match BotCommands::parse(text, "buttons") {
|
||||
Ok(Command::Help) => {
|
||||
// Just send the description of all commands.
|
||||
bot.send_message(m.chat.id, Command::descriptions()).await?;
|
||||
bot.send_message(m.chat.id, Command::descriptions().to_string()).await?;
|
||||
}
|
||||
Ok(Command::Start) => {
|
||||
// Create a list of buttons and send them.
|
||||
|
|
|
@ -7,7 +7,8 @@ use teloxide::{
|
|||
ErasedStorage, RedisStorage, SqliteStorage, Storage,
|
||||
},
|
||||
prelude::*,
|
||||
utils::command::BotCommand,
|
||||
types::Me,
|
||||
utils::command::BotCommands,
|
||||
};
|
||||
|
||||
type MyDialogue = Dialogue<State, ErasedStorage<State>>;
|
||||
|
@ -26,7 +27,7 @@ impl Default for State {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(BotCommand, Clone)]
|
||||
#[derive(BotCommands)]
|
||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||
pub enum Command {
|
||||
#[command(description = "get your number.")]
|
||||
|
|
|
@ -6,7 +6,7 @@ use rand::Rng;
|
|||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{Dice, Update},
|
||||
utils::command::BotCommand,
|
||||
utils::command::BotCommands,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -24,28 +24,6 @@ async fn main() {
|
|||
let handler = Update::filter_message()
|
||||
// You can use branching to define multiple ways in which an update will be handled. If the
|
||||
// first branch fails, an update will be passed to the second branch, and so on.
|
||||
.branch(
|
||||
// Filtering allow you to filter updates by some condition.
|
||||
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
|
||||
// An endpoint is the last update handler.
|
||||
.endpoint(|msg: Message, bot: AutoSend<Bot>| async move {
|
||||
log::info!("Received a message from a group chat.");
|
||||
bot.send_message(msg.chat.id, "This is a group chat.").await?;
|
||||
respond(())
|
||||
}),
|
||||
)
|
||||
.branch(
|
||||
// There are some extension filtering functions on `Message`. The following filter will
|
||||
// filter only messages with dices.
|
||||
Message::filter_dice().endpoint(
|
||||
|msg: Message, dice: Dice, bot: AutoSend<Bot>| async move {
|
||||
bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
|
||||
.reply_to_message_id(msg.id)
|
||||
.await?;
|
||||
Ok(())
|
||||
},
|
||||
),
|
||||
)
|
||||
.branch(
|
||||
dptree::entry()
|
||||
// Filter commands: the next handlers will receive a parsed `SimpleCommand`.
|
||||
|
@ -72,6 +50,28 @@ async fn main() {
|
|||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
.branch(
|
||||
// Filtering allow you to filter updates by some condition.
|
||||
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
|
||||
// An endpoint is the last update handler.
|
||||
.endpoint(|msg: Message, bot: AutoSend<Bot>| async move {
|
||||
log::info!("Received a message from a group chat.");
|
||||
bot.send_message(msg.chat.id, "This is a group chat.").await?;
|
||||
respond(())
|
||||
}),
|
||||
)
|
||||
.branch(
|
||||
// There are some extension filtering functions on `Message`. The following filter will
|
||||
// filter only messages with dices.
|
||||
Message::filter_dice().endpoint(
|
||||
|msg: Message, dice: Dice, bot: AutoSend<Bot>| async move {
|
||||
bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
|
||||
.reply_to_message_id(msg.id)
|
||||
.await?;
|
||||
Ok(())
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
Dispatcher::builder(bot, handler)
|
||||
|
@ -99,7 +99,7 @@ struct ConfigParameters {
|
|||
maintainer_username: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(BotCommand, Clone)]
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename = "lowercase", description = "Simple commands")]
|
||||
enum SimpleCommand {
|
||||
#[command(description = "shows this message.")]
|
||||
|
@ -110,7 +110,7 @@ enum SimpleCommand {
|
|||
MyId,
|
||||
}
|
||||
|
||||
#[derive(BotCommand, Clone)]
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename = "lowercase", description = "Maintainer commands")]
|
||||
enum MaintainerCommands {
|
||||
#[command(parse_with = "split", description = "generate a number within range")]
|
||||
|
@ -122,13 +122,20 @@ async fn simple_commands_handler(
|
|||
bot: AutoSend<Bot>,
|
||||
cmd: SimpleCommand,
|
||||
cfg: ConfigParameters,
|
||||
me: teloxide::types::Me,
|
||||
) -> Result<(), teloxide::RequestError> {
|
||||
let text = match cmd {
|
||||
SimpleCommand::Help => {
|
||||
if msg.from().unwrap().id == cfg.bot_maintainer {
|
||||
format!("{}\n{}", SimpleCommand::descriptions(), MaintainerCommands::descriptions())
|
||||
format!(
|
||||
"{}\n\n{}",
|
||||
SimpleCommand::descriptions(),
|
||||
MaintainerCommands::descriptions()
|
||||
)
|
||||
} else if msg.chat.is_group() || msg.chat.is_supergroup() {
|
||||
SimpleCommand::descriptions().username_from_me(&me).to_string()
|
||||
} else {
|
||||
SimpleCommand::descriptions()
|
||||
SimpleCommand::descriptions().to_string()
|
||||
}
|
||||
}
|
||||
SimpleCommand::Maintainer => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||
use teloxide::{prelude::*, utils::command::BotCommands};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
|
@ -12,7 +12,7 @@ async fn main() {
|
|||
teloxide::commands_repl(bot, answer, Command::ty()).await;
|
||||
}
|
||||
|
||||
#[derive(BotCommand, Clone)]
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||
enum Command {
|
||||
#[command(description = "display this text.")]
|
||||
|
@ -29,7 +29,9 @@ async fn answer(
|
|||
command: Command,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match command {
|
||||
Command::Help => bot.send_message(message.chat.id, Command::descriptions()).await?,
|
||||
Command::Help => {
|
||||
bot.send_message(message.chat.id, Command::descriptions().to_string()).await?
|
||||
}
|
||||
Command::Username(username) => {
|
||||
bot.send_message(message.chat.id, format!("Your username is @{}.", username)).await?
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[build]
|
||||
command = "rustup install nightly --profile minimal && cargo +nightly doc --all-features --no-deps -Zunstable-options -Zrustdoc-scrape-examples=examples && cp -r target/doc _netlify_out"
|
||||
command = "rustup install nightly-2022-02-02 --profile minimal && cargo +nightly doc --all-features --no-deps -Zunstable-options -Zrustdoc-scrape-examples=examples && cp -r target/doc _netlify_out"
|
||||
publish = "_netlify_out"
|
||||
|
||||
[build.environment]
|
||||
|
|
|
@ -13,28 +13,20 @@
|
|||
//! dialogues. Your dialogue state can be represented as an enumeration:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! #[derive(DialogueState, Clone)]
|
||||
//! #[handler_out(anyhow::Result<()>)]
|
||||
//! #[derive(Clone)]
|
||||
//! pub enum State {
|
||||
//! #[handler(handle_start)]
|
||||
//! Start,
|
||||
//!
|
||||
//! #[handler(handle_receive_full_name)]
|
||||
//! ReceiveFullName,
|
||||
//!
|
||||
//! #[handler(handle_receive_age)]
|
||||
//! ReceiveAge { full_name: String },
|
||||
//!
|
||||
//! #[handler(handle_receive_location)]
|
||||
//! ReceiveLocation { full_name: String, age: u8 },
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! Each state is associated with its respective handler: e.g., when a dialogue
|
||||
//! state is `ReceiveAge`, `handle_receive_age` is invoked:
|
||||
//! state is `ReceiveAge`, `receive_age` is invoked:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! async fn handle_receive_age(
|
||||
//! async fn receive_age(
|
||||
//! bot: AutoSend<Bot>,
|
||||
//! msg: Message,
|
||||
//! dialogue: MyDialogue,
|
||||
|
@ -61,7 +53,7 @@
|
|||
//! the inner storage:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! async fn handle_receive_location(
|
||||
//! async fn receive_location(
|
||||
//! bot: AutoSend<Bot>,
|
||||
//! msg: Message,
|
||||
//! dialogue: MyDialogue,
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
HandlerFactory,
|
||||
},
|
||||
types::{Me, Message},
|
||||
utils::command::BotCommand,
|
||||
utils::command::BotCommands,
|
||||
};
|
||||
use dptree::{di::DependencyMap, Handler};
|
||||
|
||||
|
@ -23,7 +23,7 @@ pub trait HandlerExt<Output> {
|
|||
#[must_use]
|
||||
fn filter_command<C>(self) -> Self
|
||||
where
|
||||
C: BotCommand + Send + Sync + 'static;
|
||||
C: BotCommands + Send + Sync + 'static;
|
||||
|
||||
/// Passes [`Dialogue<D, S>`] and `D` as handler dependencies.
|
||||
///
|
||||
|
@ -62,7 +62,7 @@ where
|
|||
{
|
||||
fn filter_command<C>(self) -> Self
|
||||
where
|
||||
C: BotCommand + Send + Sync + 'static,
|
||||
C: BotCommands + Send + Sync + 'static,
|
||||
{
|
||||
self.chain(dptree::filter_map(move |message: Message, me: Me| {
|
||||
let bot_name = me.user.username.expect("Bots must have a username");
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
},
|
||||
error_handlers::LoggingErrorHandler,
|
||||
types::Update,
|
||||
utils::command::BotCommand,
|
||||
utils::command::BotCommands,
|
||||
};
|
||||
use dptree::di::{DependencyMap, Injectable};
|
||||
use std::{fmt::Debug, marker::PhantomData};
|
||||
|
@ -28,7 +28,7 @@ use teloxide_core::requests::Requester;
|
|||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn commands_repl<'a, R, Cmd, H, E, Args>(bot: R, handler: H, cmd: PhantomData<Cmd>)
|
||||
where
|
||||
Cmd: BotCommand + Send + Sync + 'static,
|
||||
Cmd: BotCommands + Send + Sync + 'static,
|
||||
H: Injectable<DependencyMap, Result<(), E>, Args> + Send + Sync + 'static,
|
||||
R: Requester + Clone + Send + Sync + 'static,
|
||||
<R as Requester>::GetUpdates: Send,
|
||||
|
@ -68,7 +68,7 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
|
|||
listener: L,
|
||||
_cmd: PhantomData<Cmd>,
|
||||
) where
|
||||
Cmd: BotCommand + Send + Sync + 'static,
|
||||
Cmd: BotCommands + Send + Sync + 'static,
|
||||
H: Injectable<DependencyMap, Result<(), E>, Args> + Send + Sync + 'static,
|
||||
L: UpdateListener<ListenerE> + Send + 'a,
|
||||
ListenerE: Debug + Send + 'a,
|
||||
|
@ -81,23 +81,16 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
|
|||
// commands. See <https://github.com/teloxide/teloxide/issues/557>.
|
||||
let ignore_update = |_upd| Box::pin(async {});
|
||||
|
||||
let mut dispatcher = Dispatcher::builder(
|
||||
Dispatcher::builder(
|
||||
bot,
|
||||
Update::filter_message().filter_command::<Cmd>().branch(dptree::endpoint(handler)),
|
||||
)
|
||||
.default_handler(ignore_update)
|
||||
.build();
|
||||
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
dispatcher.setup_ctrlc_handler();
|
||||
|
||||
// To make mutable var from immutable.
|
||||
let mut dispatcher = dispatcher;
|
||||
|
||||
dispatcher
|
||||
.dispatch_with_listener(
|
||||
listener,
|
||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||
)
|
||||
.await;
|
||||
.build()
|
||||
.setup_ctrlc_handler()
|
||||
.dispatch_with_listener(
|
||||
listener,
|
||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
|
|
@ -1,96 +0,0 @@
|
|||
use crate::{
|
||||
dispatching::{
|
||||
dialogue::{DialogueDispatcher, DialogueStage, DialogueWithCx, InMemStorageError},
|
||||
update_listeners,
|
||||
update_listeners::UpdateListener,
|
||||
Dispatcher, UpdateWithCx,
|
||||
},
|
||||
error_handlers::LoggingErrorHandler,
|
||||
};
|
||||
use std::{fmt::Debug, future::Future, sync::Arc};
|
||||
use teloxide_core::{requests::Requester, types::Message};
|
||||
|
||||
/// A [REPL] for dialogues.
|
||||
///
|
||||
/// All errors from an update listener and handler will be logged. This function
|
||||
/// uses [`InMemStorage`].
|
||||
///
|
||||
/// # 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.
|
||||
///
|
||||
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||
/// [`InMemStorage`]: crate::dispatching::dialogue::InMemStorage
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn dialogues_repl<'a, R, H, D, Fut>(requester: R, handler: H)
|
||||
where
|
||||
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
||||
D: Clone + Default + Send + 'static,
|
||||
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
||||
R: Requester + Send + Clone + 'static,
|
||||
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
||||
{
|
||||
let cloned_requester = requester.clone();
|
||||
|
||||
dialogues_repl_with_listener(
|
||||
requester,
|
||||
handler,
|
||||
update_listeners::polling_default(cloned_requester).await,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Like [`dialogues_repl`], but with a custom [`UpdateListener`].
|
||||
///
|
||||
/// All errors from an update listener and handler will be logged. This function
|
||||
/// uses [`InMemStorage`].
|
||||
///
|
||||
/// # 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.
|
||||
///
|
||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||
/// [`dialogues_repl`]: crate::dispatching::repls::dialogues_repl()
|
||||
/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener
|
||||
/// [`InMemStorage`]: crate::dispatching::dialogue::InMemStorage
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn dialogues_repl_with_listener<'a, R, H, D, Fut, L, ListenerE>(
|
||||
requester: R,
|
||||
handler: H,
|
||||
listener: L,
|
||||
) where
|
||||
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
||||
D: Clone + Default + Send + 'static,
|
||||
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
||||
L: UpdateListener<ListenerE> + Send + 'a,
|
||||
ListenerE: Debug + Send + 'a,
|
||||
R: Requester + Send + Clone + 'static,
|
||||
{
|
||||
let handler = Arc::new(handler);
|
||||
|
||||
Dispatcher::new(requester)
|
||||
.messages_handler(DialogueDispatcher::new(
|
||||
move |DialogueWithCx { cx, dialogue }: DialogueWithCx<
|
||||
R,
|
||||
Message,
|
||||
D,
|
||||
InMemStorageError,
|
||||
>| {
|
||||
let handler = Arc::clone(&handler);
|
||||
|
||||
async move {
|
||||
let dialogue = dialogue.expect("std::convert::Infallible");
|
||||
handler(cx, dialogue).await
|
||||
}
|
||||
},
|
||||
))
|
||||
.setup_ctrlc_handler()
|
||||
.dispatch_with_listener(
|
||||
listener,
|
||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||
)
|
||||
.await;
|
||||
}
|
|
@ -4,5 +4,4 @@ mod commands_repl;
|
|||
mod repl;
|
||||
|
||||
pub use commands_repl::{commands_repl, commands_repl_with_listener};
|
||||
//pub use dialogues_repl::{dialogues_repl, dialogues_repl_with_listener};
|
||||
pub use repl::{repl, repl_with_listener};
|
||||
|
|
|
@ -59,19 +59,22 @@ where
|
|||
// messages. See <https://github.com/teloxide/teloxide/issues/557>.
|
||||
let ignore_update = |_upd| Box::pin(async {});
|
||||
|
||||
#[allow(unused_mut)]
|
||||
let mut dispatcher =
|
||||
Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler)))
|
||||
.default_handler(ignore_update)
|
||||
.build();
|
||||
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
dispatcher.setup_ctrlc_handler();
|
||||
|
||||
dispatcher
|
||||
Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler)))
|
||||
.default_handler(ignore_update)
|
||||
.build()
|
||||
.setup_ctrlc_handler()
|
||||
.dispatch_with_listener(
|
||||
listener,
|
||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repl_is_send() {
|
||||
let bot = crate::Bot::new("");
|
||||
let repl = crate::repl(bot, || async { crate::respond(()) });
|
||||
assert_send(&repl);
|
||||
|
||||
fn assert_send(_: &impl Send) {}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ pub use self::{
|
|||
/// [module-level documentation]: mod@self
|
||||
pub trait UpdateListener<E>: for<'a> AsUpdateStream<'a, E> {
|
||||
/// The type of token which allows to stop this listener.
|
||||
type StopToken: StopToken;
|
||||
type StopToken: StopToken + Send;
|
||||
|
||||
/// Returns a token which stops this listener.
|
||||
///
|
||||
|
|
|
@ -7,7 +7,7 @@ use futures::{
|
|||
|
||||
use crate::{
|
||||
dispatching::{
|
||||
stop_token::{AsyncStopFlag, AsyncStopToken, StopToken},
|
||||
stop_token::{AsyncStopFlag, AsyncStopToken},
|
||||
update_listeners::{stateful_listener::StatefulListener, UpdateListener},
|
||||
},
|
||||
payloads::{GetUpdates, GetUpdatesSetters as _},
|
||||
|
@ -22,9 +22,7 @@ use crate::{
|
|||
/// ## Notes
|
||||
///
|
||||
/// This function will automatically delete a webhook if it was set up.
|
||||
pub async fn polling_default<R>(
|
||||
requester: R,
|
||||
) -> impl UpdateListener<R::Err, StopToken = impl Send + StopToken>
|
||||
pub async fn polling_default<R>(requester: R) -> impl UpdateListener<R::Err>
|
||||
where
|
||||
R: Requester + Send + 'static,
|
||||
<R as Requester>::GetUpdates: Send,
|
||||
|
@ -130,7 +128,7 @@ pub fn polling<R>(
|
|||
timeout: Option<Duration>,
|
||||
limit: Option<u8>,
|
||||
allowed_updates: Option<Vec<AllowedUpdate>>,
|
||||
) -> impl UpdateListener<R::Err, StopToken = impl Send + StopToken>
|
||||
) -> impl UpdateListener<R::Err>
|
||||
where
|
||||
R: Requester + Send + 'static,
|
||||
<R as Requester>::GetUpdates: Send,
|
||||
|
|
|
@ -123,7 +123,7 @@ impl<St, Assf, Sf, Hauf, Stt, Thf, E> UpdateListener<E>
|
|||
where
|
||||
Self: for<'a> AsUpdateStream<'a, E>,
|
||||
Sf: FnMut(&mut St) -> Stt,
|
||||
Stt: StopToken,
|
||||
Stt: StopToken + Send,
|
||||
Hauf: FnMut(&mut St, &mut dyn Iterator<Item = AllowedUpdate>),
|
||||
Thf: Fn(&St) -> Option<Duration>,
|
||||
{
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
//! Command parsers.
|
||||
//!
|
||||
//! You can either create an `enum` with derived [`BotCommand`], containing
|
||||
//! You can either create an `enum` with derived [`BotCommands`], containing
|
||||
//! commands of your bot, or use functions, which split input text into a string
|
||||
//! command with its arguments.
|
||||
//!
|
||||
//! # Using BotCommand
|
||||
//! # Using BotCommands
|
||||
//!
|
||||
//! ```
|
||||
//! # #[cfg(feature = "macros")] {
|
||||
//! use teloxide::utils::command::BotCommand;
|
||||
//! use teloxide::utils::command::BotCommands;
|
||||
//!
|
||||
//! type UnitOfTime = u8;
|
||||
//!
|
||||
//! #[derive(BotCommand, PartialEq, Debug)]
|
||||
//! #[derive(BotCommands, PartialEq, Debug)]
|
||||
//! #[command(rename = "lowercase", parse_with = "split")]
|
||||
//! enum AdminCommand {
|
||||
//! Mute(UnitOfTime, char),
|
||||
|
@ -24,6 +25,7 @@
|
|||
//! ```
|
||||
//!
|
||||
//! # Using parse_command
|
||||
//!
|
||||
//! ```
|
||||
//! use teloxide::utils::command::parse_command;
|
||||
//!
|
||||
|
@ -33,6 +35,7 @@
|
|||
//! ```
|
||||
//!
|
||||
//! # Using parse_command_with_prefix
|
||||
//!
|
||||
//! ```
|
||||
//! use teloxide::utils::command::parse_command_with_prefix;
|
||||
//!
|
||||
|
@ -46,25 +49,27 @@
|
|||
//!
|
||||
//! [examples/admin_bot]: https://github.com/teloxide/teloxide/blob/master/examples/admin_bot/
|
||||
|
||||
use core::fmt;
|
||||
use std::{
|
||||
error::Error,
|
||||
fmt::{Display, Formatter},
|
||||
fmt::{Display, Formatter, Write},
|
||||
};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use teloxide_core::types::{BotCommand, Me};
|
||||
#[cfg(feature = "macros")]
|
||||
pub use teloxide_macros::BotCommand;
|
||||
pub use teloxide_macros::BotCommands;
|
||||
|
||||
/// An enumeration of bot's commands.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// # #[cfg(feature = "macros")] {
|
||||
/// use teloxide::utils::command::BotCommand;
|
||||
/// use teloxide::utils::command::BotCommands;
|
||||
///
|
||||
/// type UnitOfTime = u8;
|
||||
///
|
||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||
/// #[derive(BotCommands, PartialEq, Debug)]
|
||||
/// #[command(rename = "lowercase", parse_with = "split")]
|
||||
/// enum AdminCommand {
|
||||
/// Mute(UnitOfTime, char),
|
||||
|
@ -98,9 +103,9 @@ pub use teloxide_macros::BotCommand;
|
|||
/// ## Example
|
||||
/// ```
|
||||
/// # #[cfg(feature = "macros")] {
|
||||
/// use teloxide::utils::command::BotCommand;
|
||||
/// use teloxide::utils::command::BotCommands;
|
||||
///
|
||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||
/// #[derive(BotCommands, PartialEq, Debug)]
|
||||
/// #[command(rename = "lowercase")]
|
||||
/// enum Command {
|
||||
/// Text(String),
|
||||
|
@ -118,9 +123,9 @@ pub use teloxide_macros::BotCommand;
|
|||
/// ## Example
|
||||
/// ```
|
||||
/// # #[cfg(feature = "macros")] {
|
||||
/// use teloxide::utils::command::BotCommand;
|
||||
/// use teloxide::utils::command::BotCommands;
|
||||
///
|
||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||
/// #[derive(BotCommands, PartialEq, Debug)]
|
||||
/// #[command(rename = "lowercase", parse_with = "split")]
|
||||
/// enum Command {
|
||||
/// Nums(u8, u16, i32),
|
||||
|
@ -138,9 +143,9 @@ pub use teloxide_macros::BotCommand;
|
|||
/// ## Example
|
||||
/// ```
|
||||
/// # #[cfg(feature = "macros")] {
|
||||
/// use teloxide::utils::command::BotCommand;
|
||||
/// use teloxide::utils::command::BotCommands;
|
||||
///
|
||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||
/// #[derive(BotCommands, PartialEq, Debug)]
|
||||
/// #[command(rename = "lowercase", parse_with = "split", separator = "|")]
|
||||
/// enum Command {
|
||||
/// Nums(u8, u16, i32),
|
||||
|
@ -171,7 +176,7 @@ pub use teloxide_macros::BotCommand;
|
|||
/// ## Example
|
||||
/// ```
|
||||
/// # #[cfg(feature = "macros")] {
|
||||
/// use teloxide::utils::command::{BotCommand, ParseError};
|
||||
/// use teloxide::utils::command::{BotCommands, ParseError};
|
||||
///
|
||||
/// fn accept_two_digits(input: String) -> Result<(u8,), ParseError> {
|
||||
/// match input.len() {
|
||||
|
@ -183,7 +188,7 @@ pub use teloxide_macros::BotCommand;
|
|||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||
/// #[derive(BotCommands, PartialEq, Debug)]
|
||||
/// #[command(rename = "lowercase")]
|
||||
/// enum Command {
|
||||
/// #[command(parse_with = "accept_two_digits")]
|
||||
|
@ -204,24 +209,41 @@ pub use teloxide_macros::BotCommand;
|
|||
/// specific variant.
|
||||
///
|
||||
/// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
|
||||
/// [`BotCommand`]: crate::utils::command::BotCommand
|
||||
pub trait BotCommand: Sized {
|
||||
fn descriptions() -> String;
|
||||
fn parse<N>(s: &str, bot_name: N) -> Result<Self, ParseError>
|
||||
/// [`BotCommands`]: crate::utils::command::BotCommands
|
||||
pub trait BotCommands: Sized {
|
||||
/// Parses a command.
|
||||
///
|
||||
/// `bot_username` is required to parse commands like
|
||||
/// `/cmd@username_of_the_bot`.
|
||||
fn parse<N>(s: &str, bot_username: N) -> Result<Self, ParseError>
|
||||
where
|
||||
N: Into<String>;
|
||||
|
||||
/// Returns descriptions of the commands suitable to be shown to the user
|
||||
/// (for example when `/help` command is used).
|
||||
fn descriptions() -> CommandDescriptions<'static>;
|
||||
|
||||
/// Returns a vector of [`BotCommand`] that can be used with
|
||||
/// [`set_my_commands`].
|
||||
///
|
||||
/// [`BotCommand`]: crate::types::BotCommand
|
||||
/// [`set_my_commands`]: crate::requests::Requester::set_my_commands
|
||||
fn bot_commands() -> Vec<BotCommand>;
|
||||
|
||||
/// Returns `PhantomData<Self>` that is used as a param of [`commands_repl`]
|
||||
///
|
||||
/// [`commands_repl`]: (crate::repls2::commands_repl)
|
||||
fn ty() -> PhantomData<Self> {
|
||||
PhantomData
|
||||
}
|
||||
fn bot_commands() -> Vec<crate::types::BotCommand>;
|
||||
}
|
||||
|
||||
pub type PrefixedBotCommand = String;
|
||||
pub type BotName = String;
|
||||
|
||||
/// Errors returned from [`BotCommand::parse`].
|
||||
/// Errors returned from [`BotCommands::parse`].
|
||||
///
|
||||
/// [`BotCommand::parse`]: crate::utils::command::BotCommand::parse
|
||||
/// [`BotCommands::parse`]: BotCommands::parse
|
||||
#[derive(Debug)]
|
||||
pub enum ParseError {
|
||||
TooFewArguments {
|
||||
|
@ -247,28 +269,77 @@ pub enum ParseError {
|
|||
Custom(Box<dyn Error + Send + Sync + 'static>),
|
||||
}
|
||||
|
||||
impl Display for ParseError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
ParseError::TooFewArguments { expected, found, message } => write!(
|
||||
f,
|
||||
"Too few arguments (expected {}, found {}, message = '{}')",
|
||||
expected, found, message
|
||||
),
|
||||
ParseError::TooManyArguments { expected, found, message } => write!(
|
||||
f,
|
||||
"Too many arguments (expected {}, found {}, message = '{}')",
|
||||
expected, found, message
|
||||
),
|
||||
ParseError::IncorrectFormat(e) => write!(f, "Incorrect format of command args: {}", e),
|
||||
ParseError::UnknownCommand(e) => write!(f, "Unknown command: {}", e),
|
||||
ParseError::WrongBotName(n) => write!(f, "Wrong bot name: {}", n),
|
||||
ParseError::Custom(e) => write!(f, "{}", e),
|
||||
}
|
||||
}
|
||||
/// Command descriptions that can be shown to the user (e.g. as a part of
|
||||
/// `/help` message)
|
||||
///
|
||||
/// Most of the time you don't need to create this struct yourself as it's
|
||||
/// returned from [`BotCommands::descriptions`].
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CommandDescriptions<'a> {
|
||||
global_description: Option<&'a str>,
|
||||
descriptions: &'a [CommandDescription<'a>],
|
||||
bot_username: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl std::error::Error for ParseError {}
|
||||
/// Description of a particular command, used in [`CommandDescriptions`].
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CommandDescription<'a> {
|
||||
/// Prefix of the command, usually `/`.
|
||||
pub prefix: &'a str,
|
||||
/// The command itself, e.g. `start`.
|
||||
pub command: &'a str,
|
||||
/// Human-readable description of the command.
|
||||
pub description: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> CommandDescriptions<'a> {
|
||||
/// Creates new [`CommandDescriptions`] from a list of command descriptions.
|
||||
pub fn new(descriptions: &'a [CommandDescription<'a>]) -> Self {
|
||||
Self { global_description: None, descriptions, bot_username: None }
|
||||
}
|
||||
|
||||
/// Sets the global description of these commands.
|
||||
pub fn global_description(self, global_description: &'a str) -> Self {
|
||||
Self { global_description: Some(global_description), ..self }
|
||||
}
|
||||
|
||||
/// Sets the username of the bot.
|
||||
///
|
||||
/// After this method is called, returned instance of
|
||||
/// [`CommandDescriptions`] will append `@bot_username` to all commands.
|
||||
/// This is useful in groups, to disambiguate commands for different bots.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```
|
||||
/// use teloxide::utils::command::{CommandDescription, CommandDescriptions};
|
||||
///
|
||||
/// let descriptions = CommandDescriptions::new(&[
|
||||
/// CommandDescription { prefix: "/", command: "start", description: "start this bot" },
|
||||
/// CommandDescription { prefix: "/", command: "help", description: "show this message" },
|
||||
/// ]);
|
||||
///
|
||||
/// assert_eq!(descriptions.to_string(), "/start — start this bot\n/help — show this message");
|
||||
/// assert_eq!(
|
||||
/// descriptions.username("username_of_the_bot").to_string(),
|
||||
/// "/start@username_of_the_bot — start this bot\n/help@username_of_the_bot — show this \
|
||||
/// message"
|
||||
/// );
|
||||
/// ```
|
||||
pub fn username(self, bot_username: &'a str) -> Self {
|
||||
Self { bot_username: Some(bot_username), ..self }
|
||||
}
|
||||
|
||||
/// Sets the username of the bot.
|
||||
///
|
||||
/// This is the same as [`username`], but uses value returned from `get_me`
|
||||
/// method to get the username.
|
||||
///
|
||||
/// [`username`]: self::CommandDescriptions::username
|
||||
pub fn username_from_me(self, me: &'a Me) -> CommandDescriptions<'a> {
|
||||
self.username(me.user.username.as_deref().expect("Bots must have usernames"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a string into a command with args.
|
||||
///
|
||||
|
@ -346,6 +417,68 @@ where
|
|||
Some((command, words.collect()))
|
||||
}
|
||||
|
||||
impl Display for ParseError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
ParseError::TooFewArguments { expected, found, message } => write!(
|
||||
f,
|
||||
"Too few arguments (expected {}, found {}, message = '{}')",
|
||||
expected, found, message
|
||||
),
|
||||
ParseError::TooManyArguments { expected, found, message } => write!(
|
||||
f,
|
||||
"Too many arguments (expected {}, found {}, message = '{}')",
|
||||
expected, found, message
|
||||
),
|
||||
ParseError::IncorrectFormat(e) => write!(f, "Incorrect format of command args: {}", e),
|
||||
ParseError::UnknownCommand(e) => write!(f, "Unknown command: {}", e),
|
||||
ParseError::WrongBotName(n) => write!(f, "Wrong bot name: {}", n),
|
||||
ParseError::Custom(e) => write!(f, "{}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ParseError {}
|
||||
|
||||
impl Display for CommandDescriptions<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if let Some(global_description) = self.global_description {
|
||||
f.write_str(global_description)?;
|
||||
f.write_str("\n\n")?;
|
||||
}
|
||||
|
||||
let mut write = |&CommandDescription { prefix, command, description }, nls| {
|
||||
if nls {
|
||||
f.write_char('\n')?;
|
||||
}
|
||||
|
||||
f.write_str(prefix)?;
|
||||
f.write_str(command)?;
|
||||
|
||||
if let Some(username) = self.bot_username {
|
||||
f.write_char('@')?;
|
||||
f.write_str(username)?;
|
||||
}
|
||||
|
||||
if !description.is_empty() {
|
||||
f.write_str(" — ")?;
|
||||
f.write_str(description)?;
|
||||
}
|
||||
|
||||
fmt::Result::Ok(())
|
||||
};
|
||||
|
||||
if let Some(descr) = self.descriptions.first() {
|
||||
write(descr, false)?;
|
||||
for descr in &self.descriptions[1..] {
|
||||
write(descr, true)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// The rest of tests are integrational due to problems with macro expansion in
|
||||
// unit tests.
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -12,7 +12,9 @@ use tokio::sync::Notify;
|
|||
|
||||
use crate::dispatching::update_listeners::UpdateListener;
|
||||
|
||||
/// A token which used to shutdown [`crate::dispatching::Dispatcher`].
|
||||
/// A token which used to shutdown [`Dispatcher`].
|
||||
///
|
||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||
#[derive(Clone)]
|
||||
pub struct ShutdownToken {
|
||||
dispatcher_state: Arc<DispatcherState>,
|
||||
|
@ -20,7 +22,9 @@ pub struct ShutdownToken {
|
|||
}
|
||||
|
||||
/// This error is returned from [`ShutdownToken::shutdown`] when trying to
|
||||
/// shutdown an idle [`crate::dispatching::Dispatcher`].
|
||||
/// shutdown an idle [`Dispatcher`].
|
||||
///
|
||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||
#[derive(Debug)]
|
||||
pub struct IdleShutdownError;
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#![allow(clippy::nonstandard_macro_braces)]
|
||||
|
||||
#[cfg(feature = "macros")]
|
||||
use teloxide::utils::command::{BotCommand, ParseError};
|
||||
use teloxide::utils::command::{BotCommands, ParseError};
|
||||
|
||||
// We put tests here because macro expand in unit tests in module
|
||||
// teloxide::utils::command was a failure
|
||||
|
@ -10,7 +10,7 @@ use teloxide::utils::command::{BotCommand, ParseError};
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_command_with_args() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
Start(String),
|
||||
|
@ -26,7 +26,7 @@ fn parse_command_with_args() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_command_with_non_string_arg() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
Start(i32),
|
||||
|
@ -42,7 +42,7 @@ fn parse_command_with_non_string_arg() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn attribute_prefix() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
#[command(prefix = "!")]
|
||||
|
@ -59,7 +59,7 @@ fn attribute_prefix() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn many_attributes() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
#[command(prefix = "!", description = "desc")]
|
||||
|
@ -68,13 +68,13 @@ fn many_attributes() {
|
|||
}
|
||||
|
||||
assert_eq!(DefaultCommands::Start, DefaultCommands::parse("!start", "").unwrap());
|
||||
assert_eq!(DefaultCommands::descriptions(), "!start - desc\n/help\n");
|
||||
assert_eq!(DefaultCommands::descriptions().to_string(), "!start — desc\n/help");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn global_attributes() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(prefix = "!", rename = "lowercase", description = "Bot commands")]
|
||||
enum DefaultCommands {
|
||||
#[command(prefix = "/")]
|
||||
|
@ -84,13 +84,13 @@ fn global_attributes() {
|
|||
|
||||
assert_eq!(DefaultCommands::Start, DefaultCommands::parse("/start", "MyNameBot").unwrap());
|
||||
assert_eq!(DefaultCommands::Help, DefaultCommands::parse("!help", "MyNameBot").unwrap());
|
||||
assert_eq!(DefaultCommands::descriptions(), "Bot commands\n/start\n!help\n");
|
||||
assert_eq!(DefaultCommands::descriptions().to_string(), "Bot commands\n\n/start\n!help");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_command_with_bot_name() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
#[command(prefix = "/")]
|
||||
|
@ -107,7 +107,7 @@ fn parse_command_with_bot_name() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_with_split() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
#[command(parse_with = "split")]
|
||||
enum DefaultCommands {
|
||||
|
@ -124,7 +124,7 @@ fn parse_with_split() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_with_split2() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
#[command(parse_with = "split", separator = "|")]
|
||||
enum DefaultCommands {
|
||||
|
@ -152,7 +152,7 @@ fn parse_custom_parser() {
|
|||
.map_err(|_| ParseError::Custom("First argument must be a integer!".to_owned().into()))
|
||||
}
|
||||
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
#[command(parse_with = "custom_parse_function")]
|
||||
|
@ -169,7 +169,7 @@ fn parse_custom_parser() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_named_fields() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
#[command(parse_with = "split")]
|
||||
enum DefaultCommands {
|
||||
|
@ -186,7 +186,7 @@ fn parse_named_fields() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn descriptions_off() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
#[command(description = "off")]
|
||||
|
@ -194,13 +194,13 @@ fn descriptions_off() {
|
|||
Help,
|
||||
}
|
||||
|
||||
assert_eq!(DefaultCommands::descriptions(), "/help\n".to_owned());
|
||||
assert_eq!(DefaultCommands::descriptions().to_string(), "/help".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn rename_rules() {
|
||||
#[derive(BotCommand, Debug, PartialEq)]
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
enum DefaultCommands {
|
||||
#[command(rename = "lowercase")]
|
||||
AaaAaa,
|
||||
|
@ -230,7 +230,7 @@ fn rename_rules() {
|
|||
assert_eq!(DefaultCommands::HhhHhh, DefaultCommands::parse("/HHH-HHH", "").unwrap());
|
||||
|
||||
assert_eq!(
|
||||
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/HHH-HHH\n",
|
||||
DefaultCommands::descriptions()
|
||||
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/HHH-HHH",
|
||||
DefaultCommands::descriptions().to_string()
|
||||
);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::{
|
|||
use teloxide::dispatching::dialogue::{RedisStorage, RedisStorageError, Serializer, Storage};
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg_attr(not(CI_REDIS), ignore)]
|
||||
async fn test_redis_json() {
|
||||
let storage = RedisStorage::open(
|
||||
"redis://127.0.0.1:7777",
|
||||
|
@ -16,6 +17,7 @@ async fn test_redis_json() {
|
|||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg_attr(not(CI_REDIS), ignore)]
|
||||
async fn test_redis_bincode() {
|
||||
let storage = RedisStorage::open(
|
||||
"redis://127.0.0.1:7778",
|
||||
|
@ -27,6 +29,7 @@ async fn test_redis_bincode() {
|
|||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg_attr(not(CI_REDIS), ignore)]
|
||||
async fn test_redis_cbor() {
|
||||
let storage = RedisStorage::open(
|
||||
"redis://127.0.0.1:7779",
|
||||
|
|
|
@ -1,36 +1,47 @@
|
|||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
fs,
|
||||
sync::Arc,
|
||||
};
|
||||
use teloxide::dispatching::dialogue::{Serializer, SqliteStorage, SqliteStorageError, Storage};
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_sqlite_json() {
|
||||
let storage =
|
||||
SqliteStorage::open("./test_db1.sqlite", teloxide::dispatching::dialogue::serializer::Json)
|
||||
.await
|
||||
.unwrap();
|
||||
fs::create_dir("./test_db1").unwrap();
|
||||
let storage = SqliteStorage::open(
|
||||
"./test_db1/test_db1.sqlite",
|
||||
teloxide::dispatching::dialogue::serializer::Json,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
test_sqlite(storage).await;
|
||||
fs::remove_dir_all("./test_db1").unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_sqlite_bincode() {
|
||||
fs::create_dir("./test_db2").unwrap();
|
||||
let storage = SqliteStorage::open(
|
||||
"./test_db2.sqlite",
|
||||
"./test_db2/test_db2.sqlite",
|
||||
teloxide::dispatching::dialogue::serializer::Bincode,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
test_sqlite(storage).await;
|
||||
fs::remove_dir_all("./test_db2").unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_sqlite_cbor() {
|
||||
let storage =
|
||||
SqliteStorage::open("./test_db3.sqlite", teloxide::dispatching::dialogue::serializer::Cbor)
|
||||
.await
|
||||
.unwrap();
|
||||
fs::create_dir("./test_db3").unwrap();
|
||||
let storage = SqliteStorage::open(
|
||||
"./test_db3/test_db3.sqlite",
|
||||
teloxide::dispatching::dialogue::serializer::Cbor,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
test_sqlite(storage).await;
|
||||
fs::remove_dir_all("./test_db3").unwrap();
|
||||
}
|
||||
|
||||
type Dialogue = String;
|
||||
|
|
Loading…
Reference in a new issue