141 lines
4.4 KiB
Rust
141 lines
4.4 KiB
Rust
mod config;
|
|
mod slack;
|
|
mod weather;
|
|
|
|
use chrono::Timelike;
|
|
use config::ConfigUser;
|
|
use dotenv::dotenv;
|
|
use serde::Serialize;
|
|
use slack::SlackUser;
|
|
use tokio::prelude::*;
|
|
use weather::WeatherSummary;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
dotenv().ok();
|
|
|
|
let config = config::get_config().unwrap();
|
|
println!("Got config: {:?}", &config);
|
|
|
|
let slack_users = slack::list_users().await.unwrap();
|
|
println!("Got {} users: {:?}", slack_users.len(), slack_users);
|
|
|
|
for slack_user in slack_users {
|
|
let config_user = config
|
|
.config_users
|
|
.iter()
|
|
.find(|config_user| config_user.name == slack_user.name);
|
|
|
|
if let Some(config_user) = config_user {
|
|
match set_user_status(&config_user, &slack_user).await {
|
|
Ok(_) => (),
|
|
Err(e) => println!(
|
|
"Error calling set_user_status for user {}: {}",
|
|
config_user.name, e
|
|
),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn set_user_status(config_user: &ConfigUser, slack_user: &SlackUser) -> Result<(), String> {
|
|
let offset = chrono::offset::FixedOffset::east(slack_user.tz_offset);
|
|
let now = chrono::offset::Utc::now().with_timezone(&offset);
|
|
let is_pm = now.hour() < 6 || now.hour() >= 18;
|
|
let sum = weather::get_summary(&config_user.location).await?;
|
|
|
|
match &sum {
|
|
WeatherSummary::Thunderstorm(_, _)
|
|
| WeatherSummary::Clouds(_, _)
|
|
| WeatherSummary::Snow(_, _)
|
|
| WeatherSummary::Rain(_, _)
|
|
| WeatherSummary::Drizzle(_, _)
|
|
| WeatherSummary::Clear(_, _)
|
|
| WeatherSummary::Atmospheric(_, _) => {
|
|
let status = status_text(&sum, is_pm);
|
|
let emoji = format!(":{}:", &emoji(&sum, is_pm));
|
|
|
|
println!(
|
|
"Set status for user {} to: {} {:?}",
|
|
&slack_user.id, &emoji, &status
|
|
);
|
|
let res = slack::set_status(&slack_user.id, &status, &emoji, 0).await?;
|
|
Ok(res)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn status_text(sum: &WeatherSummary, is_pm: bool) -> String {
|
|
match sum {
|
|
WeatherSummary::Thunderstorm(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Thunderstorm, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Thunderstorm, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
WeatherSummary::Clouds(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Cloudy, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Cloudy, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
WeatherSummary::Snow(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Snow, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Snow, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
WeatherSummary::Rain(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Rain, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Rain, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
WeatherSummary::Drizzle(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Drizzle, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Drizzle, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
WeatherSummary::Clear(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Clear, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Sunny, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
WeatherSummary::Atmospheric(temp_min, temp_max) => {
|
|
if is_pm {
|
|
format!("Foggy, min {}\u{00b0}C", temp_min)
|
|
} else {
|
|
format!("Foggy, max {}\u{00b0}C", temp_max)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn emoji(sum: &WeatherSummary, is_pm: bool) -> String {
|
|
let emoji = match sum {
|
|
WeatherSummary::Thunderstorm(_, _) => "thunder_cloud_and_rain",
|
|
WeatherSummary::Clouds(_, _) => "cloud",
|
|
WeatherSummary::Snow(_, _) => "snow_cloud",
|
|
WeatherSummary::Rain(_, _) => "rain_cloud",
|
|
WeatherSummary::Drizzle(_, _) => "rain_cloud",
|
|
WeatherSummary::Clear(_, _) => {
|
|
if is_pm {
|
|
"crescent_moon"
|
|
} else {
|
|
"sunny"
|
|
}
|
|
}
|
|
WeatherSummary::Atmospheric(_, _) => "foggy",
|
|
};
|
|
|
|
emoji.to_string()
|
|
}
|