mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-24 17:22:43 +01:00
Fix the examples
This commit is contained in:
parent
a6e0c48427
commit
1af0bfdcbc
15 changed files with 44 additions and 51 deletions
|
@ -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.`
|
nightly = [] # currently used for `README.md` tests and building docs for `docsrs` to add `This is supported on feature="..." only.`
|
||||||
|
|
||||||
[dependencies]
|
[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_json = "1.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -9,7 +9,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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"] }
|
teloxide = { path = "../../", features = ["macros"] }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -14,7 +14,7 @@ frunk = "0.3.1"
|
||||||
frunk_core = "0.3.1"
|
frunk_core = "0.3.1"
|
||||||
|
|
||||||
futures = "0.3.5"
|
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 = { path = "../../", features = ["frunk"] }
|
||||||
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", branch = "master" }
|
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", branch = "master" }
|
||||||
|
|
|
@ -9,7 +9,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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 = "../../" }
|
teloxide = { path = "../../" }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -9,10 +9,11 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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 = "../../" }
|
teloxide = { path = "../../" }
|
||||||
|
tokio-stream = "0.1.4"
|
||||||
|
|
||||||
# Used to setup a webhook
|
# Used to setup a webhook
|
||||||
warp = "0.2.2"
|
warp = "0.3.0"
|
||||||
reqwest = "0.10.4"
|
reqwest = "0.10.4"
|
||||||
serde_json = "1.0.50"
|
serde_json = "1.0.50"
|
||||||
|
|
|
@ -5,6 +5,7 @@ use teloxide::{dispatching::update_listeners, prelude::*};
|
||||||
|
|
||||||
use std::{convert::Infallible, env, net::SocketAddr};
|
use std::{convert::Infallible, env, net::SocketAddr};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
|
|
||||||
use reqwest::StatusCode;
|
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::path(path))
|
||||||
.and(warp::body::json())
|
.and(warp::body::json())
|
||||||
.map(move |json: serde_json::Value| {
|
.map(move |json: serde_json::Value| {
|
||||||
let try_parse = match serde_json::from_str(&json.to_string()) {
|
if let Ok(update) = Update::try_parse(&json) {
|
||||||
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 {
|
|
||||||
tx.send(Ok(update)).expect("Cannot send an incoming update from the webhook")
|
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);
|
let address = format!("0.0.0.0:{}", port);
|
||||||
tokio::spawn(serve.run(address.parse::<SocketAddr>().unwrap()));
|
tokio::spawn(serve.run(address.parse::<SocketAddr>().unwrap()));
|
||||||
rx
|
UnboundedReceiverStream::new(rx)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run() {
|
async fn run() {
|
||||||
|
@ -78,7 +66,7 @@ async fn run() {
|
||||||
bot,
|
bot,
|
||||||
|message| async move {
|
|message| async move {
|
||||||
message.answer("pong").await?;
|
message.answer("pong").await?;
|
||||||
respond(())
|
respond(())
|
||||||
},
|
},
|
||||||
webhook(cloned_bot).await,
|
webhook(cloned_bot).await,
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,10 +9,11 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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 = "../../" }
|
teloxide = { path = "../../" }
|
||||||
|
|
||||||
# Used to setup a webhook
|
# Used to setup a webhook
|
||||||
warp = "0.2.2"
|
warp = "0.3.0"
|
||||||
reqwest = "0.10.4"
|
reqwest = "0.10.4"
|
||||||
serde_json = "1.0.50"
|
serde_json = "1.0.50"
|
||||||
|
|
|
@ -5,6 +5,7 @@ use teloxide::{dispatching::update_listeners, prelude::*};
|
||||||
|
|
||||||
use std::{convert::Infallible, net::SocketAddr};
|
use std::{convert::Infallible, net::SocketAddr};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
|
|
||||||
use reqwest::StatusCode;
|
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.
|
// setup a self-signed TLS certificate.
|
||||||
|
|
||||||
tokio::spawn(serve.run("127.0.0.1:80".parse::<SocketAddr>().unwrap()));
|
tokio::spawn(serve.run("127.0.0.1:80".parse::<SocketAddr>().unwrap()));
|
||||||
rx
|
UnboundedReceiverStream::new(rx)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run() {
|
async fn run() {
|
||||||
|
|
|
@ -7,7 +7,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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
|
# You can also choose "cbor-serializer" or built-in JSON serializer
|
||||||
teloxide = { path = "../../", features = ["redis-storage", "bincode-serializer"] }
|
teloxide = { path = "../../", features = ["redis-storage", "bincode-serializer"] }
|
||||||
|
|
|
@ -9,7 +9,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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"
|
tokio-stream = "0.1.3"
|
||||||
teloxide = { path = "../../" }
|
teloxide = { path = "../../" }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
|
|
@ -9,5 +9,5 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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"] }
|
teloxide = { path = "../../", features = ["macros"] }
|
||||||
|
|
|
@ -7,7 +7,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
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
|
# You can also choose "cbor-serializer" or built-in JSON serializer
|
||||||
teloxide = { path = "../../", features = ["sqlite-storage", "bincode-serializer", "redis-storage"] }
|
teloxide = { path = "../../", features = ["sqlite-storage", "bincode-serializer", "redis-storage"] }
|
||||||
|
|
BIN
examples/sqlite_remember_bot/db.sqlite-journal
Normal file
BIN
examples/sqlite_remember_bot/db.sqlite-journal
Normal file
Binary file not shown.
|
@ -86,12 +86,10 @@ where
|
||||||
dialogue: D,
|
dialogue: D,
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
|
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let prev_dialogue = match get_dialogue(&self.pool, chat_id).await? {
|
let prev_dialogue = get_dialogue(&self.pool, chat_id)
|
||||||
Some(d) => {
|
.await?
|
||||||
Some(self.serializer.deserialize(&d).map_err(SqliteStorageError::SerdeError)?)
|
.map(|d| self.serializer.deserialize(&d).map_err(SqliteStorageError::SerdeError))
|
||||||
}
|
.transpose()?;
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
let upd_dialogue =
|
let upd_dialogue =
|
||||||
self.serializer.serialize(&dialogue).map_err(SqliteStorageError::SerdeError)?;
|
self.serializer.serialize(&dialogue).map_err(SqliteStorageError::SerdeError)?;
|
||||||
self.pool
|
self.pool
|
||||||
|
@ -122,16 +120,11 @@ async fn get_dialogue(
|
||||||
pool: &SqlitePool,
|
pool: &SqlitePool,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
) -> Result<Option<Box<Vec<u8>>>, sqlx::Error> {
|
) -> Result<Option<Box<Vec<u8>>>, sqlx::Error> {
|
||||||
Ok(
|
Ok(sqlx::query_as::<_, DialogueDbRow>(
|
||||||
match sqlx::query_as::<_, DialogueDbRow>(
|
"SELECT dialogue FROM teloxide_dialogues WHERE chat_id = ?",
|
||||||
"SELECT dialogue FROM teloxide_dialogues WHERE chat_id = ?",
|
|
||||||
)
|
|
||||||
.bind(chat_id)
|
|
||||||
.fetch_optional(pool)
|
|
||||||
.await?
|
|
||||||
{
|
|
||||||
Some(r) => Some(Box::new(r.dialogue)),
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
.bind(chat_id)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
.map(|r| Box::new(r.dialogue)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,10 +179,19 @@ where
|
||||||
offset = id + 1;
|
offset = id + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let updates =
|
for update in &updates {
|
||||||
updates.into_iter().filter_map(Result::ok).collect::<Vec<Update>>();
|
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<_>>()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue