Skip to content

Rust implementation 🤩 #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rust/dependencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
smack_my_bitch_up + hangover
-rand = "0.8.5"
-dotenv = "0.15.0"
-openapi = { path = "./twilio-rust" }
-tokio = { version = "1.21.1", features = ["rt", "rt-multi-thread", "macros"] }

fucking_coffee
-pure rust

37 changes: 37 additions & 0 deletions rust/fucking_coffee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{
io::{Read, Write},
net::TcpStream,
process::Command,
thread,
time::Duration,
};

const COFFEE_MACHINE_IP: &str = "10.10.42.42:23";
const COFFEE_MACHINE_PASSWORD: &str = "1234";

fn main() -> std::io::Result<()> {
//Exit early if no sessions with my username are found
let users = Command::new("who").output().unwrap();
let is_active = String::from_utf8_lossy(&users.stdout)
.split('\n')
.any(|line| line.starts_with("username"));
if !is_active {
std::process::exit(0);
}

//Brew fucking coffee
let delay_before_brew = Duration::from_secs(17);
let delay = Duration::from_secs(24);

thread::sleep(delay_before_brew);
let mut conn = TcpStream::connect(COFFEE_MACHINE_IP)?;

conn.write_all(COFFEE_MACHINE_PASSWORD.as_bytes())?;
conn.read_exact(&mut [0u8; 1024])?;
conn.write_all(b"sys brew")?;

thread::sleep(delay);
conn.write_all(b"sys pour")?;

Ok(())
}
55 changes: 55 additions & 0 deletions rust/hangover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{env, process::Command};
use dotenv::dotenv;
use rand::seq::SliceRandom;
use openapi::apis::{
configuration::Configuration,
default_api::{self as twilio_api, CreateMessageParams},
};

#[tokio::main]
async fn main() {
dotenv().expect("Error reading .env file");
let my_number = env::var("MY_NUMBER").expect("oof");
let boss_number = env::var("BOSSES_NUMBER").unwrap();
let api_key_sid = env::var("API_KEY_SID").unwrap();
let api_key_secret = env::var("API_KEY_SECRET").unwrap();
let account_sid = env::var("ACCOUNT_SID").unwrap();

//Exit early if no sessions with my username are found
let users = Command::new("who").output().unwrap();
let is_active = String::from_utf8_lossy(&users.stdout)
.split('\n')
.any(|line| line.starts_with("username"));
if !is_active {
std::process::exit(0);
}

//Create message
let reasons: Vec<&str> = vec!["Locked out",
"Pipes broke",
"Food poisoning",
"Not feeling well"];
let random_reason = reasons.choose(&mut rand::thread_rng()).unwrap();
let message = format!("Gonna work from home. {}", random_reason);


//Send a text message
let twilio_config = Configuration {
basic_auth: Some((api_key_sid, Some(api_key_secret))),
..Default::default()
};

let msg_params = CreateMessageParams {
account_sid,
to: my_number,
from: Some(boss_number),
body: Some(msg.into()),
..Default::default()
};

let send_msg = twilio_api::create_message(&twilio_config, msg_params).await;
match send_msg {
Ok(result) => result,
Err(error) => panic!("Error sending message: {}", error),
};
}
53 changes: 53 additions & 0 deletions rust/smack_my_bitch_up.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{env, process::Command};
use dotenv::dotenv;
use rand::seq::SliceRandom;
use openapi::apis::{
configuration::Configuration,
default_api::{self as twilio_api, CreateMessageParams},
};

#[tokio::main]
async fn main() {
dotenv().expect("Error reading .env file");
let my_number = env::var("MY_NUMBER").expect("oof");
let her_number = env::var("HER_NUMBER").unwrap();
let api_key_sid = env::var("API_KEY_SID").unwrap();
let api_key_secret = env::var("API_KEY_SECRET").unwrap();
let account_sid = env::var("ACCOUNT_SID").unwrap();

//Exit early if no sessions with my username are found
let users = Command::new("who").output().unwrap();
let is_active = String::from_utf8_lossy(&users.stdout)
.split('\n')
.any(|line| line.starts_with("username"));
if !is_active {
std::process::exit(0);
}

//Create message
let reasons: Vec<&str> = vec!["Working hard",
"Gotta ship this feature",
"Someone fucked the system again"];
let random_reason: &str = reasons.choose(&mut rand::thread_rng()).unwrap();
let msg = format!("Late at work. {}", random_reason);

//Send a text message
let twilio_config = Configuration {
basic_auth: Some((api_key_sid, Some(api_key_secret))),
..Default::default()
};

let msg_params = CreateMessageParams {
account_sid,
to: my_number,
from: Some(her_number),
body: Some(msg.into()),
..Default::default()
};

let send_msg = twilio_api::create_message(&twilio_config, msg_params).await;
match send_msg {
Ok(result) => result,
Err(error) => panic!("Error sending message: {}", error),
};
}