Fix the examples

This commit is contained in:
Temirkhan Myrzamadi 2021-03-13 22:13:24 +06:00
parent a6e0c48427
commit 1af0bfdcbc
15 changed files with 44 additions and 51 deletions

View file

@ -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"] }

View file

@ -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
lto = true

View file

@ -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" }

View file

@ -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
lto = true

View file

@ -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"

View file

@ -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<Bot>) -> 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<Bot>) -> impl update_listeners::UpdateLis
let address = format!("0.0.0.0:{}", port);
tokio::spawn(serve.run(address.parse::<SocketAddr>().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,
)

View file

@ -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"

View file

@ -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<Bot>) -> impl update_listeners::UpdateLis
// setup a self-signed TLS certificate.
tokio::spawn(serve.run("127.0.0.1:80".parse::<SocketAddr>().unwrap()));
rx
UnboundedReceiverStream::new(rx)
}
async fn run() {

View file

@ -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"] }

View file

@ -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"

View file

@ -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"] }

View file

@ -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"] }

Binary file not shown.

View file

@ -86,12 +86,10 @@ where
dialogue: D,
) -> BoxFuture<'static, Result<Option<D>, 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<Option<Box<Vec<u8>>>, 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)))
}

View file

@ -179,10 +179,19 @@ where
offset = id + 1;
}
let updates =
updates.into_iter().filter_map(Result::ok).collect::<Vec<Update>>();
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::<Vec<_>>()
updates.into_iter().filter_map(Result::ok).map(Ok).collect::<Vec<_>>()
}
};