Update examples/simple_dialogue

This commit is contained in:
Temirkhan Myrzamadi 2020-02-12 16:56:58 +06:00
parent 26d721d928
commit 3aeb9c4295
2 changed files with 16 additions and 10 deletions

View file

@ -1,19 +1,21 @@
use teloxide::prelude::*; use teloxide::prelude::*;
use std::env::{set_var, var};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Configure a fancy logger. Let this bot print everything, but restrict // Configure a fancy logger. Let this bot print everything, but restrict
// teloxide to only log errors. // teloxide to only log errors.
std::env::set_var("RUST_LOG", "ping_pong_bot=trace"); set_var("RUST_LOG", "ping_pong_bot=trace");
std::env::set_var("RUST_LOG", "teloxide=error"); set_var("RUST_LOG", "teloxide=error");
pretty_env_logger::init(); pretty_env_logger::init();
log::info!("Starting the ping-pong bot!"); log::info!("Starting the ping-pong bot!");
// Creates a dispatcher of updates with the specified bot. Don't forget to let bot = Bot::new(var("TELOXIDE_TOKEN").unwrap());
// replace `MyAwesomeToken` with yours.
Dispatcher::<RequestError>::new(Bot::new("MyAwesomeToken")) // Create a dispatcher with a single message handler that answers "pong" to
// Registers a message handler. Inside a body of the closure, answer // each incoming message.
// `"pong"` to an incoming message. Dispatcher::<RequestError>::new(bot)
.message_handler(&|ctx: DispatcherHandlerCtx<Message>| async move { .message_handler(&|ctx: DispatcherHandlerCtx<Message>| async move {
ctx.answer("pong").send().await?; ctx.answer("pong").send().await?;
Ok(()) Ok(())

View file

@ -3,6 +3,8 @@
#[macro_use] #[macro_use]
extern crate smart_default; extern crate smart_default;
use std::env::{set_var, var};
use teloxide::{ use teloxide::{
prelude::*, prelude::*,
types::{KeyboardButton, ReplyKeyboardMarkup}, types::{KeyboardButton, ReplyKeyboardMarkup},
@ -173,12 +175,14 @@ async fn handle_message(ctx: Ctx<Dialogue>) -> Res {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
std::env::set_var("RUST_LOG", "simple_dialogue=trace"); set_var("RUST_LOG", "simple_dialogue=trace");
std::env::set_var("RUST_LOG", "teloxide=error"); set_var("RUST_LOG", "teloxide=error");
pretty_env_logger::init(); pretty_env_logger::init();
log::info!("Starting the simple_dialogue bot!"); log::info!("Starting the simple_dialogue bot!");
Dispatcher::new(Bot::new("MyAwesomeToken")) let bot = Bot::new(var("TELOXIDE_TOKEN").unwrap());
Dispatcher::new(bot)
.message_handler(&DialogueDispatcher::new(|ctx| async move { .message_handler(&DialogueDispatcher::new(|ctx| async move {
handle_message(ctx) handle_message(ctx)
.await .await