From 5020cafcd2402f49916bd6e224515c34dc2e164c Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Mon, 2 Sep 2019 02:00:59 +0600 Subject: [PATCH 001/901] Separate types and functions --- src/core/functions.rs | 35 +++++++++++++++++++++++++++++++ src/core/mod.rs | 48 ++++--------------------------------------- src/core/types.rs | 11 ++++++++++ 3 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 src/core/functions.rs create mode 100644 src/core/types.rs diff --git a/src/core/functions.rs b/src/core/functions.rs new file mode 100644 index 00000000..0d39e59a --- /dev/null +++ b/src/core/functions.rs @@ -0,0 +1,35 @@ +use crate::core::types::User; +use crate::core::{Error, Response, TELEGRAM_URL_START, REQWEST_CLIENT}; +use serde_json::Value; +use futures::compat::Future01CompatExt; + +pub async fn get_me(bot_token: &str) -> Response { + let mut response = REQWEST_CLIENT + .get(&format!( + "{}{bot_token}/getMe", + TELEGRAM_URL_START, + bot_token = bot_token + )) + .send() + .compat() + .await + .map_err(Error::Send)?; + + let response_json = response + .json::() + .compat() + .await + .map_err(Error::InvalidJson)?; + + if response_json["ok"] == "false" { + return Err(Error::Api { + status_code: response.status(), + description: match response_json.get("description") { + None => None, + Some(description) => Some(description.to_string()), + }, + }); + } + + Ok(serde_json::from_value(response_json["result"].clone()).unwrap()) +} diff --git a/src/core/mod.rs b/src/core/mod.rs index 32bd3ec3..ab9b2a4e 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,8 +1,9 @@ -use futures::compat::Future01CompatExt; use reqwest::r#async::Client; + use reqwest::StatusCode; -use serde::Deserialize; -use serde_json::Value; + +mod functions; +mod types; lazy_static! { static ref REQWEST_CLIENT: Client = Client::new(); @@ -21,44 +22,3 @@ pub enum Error { } pub type Response = Result; - -#[derive(Debug, Deserialize)] -pub struct User { - id: i64, - is_bot: bool, - first_name: String, - last_name: Option, - username: Option, - language_code: Option, -} - -pub async fn get_me(bot_token: &str) -> Response { - let mut response = REQWEST_CLIENT - .get(&format!( - "{}{bot_token}/getMe", - TELEGRAM_URL_START, - bot_token = bot_token - )) - .send() - .compat() - .await - .map_err(Error::Send)?; - - let response_json = response - .json::() - .compat() - .await - .map_err(Error::InvalidJson)?; - - if response_json["ok"] == "false" { - return Err(Error::Api { - status_code: response.status(), - description: match response_json.get("description") { - None => None, - Some(description) => Some(description.to_string()), - }, - }); - } - - Ok(serde_json::from_value(response_json["result"].clone()).unwrap()) -} diff --git a/src/core/types.rs b/src/core/types.rs new file mode 100644 index 00000000..bd9b6a68 --- /dev/null +++ b/src/core/types.rs @@ -0,0 +1,11 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct User { + id: i64, + is_bot: bool, + first_name: String, + last_name: Option, + username: Option, + language_code: Option, +} From 2567ae18258fb384e3645f0202a5265ccfe0fcff Mon Sep 17 00:00:00 2001 From: fedechkin_alexey Date: Mon, 2 Sep 2019 11:02:50 +0700 Subject: [PATCH 002/901] add Chat struct --- src/core/types.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/core/types.rs b/src/core/types.rs index bd9b6a68..3e085379 100644 --- a/src/core/types.rs +++ b/src/core/types.rs @@ -9,3 +9,20 @@ pub struct User { username: Option, language_code: Option, } + +#[derive(Debug, Deserialize)] +pub struct Chat { + id: i64, + chat_type: String, + title: Option, + username:Option, + first_name: Option, + last_name: Option, + photo: Option, + description: Option, + invite_link: Option, + pinned_message: Option, + permissions: Option, + sticker_set_name: Option, + can_set_sticker_set: Option, +} \ No newline at end of file From de8dd3d84e7a5da5824c9afd1235d1001ceeff82 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Mon, 2 Sep 2019 10:13:41 +0600 Subject: [PATCH 003/901] Add ChatPhoto (core/types.rs) --- src/core/types.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/types.rs b/src/core/types.rs index 3e085379..5e7eed85 100644 --- a/src/core/types.rs +++ b/src/core/types.rs @@ -25,4 +25,10 @@ pub struct Chat { permissions: Option, sticker_set_name: Option, can_set_sticker_set: Option, +} + +#[derive(Debug, Deserialize)] +pub struct ChatPhoto { + small_file_id: String, + big_file_id: String, } \ No newline at end of file From a3188e34de0366d383d6b795d54bdc02fcaac8bc Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Mon, 2 Sep 2019 10:17:21 +0600 Subject: [PATCH 004/901] Add ChatPermissions (core/types.rs) --- src/core/types.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/types.rs b/src/core/types.rs index 5e7eed85..b55dd871 100644 --- a/src/core/types.rs +++ b/src/core/types.rs @@ -24,11 +24,23 @@ pub struct Chat { pinned_message: Option, permissions: Option, sticker_set_name: Option, - can_set_sticker_set: Option, + can_set_sticker_set: Option, } #[derive(Debug, Deserialize)] pub struct ChatPhoto { small_file_id: String, big_file_id: String, +} + +#[derive(Debug, Deserialize)] +pub struct ChatPermissions { + can_send_messages: Option, + can_send_media_messages: Option, + can_send_polls: Option, + can_send_other_messages: Option, + can_add_web_page_previews: Option, + can_change_info: Option, + can_invite_users: Option, + can_pin_messages: Option, } \ No newline at end of file From 8ba3362eba6fe261e255b8292dcf4d7f7a125b11 Mon Sep 17 00:00:00 2001 From: fedechkin_alexey Date: Mon, 2 Sep 2019 11:21:55 +0700 Subject: [PATCH 005/901] add Message struct --- src/core/types.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/core/types.rs b/src/core/types.rs index 3e085379..6b42bc3c 100644 --- a/src/core/types.rs +++ b/src/core/types.rs @@ -25,4 +25,55 @@ pub struct Chat { permissions: Option, sticker_set_name: Option, can_set_sticker_set: Option, +} + +#[derive(Debug, Deserialize)] +pub struct Message { + message_id: i64, + from: Option, + date: i64, + chat: Chat, + forward_from: Option, + forward_from_chat: Option, + forward_from_message_id: Option, + forward_signature: Option, + forward_sender_name: Option, + forward_date: Option, + reply_to_message: Option, + edit_date: Option, + media_group_id: Option, + author_signature: Option, + text: Option, + entities: Option>, + caption_entities: Option>, + audio: Option