Use same identifiers across the examples

Former-commit-id: a137b41ab2
This commit is contained in:
Hirrolot 2022-10-03 17:07:38 +06:00
parent 2bdea18ec3
commit d3d7cc2fa9
6 changed files with 25 additions and 26 deletions

View file

@ -63,8 +63,8 @@ async fn main() {
teloxide::commands_repl(bot, action, Command::ty()).await;
}
async fn action(bot: Bot, msg: Message, command: Command) -> ResponseResult<()> {
match command {
async fn action(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
match cmd {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
}

View file

@ -58,21 +58,25 @@ fn make_keyboard() -> InlineKeyboardMarkup {
/// Parse the text wrote on Telegram and check if that text is a valid command
/// or not, then match the command. If the command is `/start` it writes a
/// markup with the `InlineKeyboardMarkup`.
async fn message_handler(m: Message, bot: Bot, me: Me) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(text) = m.text() {
async fn message_handler(
msg: Message,
bot: Bot,
me: Me,
) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(text) = msg.text() {
match BotCommands::parse(text, me.username()) {
Ok(Command::Help) => {
// Just send the description of all commands.
bot.send_message(m.chat.id, Command::descriptions().to_string()).await?;
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
}
Ok(Command::Start) => {
// Create a list of buttons and send them.
let keyboard = make_keyboard();
bot.send_message(m.chat.id, "Debian versions:").reply_markup(keyboard).await?;
bot.send_message(msg.chat.id, "Debian versions:").reply_markup(keyboard).await?;
}
Err(_) => {
bot.send_message(m.chat.id, "Command not found!").await?;
bot.send_message(msg.chat.id, "Command not found!").await?;
}
}
}

View file

@ -21,20 +21,15 @@ 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}."),
)
.await?
bot.send_message(msg.chat.id, format!("Your username is @{username} and age is {age}."))
.await?
}
};

View file

@ -104,8 +104,8 @@ async fn receive_location(
) -> 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 => {

View file

@ -14,7 +14,7 @@ async fn main() {
let bot = Bot::from_env();
let handler = Update::filter_inline_query().branch(dptree::endpoint(
|query: InlineQuery, bot: Bot| async move {
|q: InlineQuery, bot: Bot| 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
@ -26,7 +26,7 @@ async fn main() {
// What message will be sent when clicked/tapped
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://www.google.com/search?q={}",
query.query,
q.query,
))),
);
// While constructing them from the struct itself is possible, it is preferred
@ -38,7 +38,7 @@ async fn main() {
"DuckDuckGo Search".to_string(),
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://duckduckgo.com/?q={}",
query.query
q.query
))),
)
.description("DuckDuckGo Search")
@ -52,7 +52,7 @@ async fn main() {
// Send it off! One thing to note -- the ID we use here must be of the query
// we're responding to.
let response = bot.answer_inline_query(&query.id, results).send().await;
let response = bot.answer_inline_query(&q.id, results).send().await;
if let Err(err) = response {
log::error!("Error in handler: {:?}", err);
}

View file

@ -9,8 +9,8 @@ async fn main() {
let bot = Bot::from_env();
teloxide::repl(bot, |message: Message, bot: Bot| async move {
bot.send_dice(message.chat.id).await?;
teloxide::repl(bot, |msg: Message, bot: Bot| async move {
bot.send_dice(msg.chat.id).await?;
Ok(())
})
.await;