mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
Apply suggestions from the review
- remove `username_from_bot` - add `username_from_me`
This commit is contained in:
parent
a5aada3dc7
commit
91bea5be5e
2 changed files with 32 additions and 39 deletions
|
@ -7,7 +7,7 @@ use rand::Rng;
|
|||
// dispatching system, which will be deprecated in the future.
|
||||
use teloxide::{
|
||||
prelude2::*,
|
||||
types::{Dice, Update},
|
||||
types::{Dice, Me, Update},
|
||||
utils::command::BotCommands,
|
||||
};
|
||||
|
||||
|
@ -26,28 +26,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`.
|
||||
|
@ -74,6 +52,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)
|
||||
|
@ -124,6 +124,7 @@ async fn simple_commands_handler(
|
|||
bot: AutoSend<Bot>,
|
||||
cmd: SimpleCommand,
|
||||
cfg: ConfigParameters,
|
||||
me: Me,
|
||||
) -> Result<(), teloxide::RequestError> {
|
||||
let text = match cmd {
|
||||
SimpleCommand::Help => {
|
||||
|
@ -134,7 +135,7 @@ async fn simple_commands_handler(
|
|||
MaintainerCommands::descriptions()
|
||||
)
|
||||
} else if msg.chat.is_group() || msg.chat.is_supergroup() {
|
||||
SimpleCommand::descriptions().username("USERNAME_BOT").to_string()
|
||||
SimpleCommand::descriptions().username_from_me(&me).to_string()
|
||||
} else {
|
||||
SimpleCommand::descriptions().to_string()
|
||||
}
|
||||
|
|
|
@ -51,16 +51,12 @@
|
|||
|
||||
use core::fmt;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
error::Error,
|
||||
fmt::{Display, Formatter, Write},
|
||||
};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use teloxide_core::{
|
||||
requests::{Request, Requester},
|
||||
types::{BotCommand, Me},
|
||||
};
|
||||
use teloxide_core::types::{BotCommand, Me};
|
||||
#[cfg(feature = "macros")]
|
||||
pub use teloxide_macros::BotCommands;
|
||||
|
||||
|
@ -282,7 +278,7 @@ pub enum ParseError {
|
|||
pub struct CommandDescriptions<'a> {
|
||||
global_description: Option<&'a str>,
|
||||
descriptions: &'a [CommandDescription<'a>],
|
||||
bot_username: Option<Cow<'a, str>>,
|
||||
bot_username: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// Description of a particular command, used in [`CommandDescriptions`].
|
||||
|
@ -331,21 +327,17 @@ impl<'a> CommandDescriptions<'a> {
|
|||
/// );
|
||||
/// ```
|
||||
pub fn username(self, bot_username: &'a str) -> Self {
|
||||
Self { bot_username: Some(Cow::Borrowed(bot_username)), ..self }
|
||||
Self { bot_username: Some(bot_username), ..self }
|
||||
}
|
||||
|
||||
/// Sets the username of the bot.
|
||||
///
|
||||
/// This is the same as [`username`], but uses `get_me` method of the bot to
|
||||
/// get the username.
|
||||
/// This is the same as [`username`], but uses value returned from `get_me`
|
||||
/// method to get the username.
|
||||
///
|
||||
/// [`username`]: self::CommandDescriptions::username
|
||||
pub async fn username_from_bot(self, bot: &impl Requester) -> CommandDescriptions<'a> {
|
||||
let Me { user, .. } = bot.get_me().send().await.expect("get_me failed");
|
||||
Self {
|
||||
bot_username: Some(Cow::Owned(user.username.expect("Bots must have usernames"))),
|
||||
..self
|
||||
}
|
||||
pub fn username_from_me(self, me: &'a Me) -> CommandDescriptions<'a> {
|
||||
self.username(me.user.username.as_deref().expect("Bots must have usernames"))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue