diff --git a/Cargo.toml b/Cargo.toml index 10f575ff..5eae9a4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ macros = ["teloxide-macros"] nightly = [] # currently used for `README.md` tests and building docs for `docsrs` to add `This is supported on feature="..." only.` [dependencies] -teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", rev = "b465da5f1650893cc033d995343858371505eaf1", features = ["full"] } +teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", features = ["full"] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } diff --git a/examples/admin_bot/Cargo.toml b/examples/admin_bot/Cargo.toml index 8f2031bc..e598c3f9 100644 --- a/examples/admin_bot/Cargo.toml +++ b/examples/admin_bot/Cargo.toml @@ -9,8 +9,8 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } teloxide = { path = "../../", features = ["macros"] } [profile.release] -lto = true \ No newline at end of file +lto = true diff --git a/examples/dialogue_bot/Cargo.toml b/examples/dialogue_bot/Cargo.toml index 3d36bb3c..c8e7cbc4 100644 --- a/examples/dialogue_bot/Cargo.toml +++ b/examples/dialogue_bot/Cargo.toml @@ -14,7 +14,7 @@ frunk = "0.3.1" frunk_core = "0.3.1" futures = "0.3.5" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } teloxide = { path = "../../", features = ["frunk"] } teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", branch = "master" } diff --git a/examples/dices_bot/Cargo.toml b/examples/dices_bot/Cargo.toml index 3c2a6ed8..b1b22c42 100644 --- a/examples/dices_bot/Cargo.toml +++ b/examples/dices_bot/Cargo.toml @@ -9,8 +9,8 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } teloxide = { path = "../../" } [profile.release] -lto = true \ No newline at end of file +lto = true diff --git a/examples/heroku_ping_pong_bot/Cargo.toml b/examples/heroku_ping_pong_bot/Cargo.toml index d568e8a8..b27602b2 100644 --- a/examples/heroku_ping_pong_bot/Cargo.toml +++ b/examples/heroku_ping_pong_bot/Cargo.toml @@ -9,10 +9,11 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } teloxide = { path = "../../" } +tokio-stream = "0.1.4" # Used to setup a webhook -warp = "0.2.2" +warp = "0.3.0" reqwest = "0.10.4" serde_json = "1.0.50" diff --git a/examples/heroku_ping_pong_bot/src/main.rs b/examples/heroku_ping_pong_bot/src/main.rs index 1cfdb396..8c4837ab 100644 --- a/examples/heroku_ping_pong_bot/src/main.rs +++ b/examples/heroku_ping_pong_bot/src/main.rs @@ -5,6 +5,7 @@ use teloxide::{dispatching::update_listeners, prelude::*}; use std::{convert::Infallible, env, net::SocketAddr}; use tokio::sync::mpsc; +use tokio_stream::wrappers::UnboundedReceiverStream; use warp::Filter; use reqwest::StatusCode; @@ -39,20 +40,7 @@ pub async fn webhook<'a>(bot: AutoSend) -> impl update_listeners::UpdateLis .and(warp::path(path)) .and(warp::body::json()) .map(move |json: serde_json::Value| { - let try_parse = match serde_json::from_str(&json.to_string()) { - Ok(update) => Ok(update), - Err(error) => { - log::error!( - "Cannot parse an update.\nError: {:?}\nValue: {}\n\ - This is a bug in teloxide, please open an issue here: \ - https://github.com/teloxide/teloxide/issues.", - error, - json - ); - Err(error) - } - }; - if let Ok(update) = try_parse { + if let Ok(update) = Update::try_parse(&json) { tx.send(Ok(update)).expect("Cannot send an incoming update from the webhook") } @@ -64,7 +52,7 @@ pub async fn webhook<'a>(bot: AutoSend) -> impl update_listeners::UpdateLis let address = format!("0.0.0.0:{}", port); tokio::spawn(serve.run(address.parse::().unwrap())); - rx + UnboundedReceiverStream::new(rx) } async fn run() { @@ -78,7 +66,7 @@ async fn run() { bot, |message| async move { message.answer("pong").await?; - respond(()) + respond(()) }, webhook(cloned_bot).await, ) diff --git a/examples/ngrok_ping_pong_bot/Cargo.toml b/examples/ngrok_ping_pong_bot/Cargo.toml index 9522426f..524484e3 100644 --- a/examples/ngrok_ping_pong_bot/Cargo.toml +++ b/examples/ngrok_ping_pong_bot/Cargo.toml @@ -9,10 +9,11 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } +tokio-stream = "0.1.4" teloxide = { path = "../../" } # Used to setup a webhook -warp = "0.2.2" +warp = "0.3.0" reqwest = "0.10.4" serde_json = "1.0.50" diff --git a/examples/ngrok_ping_pong_bot/src/main.rs b/examples/ngrok_ping_pong_bot/src/main.rs index 9f7ae1d0..9d614807 100644 --- a/examples/ngrok_ping_pong_bot/src/main.rs +++ b/examples/ngrok_ping_pong_bot/src/main.rs @@ -5,6 +5,7 @@ use teloxide::{dispatching::update_listeners, prelude::*}; use std::{convert::Infallible, net::SocketAddr}; use tokio::sync::mpsc; +use tokio_stream::wrappers::UnboundedReceiverStream; use warp::Filter; use reqwest::StatusCode; @@ -45,7 +46,7 @@ pub async fn webhook<'a>(bot: AutoSend) -> impl update_listeners::UpdateLis // setup a self-signed TLS certificate. tokio::spawn(serve.run("127.0.0.1:80".parse::().unwrap())); - rx + UnboundedReceiverStream::new(rx) } async fn run() { diff --git a/examples/redis_remember_bot/Cargo.toml b/examples/redis_remember_bot/Cargo.toml index f7843219..18f4e618 100644 --- a/examples/redis_remember_bot/Cargo.toml +++ b/examples/redis_remember_bot/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } # You can also choose "cbor-serializer" or built-in JSON serializer teloxide = { path = "../../", features = ["redis-storage", "bincode-serializer"] } diff --git a/examples/shared_state_bot/Cargo.toml b/examples/shared_state_bot/Cargo.toml index f8127852..ecb937ac 100644 --- a/examples/shared_state_bot/Cargo.toml +++ b/examples/shared_state_bot/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } tokio-stream = "0.1.3" teloxide = { path = "../../" } lazy_static = "1.4.0" diff --git a/examples/simple_commands_bot/Cargo.toml b/examples/simple_commands_bot/Cargo.toml index dcf9aa20..38814c5f 100644 --- a/examples/simple_commands_bot/Cargo.toml +++ b/examples/simple_commands_bot/Cargo.toml @@ -9,5 +9,5 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } teloxide = { path = "../../", features = ["macros"] } diff --git a/examples/sqlite_remember_bot/Cargo.toml b/examples/sqlite_remember_bot/Cargo.toml index cf4cb204..c61fbfd2 100644 --- a/examples/sqlite_remember_bot/Cargo.toml +++ b/examples/sqlite_remember_bot/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] log = "0.4.8" pretty_env_logger = "0.4.0" -tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } +tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] } # You can also choose "cbor-serializer" or built-in JSON serializer teloxide = { path = "../../", features = ["sqlite-storage", "bincode-serializer", "redis-storage"] } diff --git a/examples/sqlite_remember_bot/db.sqlite-journal b/examples/sqlite_remember_bot/db.sqlite-journal new file mode 100644 index 00000000..5d08624e Binary files /dev/null and b/examples/sqlite_remember_bot/db.sqlite-journal differ diff --git a/src/dispatching/dialogue/storage/sqlite_storage.rs b/src/dispatching/dialogue/storage/sqlite_storage.rs index 244da6d0..f4e4d98c 100644 --- a/src/dispatching/dialogue/storage/sqlite_storage.rs +++ b/src/dispatching/dialogue/storage/sqlite_storage.rs @@ -86,12 +86,10 @@ where dialogue: D, ) -> BoxFuture<'static, Result, Self::Error>> { Box::pin(async move { - let prev_dialogue = match get_dialogue(&self.pool, chat_id).await? { - Some(d) => { - Some(self.serializer.deserialize(&d).map_err(SqliteStorageError::SerdeError)?) - } - _ => None, - }; + let prev_dialogue = get_dialogue(&self.pool, chat_id) + .await? + .map(|d| self.serializer.deserialize(&d).map_err(SqliteStorageError::SerdeError)) + .transpose()?; let upd_dialogue = self.serializer.serialize(&dialogue).map_err(SqliteStorageError::SerdeError)?; self.pool @@ -122,16 +120,11 @@ async fn get_dialogue( pool: &SqlitePool, chat_id: i64, ) -> Result>>, sqlx::Error> { - Ok( - match sqlx::query_as::<_, DialogueDbRow>( - "SELECT dialogue FROM teloxide_dialogues WHERE chat_id = ?", - ) - .bind(chat_id) - .fetch_optional(pool) - .await? - { - Some(r) => Some(Box::new(r.dialogue)), - _ => None, - }, + Ok(sqlx::query_as::<_, DialogueDbRow>( + "SELECT dialogue FROM teloxide_dialogues WHERE chat_id = ?", ) + .bind(chat_id) + .fetch_optional(pool) + .await? + .map(|r| Box::new(r.dialogue))) } diff --git a/src/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index 8f7432c7..0632611d 100644 --- a/src/dispatching/update_listeners.rs +++ b/src/dispatching/update_listeners.rs @@ -179,10 +179,19 @@ where offset = id + 1; } - let updates = - updates.into_iter().filter_map(Result::ok).collect::>(); + for update in &updates { + if let Err((value, e)) = update { + log::error!( + "Cannot parse an update.\nError: {:?}\nValue: {}\n\ + This is a bug in teloxide-core, please open an issue here: \ + https://github.com/teloxide/teloxide-core/issues.", + e, + value + ); + } + } - updates.into_iter().map(Ok).collect::>() + updates.into_iter().filter_map(Result::ok).map(Ok).collect::>() } };