Fix update_listeners

This commit is contained in:
Temirkhan Myrzamadi 2020-02-12 04:50:10 +06:00
parent 7433a2b072
commit dce662064f
4 changed files with 30 additions and 9 deletions

View file

@ -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!");

View file

@ -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!");

View file

@ -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::<Vec<Update>>();
if let Some(upd) = updates.last() {
offset = upd.id + 1;
}
updates.into_iter().map(Ok).collect::<Vec<_>>()
}
};

View file

@ -32,12 +32,14 @@ pub struct GetUpdates {
#[async_trait::async_trait]
impl Request for GetUpdates {
type Output = Vec<serde_json::Result<Update>>;
type Output = Vec<Result<Update, (Value, serde_json::Error)>>;
/// Deserialize to `Vec<serde_json::Result<Update>>` instead of
/// `Vec<Update>`, because we want to parse the rest of updates even if our
/// library hasn't parsed one.
async fn send(&self) -> ResponseResult<Vec<serde_json::Result<Update>>> {
async fn send(
&self,
) -> ResponseResult<Vec<Result<Update, (Value, serde_json::Error)>>> {
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::<Vec<Update>>(value)