diff --git a/examples/ping_pong_bot/src/main.rs b/examples/ping_pong_bot/src/main.rs index 5aa70401..70c30a88 100644 --- a/examples/ping_pong_bot/src/main.rs +++ b/examples/ping_pong_bot/src/main.rs @@ -3,6 +3,7 @@ use teloxide::prelude::*; #[tokio::main] async fn main() { std::env::set_var("RUST_LOG", "ping_pong_bot=trace"); + std::env::set_var("RUST_LOG", "teloxide=error"); pretty_env_logger::init(); log::info!("Starting the ping-pong bot!"); diff --git a/examples/simple_dialogue/src/main.rs b/examples/simple_dialogue/src/main.rs index 7c6aae3c..e02dc804 100644 --- a/examples/simple_dialogue/src/main.rs +++ b/examples/simple_dialogue/src/main.rs @@ -152,6 +152,7 @@ async fn handle_message(ctx: Ctx) -> Res { #[tokio::main] async fn main() { std::env::set_var("RUST_LOG", "simple_dialogue=trace"); + std::env::set_var("RUST_LOG", "teloxide=error"); pretty_env_logger::init(); log::info!("Starting the simple_dialogue bot!"); diff --git a/src/dispatching/update_listeners.rs b/src/dispatching/update_listeners.rs index d5b8864b..0b1190e3 100644 --- a/src/dispatching/update_listeners.rs +++ b/src/dispatching/update_listeners.rs @@ -155,13 +155,30 @@ pub fn polling( let updates = match req.send().await { Err(err) => vec![Err(err)], Ok(updates) => { + // Set offset to the last update's id + 1 + if let Some(upd) = updates.last() { + let id: i32 = match upd { + Ok(ok) => ok.id, + Err((value, _)) => value["update_id"] + .as_i64() + .expect( + "The 'update_id' field must always exist in \ + Update", + ) + .try_into() + .expect("update_id must be i32"), + }; + + offset = id + 1; + } + let updates = updates .into_iter() .filter(|update| match update { - Err(error) => { - log::error!("Cannot parse an update: {:?}! \ + Err((value, 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); + https://github.com/teloxide/teloxide/issues.", error, value); false } Ok(_) => true, @@ -171,9 +188,6 @@ pub fn polling( }) .collect::>(); - if let Some(upd) = updates.last() { - offset = upd.id + 1; - } updates.into_iter().map(Ok).collect::>() } }; diff --git a/src/requests/all/get_updates.rs b/src/requests/all/get_updates.rs index 05e9e84b..f3fbe0c4 100644 --- a/src/requests/all/get_updates.rs +++ b/src/requests/all/get_updates.rs @@ -32,12 +32,14 @@ pub struct GetUpdates { #[async_trait::async_trait] impl Request for GetUpdates { - type Output = Vec>; + type Output = Vec>; /// Deserialize to `Vec>` instead of /// `Vec`, because we want to parse the rest of updates even if our /// library hasn't parsed one. - async fn send(&self) -> ResponseResult>> { + async fn send( + &self, + ) -> ResponseResult>> { let value: Value = net::request_json( self.bot.client(), self.bot.token(), @@ -49,7 +51,10 @@ impl Request for GetUpdates { match value { Value::Array(array) => Ok(array .into_iter() - .map(|value| serde_json::from_str(&value.to_string())) + .map(|value| { + serde_json::from_str(&value.to_string()) + .map_err(|error| (value, error)) + }) .collect()), _ => Err(RequestError::InvalidJson( serde_json::from_value::>(value)