Rust challenge 50/100 - telegram bot
Table of content
What is this
The rules of the game are explained in my original. I haven’t posted in a while but I want to get started again.
50th Challenge
Challenge
Today the challenge is to create a telegram bot that displays a random number between 0 and 100.
Solution
This was failry simple but fun. The hard work was done by the teloxide cargo.
use teloxide::prelude::*;
use rand::Rng;
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let bot = Bot::from_env().auto_send();
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
let random_number = rand::thread_rng().gen_range(0..100);
bot.send_message(message.chat.id,format!("{}",random_number)).await?;
respond(())
})
.await;
}
Once we define the token setup by the botfather
we can start the bot and interact with it which looks like the following:
See github for today’s simple snippet.