Merge branch 'rework-dialogue-bot' of https://github.com/teloxide/teloxide into rework-dialogue-bot

This commit is contained in:
Temirkhan Myrzamadi 2020-07-24 18:28:50 +06:00
commit 4e51eb9127
4 changed files with 33 additions and 6 deletions

View file

@ -29,7 +29,7 @@ cbor-serializer = ["serde_cbor"]
bincode-serializer = ["bincode"]
[dependencies]
serde_json = "1.0.56"
serde_json = "1.0.55"
serde = { version = "1.0.114", features = ["derive"] }
tokio = { version = "0.2.21", features = ["full"] }

View file

@ -1,6 +1,9 @@
use crate::types::ParseMode;
use reqwest::Client;
use std::sync::Arc;
use reqwest::{
header::{HeaderMap, CONNECTION},
Client, ClientBuilder,
};
use std::{sync::Arc, time::Duration};
mod api;
mod download;
@ -55,7 +58,7 @@ impl Bot {
where
S: Into<String>,
{
Self::with_client(token, Client::new())
Self::with_client(token, sound_bot())
}
/// Creates a new `Bot` with the specified token and your
@ -76,6 +79,23 @@ impl Bot {
}
}
// See https://github.com/teloxide/teloxide/issues/223.
fn sound_bot() -> Client {
let mut headers = HeaderMap::new();
headers.insert(CONNECTION, "keep-alive".parse().unwrap());
let connect_timeout = Duration::from_secs(5);
let timeout = 10;
ClientBuilder::new()
.connect_timeout(connect_timeout)
.timeout(Duration::from_secs(connect_timeout.as_secs() + timeout + 2))
.tcp_nodelay_(true)
.default_headers(headers)
.build()
.expect("Cannot build reqwest::Client")
}
impl Bot {
// TODO: const fn
pub fn token(&self) -> &str {

View file

@ -120,11 +120,11 @@ pub trait UpdateListener<E>: Stream<Item = Result<Update, E>> {
}
impl<S, E> UpdateListener<E> for S where S: Stream<Item = Result<Update, E>> {}
/// Returns a long polling update listener with `timeout` of 1 minute.
/// Returns a long polling update listener with `timeout` of 10 seconds.
///
/// See also: [`polling`](polling).
pub fn polling_default(bot: Bot) -> impl UpdateListener<RequestError> {
polling(bot, Some(Duration::from_secs(60)), None, None)
polling(bot, Some(Duration::from_secs(10)), None, None)
}
/// Returns a long/short polling update listener with some additional options.

View file

@ -4,6 +4,9 @@ use serde::{de::DeserializeOwned, Serialize};
use crate::{requests::ResponseResult, RequestError};
use super::{TelegramResponse, TELEGRAM_API_URL};
use std::time::Duration;
const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10);
pub async fn request_multipart<T>(
client: &Client,
@ -48,6 +51,10 @@ async fn process_response<T>(response: Response) -> ResponseResult<T>
where
T: DeserializeOwned,
{
if response.status().is_server_error() {
tokio::time::delay_for(DELAY_ON_SERVER_ERROR).await;
}
serde_json::from_str::<TelegramResponse<T>>(
&response.text().await.map_err(RequestError::NetworkError)?,
)