mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +01:00
Fix update_listeners
This commit is contained in:
parent
7433a2b072
commit
dce662064f
4 changed files with 30 additions and 9 deletions
|
@ -3,6 +3,7 @@ use teloxide::prelude::*;
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
std::env::set_var("RUST_LOG", "ping_pong_bot=trace");
|
std::env::set_var("RUST_LOG", "ping_pong_bot=trace");
|
||||||
|
std::env::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!");
|
||||||
|
|
||||||
|
|
|
@ -152,6 +152,7 @@ async fn handle_message(ctx: Ctx) -> Res {
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
std::env::set_var("RUST_LOG", "simple_dialogue=trace");
|
std::env::set_var("RUST_LOG", "simple_dialogue=trace");
|
||||||
|
std::env::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!");
|
||||||
|
|
||||||
|
|
|
@ -155,13 +155,30 @@ pub fn polling(
|
||||||
let updates = match req.send().await {
|
let updates = match req.send().await {
|
||||||
Err(err) => vec![Err(err)],
|
Err(err) => vec![Err(err)],
|
||||||
Ok(updates) => {
|
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
|
let updates = updates
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|update| match update {
|
.filter(|update| match update {
|
||||||
Err(error) => {
|
Err((value, error)) => {
|
||||||
log::error!("Cannot parse an update: {:?}! \
|
log::error!("Cannot parse an update.\nError: {:?}\nValue: {}\n\
|
||||||
This is a bug in teloxide, please open an issue here: \
|
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
|
false
|
||||||
}
|
}
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
|
@ -171,9 +188,6 @@ pub fn polling(
|
||||||
})
|
})
|
||||||
.collect::<Vec<Update>>();
|
.collect::<Vec<Update>>();
|
||||||
|
|
||||||
if let Some(upd) = updates.last() {
|
|
||||||
offset = upd.id + 1;
|
|
||||||
}
|
|
||||||
updates.into_iter().map(Ok).collect::<Vec<_>>()
|
updates.into_iter().map(Ok).collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,12 +32,14 @@ pub struct GetUpdates {
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl Request for GetUpdates {
|
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
|
/// Deserialize to `Vec<serde_json::Result<Update>>` instead of
|
||||||
/// `Vec<Update>`, because we want to parse the rest of updates even if our
|
/// `Vec<Update>`, because we want to parse the rest of updates even if our
|
||||||
/// library hasn't parsed one.
|
/// 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(
|
let value: Value = net::request_json(
|
||||||
self.bot.client(),
|
self.bot.client(),
|
||||||
self.bot.token(),
|
self.bot.token(),
|
||||||
|
@ -49,7 +51,10 @@ impl Request for GetUpdates {
|
||||||
match value {
|
match value {
|
||||||
Value::Array(array) => Ok(array
|
Value::Array(array) => Ok(array
|
||||||
.into_iter()
|
.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()),
|
.collect()),
|
||||||
_ => Err(RequestError::InvalidJson(
|
_ => Err(RequestError::InvalidJson(
|
||||||
serde_json::from_value::<Vec<Update>>(value)
|
serde_json::from_value::<Vec<Update>>(value)
|
||||||
|
|
Loading…
Reference in a new issue