From 67a781fb20f9ebf4276155ef7ad34074152645b9 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 14 Feb 2020 04:35:36 +0600 Subject: [PATCH] Modify examples/simple_commands_bot --- examples/simple_commands_bot/Cargo.toml | 1 + examples/simple_commands_bot/src/main.rs | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/simple_commands_bot/Cargo.toml b/examples/simple_commands_bot/Cargo.toml index f3e9db65..6baf6eb8 100644 --- a/examples/simple_commands_bot/Cargo.toml +++ b/examples/simple_commands_bot/Cargo.toml @@ -9,5 +9,6 @@ edition = "2018" [dependencies] log = "0.4.8" tokio = "0.2.9" +rand = "0.7.3" pretty_env_logger = "0.4.0" teloxide = { path = "../../" } diff --git a/examples/simple_commands_bot/src/main.rs b/examples/simple_commands_bot/src/main.rs index 20ceebbc..3d6525c5 100644 --- a/examples/simple_commands_bot/src/main.rs +++ b/examples/simple_commands_bot/src/main.rs @@ -1,5 +1,7 @@ use teloxide::{prelude::*, utils::command::BotCommand}; +use rand::{thread_rng, Rng}; + #[derive(BotCommand)] #[command(rename = "lowercase", description = "These commands are supported:")] enum Command { @@ -7,10 +9,8 @@ enum Command { Help, #[command(description = "be a cat.")] Meow, - #[command(description = "be a dog.")] - Woof, - #[command(description = "be a cow.")] - Moo, + #[command(description = "generate a random number within [0; 1).")] + Generate, } async fn handle_command( @@ -34,9 +34,12 @@ async fn handle_command( match command { Command::Help => ctx.answer(Command::descriptions()).send().await?, + Command::Generate => { + ctx.answer(thread_rng().gen_range(0.0, 1.0).to_string()) + .send() + .await? + } Command::Meow => ctx.answer("I am a cat! Meow!").send().await?, - Command::Woof => ctx.answer("I am a dog! Woof!").send().await?, - Command::Moo => ctx.answer("I am a cow! Moo!").send().await?, }; Ok(())