From 7f3f9f0bf18ba9c48b7b62d4825a2932b6b0c68d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 29 Jul 2021 16:53:42 +0200 Subject: feat: make login --- Cargo.toml | 4 ++++ src/browser.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/commands.rs | 20 ++++++++++++++++++++ src/config.rs | 16 ++++++++++++++++ src/main.rs | 33 +++++++++++++++------------------ 5 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 src/browser.rs create mode 100644 src/commands.rs create mode 100644 src/config.rs diff --git a/Cargo.toml b/Cargo.toml index 06ee770..4f5f5e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,7 @@ teloxide = { version = "0.4", features = ["auto-send", "macros"] } log = "0.4.8" pretty_env_logger = "0.4.0" tokio = { version = "1.3", features = ["rt-multi-thread", "macros"] } +thirtyfour = "0.26.0" + +config = "0.10.1" +serde = { version = "1.0.104", features = ["derive"] } diff --git a/src/browser.rs b/src/browser.rs new file mode 100644 index 0000000..4c06aeb --- /dev/null +++ b/src/browser.rs @@ -0,0 +1,41 @@ +use crate::config::Config; +use std::{thread, time}; +use thirtyfour::prelude::{By, WebDriverResult}; +use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands}; + +pub async fn init() -> WebDriver { + let driver = match WebDriver::new("http://localhost:4444", FirefoxCapabilities::new()).await { + Ok(driver) => driver, + Err(e) => { + panic!(e); + } + }; + + return driver; +} + +pub async fn login(driver: &WebDriver, credentials: &Config) -> WebDriverResult<()> { + driver + .get("https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx") + .await?; + + let cf_input = driver + .find_element(By::Name("ctl01$contents$UserName")) + .await?; + cf_input.send_keys(&credentials.cf).await?; + + let psw_input = driver + .find_element(By::Name("ctl01$contents$UserPassword")) + .await?; + psw_input.send_keys(&credentials.password).await?; + + thread::sleep(time::Duration::from_millis(1000)); + + driver + .find_element(By::Name("ctl01$contents$LogonButton")) + .await? + .click() + .await?; + + Ok(()) +} diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..11c59a9 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,20 @@ +use std::error::Error; +use teloxide::prelude::{AutoSend, Bot, Message, UpdateWithCx}; +use teloxide::utils::command::BotCommand; + +#[derive(BotCommand)] +#[command(rename = "lowercase", description = "These commands are supported:")] +pub enum Command { + #[command(description = "display this text")] + Help, +} + +pub async fn handler( + cx: UpdateWithCx, Message>, + command: Command, +) -> Result<(), Box> { + match command { + Command::Help => cx.answer(Command::descriptions()).await?, + }; + Ok(()) +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..83ed7bd --- /dev/null +++ b/src/config.rs @@ -0,0 +1,16 @@ +pub use config::ConfigError; +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct Config { + pub cf: String, + pub password: String, +} + +impl Config { + pub fn from_env() -> Result { + let mut cfg = config::Config::new(); + cfg.merge(config::Environment::new())?; + cfg.try_into() + } +} diff --git a/src/main.rs b/src/main.rs index abe7f2e..0ac5a4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,12 @@ use std::error::Error; use teloxide::prelude::*; -use teloxide::utils::command::BotCommand; +use thirtyfour::WebDriver; -#[derive(BotCommand)] -#[command(rename = "lowercase", description = "These commands are supported:")] -enum Command { - #[command(description = "display this text")] - Help, -} - -async fn handler( - cx: UpdateWithCx, Message>, - command: Command, -) -> Result<(), Box> { - match command { - Command::Help => cx.answer(Command::descriptions()).await?, - }; +mod browser; +mod commands; +mod config; - Ok(()) -} +use crate::config::Config; #[tokio::main] async fn main() -> Result<(), Box> { @@ -26,8 +14,17 @@ async fn main() -> Result<(), Box> { log::info!("Starting bot..."); let bot = Bot::from_env().auto_send(); + let config = Config::from_env().unwrap(); - teloxide::commands_repl(bot, "unict-reservation", handler).await; + let driver: WebDriver = browser::init().await; + match browser::login(&driver, &config).await { + Ok(_) => {} + Err(e) => { + panic!("You can't connect: `{}`, credentials are {:?}", e, config); + } + }; + teloxide::commands_repl(bot, "unict-reservation", commands::handler).await; + log::info!("Closing bot... Goodbye!"); Ok(()) } -- cgit v1.2.3-18-g5258