mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-22 06:45:37 +01:00
commit
4a5dc7bcef
4 changed files with 33 additions and 6 deletions
|
@ -29,7 +29,7 @@ cbor-serializer = ["serde_cbor"]
|
||||||
bincode-serializer = ["bincode"]
|
bincode-serializer = ["bincode"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde_json = "1.0.56"
|
serde_json = "1.0.55"
|
||||||
serde = { version = "1.0.114", features = ["derive"] }
|
serde = { version = "1.0.114", features = ["derive"] }
|
||||||
|
|
||||||
tokio = { version = "0.2.21", features = ["full"] }
|
tokio = { version = "0.2.21", features = ["full"] }
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
use crate::types::ParseMode;
|
use crate::types::ParseMode;
|
||||||
use reqwest::Client;
|
use reqwest::{
|
||||||
use std::sync::Arc;
|
header::{HeaderMap, CONNECTION},
|
||||||
|
Client, ClientBuilder,
|
||||||
|
};
|
||||||
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod download;
|
mod download;
|
||||||
|
@ -55,7 +58,7 @@ impl Bot {
|
||||||
where
|
where
|
||||||
S: Into<String>,
|
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
|
/// 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 {
|
impl Bot {
|
||||||
// TODO: const fn
|
// TODO: const fn
|
||||||
pub fn token(&self) -> &str {
|
pub fn token(&self) -> &str {
|
||||||
|
|
|
@ -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>> {}
|
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).
|
/// See also: [`polling`](polling).
|
||||||
pub fn polling_default(bot: Bot) -> impl UpdateListener<RequestError> {
|
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.
|
/// Returns a long/short polling update listener with some additional options.
|
||||||
|
|
|
@ -4,6 +4,9 @@ use serde::{de::DeserializeOwned, Serialize};
|
||||||
use crate::{requests::ResponseResult, RequestError};
|
use crate::{requests::ResponseResult, RequestError};
|
||||||
|
|
||||||
use super::{TelegramResponse, TELEGRAM_API_URL};
|
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>(
|
pub async fn request_multipart<T>(
|
||||||
client: &Client,
|
client: &Client,
|
||||||
|
@ -48,6 +51,10 @@ async fn process_response<T>(response: Response) -> ResponseResult<T>
|
||||||
where
|
where
|
||||||
T: DeserializeOwned,
|
T: DeserializeOwned,
|
||||||
{
|
{
|
||||||
|
if response.status().is_server_error() {
|
||||||
|
tokio::time::delay_for(DELAY_ON_SERVER_ERROR).await;
|
||||||
|
}
|
||||||
|
|
||||||
serde_json::from_str::<TelegramResponse<T>>(
|
serde_json::from_str::<TelegramResponse<T>>(
|
||||||
&response.text().await.map_err(RequestError::NetworkError)?,
|
&response.text().await.map_err(RequestError::NetworkError)?,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue