summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-07-29 16:53:42 +0200
committerSanto Cariotti <santo@dcariotti.me>2021-07-29 16:53:42 +0200
commit7f3f9f0bf18ba9c48b7b62d4825a2932b6b0c68d (patch)
treeaaa1a67935e8a129db43007716a5b92d04326682 /src
parentb5256b0f78a91101f19c6ffdf462e06d05d5c2ad (diff)
feat: make login
Diffstat (limited to 'src')
-rw-r--r--src/browser.rs41
-rw-r--r--src/commands.rs20
-rw-r--r--src/config.rs16
-rw-r--r--src/main.rs33
4 files changed, 92 insertions, 18 deletions
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<AutoSend<Bot>, Message>,
+ command: Command,
+) -> Result<(), Box<dyn Error + Send + Sync>> {
+ 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<Self, ConfigError> {
+ 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<AutoSend<Bot>, Message>,
- command: Command,
-) -> Result<(), Box<dyn Error + Send + Sync>> {
- 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<dyn Error>> {
@@ -26,8 +14,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
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(())
}