diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-08-05 19:00:37 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-08-05 19:00:37 +0200 |
commit | 429489349688ce452f124a691c7542d2ca2177d1 (patch) | |
tree | e2f124dc11a1bf92f13e172f267ae9ae42e07f53 /src | |
parent | 51ea2896cdcdf7b6027f96421a0ce2b454486e03 (diff) |
feat: handle the callback of the faculty
Diffstat (limited to 'src')
-rw-r--r-- | src/browser/mod.rs | 16 | ||||
-rw-r--r-- | src/browser/web_browser.rs | 21 | ||||
-rw-r--r-- | src/callbacks.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 5 |
4 files changed, 58 insertions, 2 deletions
diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 7205f27..1675595 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -34,3 +34,19 @@ pub async unsafe fn get_faculties() -> WebDriverResult<Option<HashMap<String, St Ok(None) } + +pub async unsafe fn select_option( + klass: &str, + property_name: &str, + property_value: &str, +) -> WebDriverResult<bool> { + if let Some(driver) = &WEB_BROWSER { + let result = driver + .select_option_from_list(klass, property_name, property_value) + .await?; + + return Ok(result); + } + + Ok(false) +} diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs index c7497cf..ffbdc13 100644 --- a/src/browser/web_browser.rs +++ b/src/browser/web_browser.rs @@ -114,6 +114,27 @@ impl Browser { Ok(None) } + + /// Select an option from a list of select elements + pub async fn select_option_from_list( + &self, + klass: &str, + property_name: &str, + property_value: &str, + ) -> WebDriverResult<bool> { + if let Some(_d) = &self.driver { + _d.find_element(By::Css( + &format!("li.{}[{}='{}']", klass, property_name, property_value).to_owned()[..], + )) + .await? + .click() + .await?; + + return Ok(true); + } + + Ok(false) + } } /// The static unsafe variable used to open a web browser diff --git a/src/callbacks.rs b/src/callbacks.rs new file mode 100644 index 0000000..c670884 --- /dev/null +++ b/src/callbacks.rs @@ -0,0 +1,18 @@ +use crate::browser; + +/// Handle the string of callback data +pub async fn handler(text: &String) { + let call: Vec<&str> = text.split("_").collect(); + + // First part of `_` string is the type of callback we want to handle + let type_ = call[0]; + + match type_ { + "faculty" => unsafe { + // Select the faculty + let _ = + browser::select_option("select2-results__option", "data-select2-id", call[1]).await; + }, + _ => {} + }; +} diff --git a/src/main.rs b/src/main.rs index c038a9b..79b6e6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::error::Error; use teloxide::prelude::*; mod browser; +mod callbacks; mod commands; mod config; @@ -41,8 +42,8 @@ async fn main() -> Result<(), Box<dyn Error>> { .callback_queries_handler(|rx: DispatcherHandlerRx<AutoSend<Bot>, CallbackQuery>| { UnboundedReceiverStream::new(rx).for_each_concurrent(None, |cx| async move { let data = &cx.update.data; - if let Some(d) = data { - println!("{}", d); + if let Some(text) = data { + callbacks::handler(text).await; } }) }) |