mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Reorder parameters in the examples
In reordering the parameters, I stick the following principle: place parameters from least changing to most changing. Thus, we have config and bot right from the beginning, next a dialogue with a possible payload, and next updates such as messages, inline queries, etc. This principle is used in languages with a native support for currying, although in Rust people appear to order parameters arbitrarily, so this commit is mostly for the sake of consistency.
Former-commit-id: 5922984f6c
This commit is contained in:
parent
d3d7cc2fa9
commit
53be9d0e92
14 changed files with 47 additions and 52 deletions
29
README.md
29
README.md
|
@ -94,7 +94,7 @@ async fn main() {
|
|||
|
||||
let bot = Bot::from_env();
|
||||
|
||||
teloxide::repl(bot, |message: Message, bot: Bot| async move {
|
||||
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
bot.send_dice(message.chat.id).await?;
|
||||
Ok(())
|
||||
})
|
||||
|
@ -143,19 +143,14 @@ enum Command {
|
|||
UsernameAndAge { username: String, age: u8 },
|
||||
}
|
||||
|
||||
async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult<()> {
|
||||
match command {
|
||||
Command::Help => {
|
||||
bot.send_message(message.chat.id, Command::descriptions().to_string()).await?
|
||||
}
|
||||
async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
|
||||
match cmd {
|
||||
Command::Help => bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?,
|
||||
Command::Username(username) => {
|
||||
bot.send_message(message.chat.id, format!("Your username is @{username}.")).await?
|
||||
bot.send_message(msg.chat.id, format!("Your username is @{username}.")).await?
|
||||
}
|
||||
Command::UsernameAndAge { username, age } => {
|
||||
bot.send_message(
|
||||
message.chat.id,
|
||||
format!("Your username is @{username} and age is {age}."),
|
||||
)
|
||||
bot.send_message(msg.chat.id, format!("Your username is @{username} and age is {age}."))
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
@ -223,13 +218,13 @@ async fn main() {
|
|||
.await;
|
||||
}
|
||||
|
||||
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||
dialogue.update(State::ReceiveFullName).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
match msg.text() {
|
||||
Some(text) => {
|
||||
bot.send_message(msg.chat.id, "How old are you?").await?;
|
||||
|
@ -245,9 +240,9 @@ async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> Hand
|
|||
|
||||
async fn receive_age(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
dialogue: MyDialogue,
|
||||
full_name: String, // Available from `State::ReceiveAge`.
|
||||
msg: Message,
|
||||
) -> HandlerResult {
|
||||
match msg.text().map(|text| text.parse::<u8>()) {
|
||||
Some(Ok(age)) => {
|
||||
|
@ -264,14 +259,14 @@ async fn receive_age(
|
|||
|
||||
async fn receive_location(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
dialogue: MyDialogue,
|
||||
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||
msg: Message,
|
||||
) -> HandlerResult {
|
||||
match msg.text() {
|
||||
Some(location) => {
|
||||
let message = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
|
||||
bot.send_message(msg.chat.id, message).await?;
|
||||
let report = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
|
||||
bot.send_message(msg.chat.id, report).await?;
|
||||
dialogue.exit().await?;
|
||||
}
|
||||
None => {
|
||||
|
|
|
@ -59,8 +59,8 @@ fn make_keyboard() -> InlineKeyboardMarkup {
|
|||
/// or not, then match the command. If the command is `/start` it writes a
|
||||
/// markup with the `InlineKeyboardMarkup`.
|
||||
async fn message_handler(
|
||||
msg: Message,
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
me: Me,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
if let Some(text) = msg.text() {
|
||||
|
@ -85,8 +85,8 @@ async fn message_handler(
|
|||
}
|
||||
|
||||
async fn inline_query_handler(
|
||||
q: InlineQuery,
|
||||
bot: Bot,
|
||||
q: InlineQuery,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let choose_debian_version = InlineQueryResultArticle::new(
|
||||
"0",
|
||||
|
@ -105,7 +105,7 @@ async fn inline_query_handler(
|
|||
///
|
||||
/// **IMPORTANT**: do not send privacy-sensitive data this way!!!
|
||||
/// Anyone can read data stored in the callback button.
|
||||
async fn callback_handler(q: CallbackQuery, bot: Bot) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
async fn callback_handler(bot: Bot, q: CallbackQuery) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
if let Some(version) = q.data {
|
||||
let text = format!("You chose: {version}");
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ async fn main() {
|
|||
.await;
|
||||
}
|
||||
|
||||
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
match msg.text().map(|text| text.parse::<i32>()) {
|
||||
Some(Ok(n)) => {
|
||||
dialogue.update(State::GotNumber(n)).await?;
|
||||
|
@ -80,9 +80,9 @@ async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
|||
|
||||
async fn got_number(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
dialogue: MyDialogue,
|
||||
num: i32,
|
||||
num: i32, // Available from `State::GotNumber`.
|
||||
msg: Message,
|
||||
cmd: Command,
|
||||
) -> HandlerResult {
|
||||
match cmd {
|
||||
|
|
|
@ -57,13 +57,13 @@ async fn main() {
|
|||
.await;
|
||||
}
|
||||
|
||||
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||
dialogue.update(State::ReceiveFullName).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
match msg.text() {
|
||||
Some(text) => {
|
||||
bot.send_message(msg.chat.id, "How old are you?").await?;
|
||||
|
@ -79,9 +79,9 @@ async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> Hand
|
|||
|
||||
async fn receive_age(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
dialogue: MyDialogue,
|
||||
full_name: String, // Available from `State::ReceiveAge`.
|
||||
msg: Message,
|
||||
) -> HandlerResult {
|
||||
match msg.text().map(|text| text.parse::<u8>()) {
|
||||
Some(Ok(age)) => {
|
||||
|
@ -98,9 +98,9 @@ async fn receive_age(
|
|||
|
||||
async fn receive_location(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
dialogue: MyDialogue,
|
||||
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||
msg: Message,
|
||||
) -> HandlerResult {
|
||||
match msg.text() {
|
||||
Some(location) => {
|
||||
|
|
|
@ -33,7 +33,7 @@ async fn main() {
|
|||
)
|
||||
.branch(
|
||||
// Filter a maintainer by a used ID.
|
||||
dptree::filter(|msg: Message, cfg: ConfigParameters| {
|
||||
dptree::filter(|cfg: ConfigParameters, msg: Message| {
|
||||
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
|
||||
})
|
||||
.filter_command::<MaintainerCommands>()
|
||||
|
@ -62,7 +62,7 @@ async fn main() {
|
|||
.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: Bot| async move {
|
||||
Message::filter_dice().endpoint(|bot: Bot, msg: Message, dice: Dice| async move {
|
||||
bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
|
||||
.reply_to_message_id(msg.id)
|
||||
.await?;
|
||||
|
@ -114,11 +114,11 @@ enum MaintainerCommands {
|
|||
}
|
||||
|
||||
async fn simple_commands_handler(
|
||||
msg: Message,
|
||||
bot: Bot,
|
||||
cmd: SimpleCommand,
|
||||
cfg: ConfigParameters,
|
||||
bot: Bot,
|
||||
me: teloxide::types::Me,
|
||||
msg: Message,
|
||||
cmd: SimpleCommand,
|
||||
) -> Result<(), teloxide::RequestError> {
|
||||
let text = match cmd {
|
||||
SimpleCommand::Help => {
|
||||
|
|
|
@ -47,7 +47,7 @@ async fn main() {
|
|||
|
||||
teloxide::repl_with_listener(
|
||||
bot,
|
||||
|msg: Message, bot: Bot| async move {
|
||||
|bot: Bot, msg: Message| async move {
|
||||
bot.send_message(msg.chat.id, "pong").await?;
|
||||
Ok(())
|
||||
},
|
||||
|
|
|
@ -14,7 +14,7 @@ async fn main() {
|
|||
let bot = Bot::from_env();
|
||||
|
||||
let handler = Update::filter_inline_query().branch(dptree::endpoint(
|
||||
|q: InlineQuery, bot: Bot| async move {
|
||||
|bot: Bot, q: InlineQuery| async move {
|
||||
// First, create your actual response
|
||||
let google_search = InlineQueryResultArticle::new(
|
||||
// Each item needs a unique ID, as well as the response container for the
|
||||
|
|
|
@ -18,7 +18,7 @@ async fn main() {
|
|||
|
||||
teloxide::repl_with_listener(
|
||||
bot,
|
||||
|msg: Message, bot: Bot| async move {
|
||||
|bot: Bot, msg: Message| async move {
|
||||
bot.send_message(msg.chat.id, "pong").await?;
|
||||
Ok(())
|
||||
},
|
||||
|
|
|
@ -83,7 +83,7 @@ fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>>
|
|||
.branch(callback_query_handler)
|
||||
}
|
||||
|
||||
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||
dialogue.update(State::ReceiveFullName).await?;
|
||||
Ok(())
|
||||
|
@ -94,7 +94,7 @@ async fn help(bot: Bot, msg: Message) -> HandlerResult {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn cancel(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
|
||||
dialogue.exit().await?;
|
||||
Ok(())
|
||||
|
@ -106,7 +106,7 @@ async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
match msg.text().map(ToOwned::to_owned) {
|
||||
Some(full_name) => {
|
||||
let products = ["Apple", "Banana", "Orange", "Potato"]
|
||||
|
@ -127,9 +127,9 @@ async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> Hand
|
|||
|
||||
async fn receive_product_selection(
|
||||
bot: Bot,
|
||||
q: CallbackQuery,
|
||||
dialogue: MyDialogue,
|
||||
full_name: String,
|
||||
full_name: String, // Available from `State::ReceiveProductChoice`.
|
||||
q: CallbackQuery,
|
||||
) -> HandlerResult {
|
||||
if let Some(product) = &q.data {
|
||||
bot.send_message(
|
||||
|
|
|
@ -16,7 +16,7 @@ async fn main() {
|
|||
let messages_total = Arc::new(AtomicU64::new(0));
|
||||
|
||||
let handler = Update::filter_message().endpoint(
|
||||
|msg: Message, bot: Bot, messages_total: Arc<AtomicU64>| async move {
|
||||
|bot: Bot, messages_total: Arc<AtomicU64>, msg: Message| async move {
|
||||
let previous = messages_total.fetch_add(1, Ordering::Relaxed);
|
||||
bot.send_message(msg.chat.id, format!("I received {previous} messages in total."))
|
||||
.await?;
|
||||
|
|
|
@ -9,7 +9,7 @@ async fn main() {
|
|||
|
||||
let bot = Bot::from_env();
|
||||
|
||||
teloxide::repl(bot, |msg: Message, bot: Bot| async move {
|
||||
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
bot.send_dice(msg.chat.id).await?;
|
||||
Ok(())
|
||||
})
|
||||
|
|
|
@ -113,26 +113,26 @@
|
|||
//! type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||
//! type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||
//!
|
||||
//! async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
//! async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! async fn help(bot: Bot, msg: Message) -> HandlerResult {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! async fn cancel(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
//! async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||
//! async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! async fn receive_product_selection(
|
||||
//! bot: Bot,
|
||||
//! q: CallbackQuery,
|
||||
//! dialogue: MyDialogue,
|
||||
//! full_name: String,
|
||||
//! full_name: String, // Available from `State::ReceiveProductChoice`.
|
||||
//! q: CallbackQuery,
|
||||
//! ) -> HandlerResult {
|
||||
//! todo!()
|
||||
//! }
|
||||
|
|
|
@ -39,9 +39,9 @@
|
|||
//! # #[derive(Clone, Debug)] enum State { ReceiveLocation { full_name: String, age: u8 } }
|
||||
//! async fn receive_age(
|
||||
//! bot: Bot,
|
||||
//! msg: Message,
|
||||
//! dialogue: MyDialogue,
|
||||
//! full_name: String, // Available from `State::ReceiveAge`.
|
||||
//! msg: Message,
|
||||
//! ) -> HandlerResult {
|
||||
//! match msg.text().map(|text| text.parse::<u8>()) {
|
||||
//! Some(Ok(age)) => {
|
||||
|
@ -71,9 +71,9 @@
|
|||
//! # #[derive(Clone, Debug)] enum State {}
|
||||
//! async fn receive_location(
|
||||
//! bot: Bot,
|
||||
//! msg: Message,
|
||||
//! dialogue: MyDialogue,
|
||||
//! (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||
//! msg: Message,
|
||||
//! ) -> HandlerResult {
|
||||
//! match msg.text() {
|
||||
//! Some(location) => {
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
//!
|
||||
//! let bot = Bot::from_env();
|
||||
//!
|
||||
//! teloxide::repl(bot, |message: Message, bot: Bot| async move {
|
||||
//! bot.send_dice(message.chat.id).await?;
|
||||
//! teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
//! bot.send_dice(msg.chat.id).await?;
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! .await;
|
||||
|
|
Loading…
Reference in a new issue