Initial commit

This commit is contained in:
2022-10-19 10:23:55 +02:00
commit e144a0aecc
13 changed files with 2544 additions and 0 deletions

152
src/main.rs Normal file
View File

@@ -0,0 +1,152 @@
mod appointment;
mod bot;
mod db;
mod error;
mod schema;
use std::{env, fs::File, io::BufReader, sync::Arc, time::Duration};
use anyhow::Result;
use async_mutex::Mutex;
use bot::fetch_and_announce_appointment;
use chrono::{NaiveTime, Utc};
use chrono_tz::Europe;
use db::ChatInfo;
use diesel::{Connection, RunQueryDsl, SqliteConnection};
use diesel::{ExpressionMethods, QueryDsl};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use error::ConfigLoadError;
use log::*;
use serde::{de::Error, Deserialize, Deserializer};
use teloxide::adaptors::Throttle;
use teloxide::prelude::RequesterExt;
use teloxide::requests::Requester;
use teloxide::types::ChatId;
use teloxide::{adaptors::throttle::Limits, Bot};
use tokio::time::interval;
use crate::db::DbChat;
#[derive(Deserialize, Debug)]
pub struct Config {
token: String,
data_path: String,
poll_interval: u64,
#[serde(deserialize_with = "deserialize_time")]
reminder_time: NaiveTime,
}
fn deserialize_time<'de, D: Deserializer<'de>>(deserializer: D) -> Result<NaiveTime, D::Error> {
let s: String = Deserialize::deserialize(deserializer)?;
NaiveTime::parse_from_str(&s, "%H:%M").map_err(D::Error::custom)
}
impl Config {
fn load() -> Result<Self, ConfigLoadError> {
let env_var_name = "CALENDAR_BOT_CONFIG_FILE";
let default_filename = "./config.yaml";
let path = env::var(env_var_name).unwrap_or_else(|_| {
warn!(
"Cannot read env var '{}', assuming '{}'",
env_var_name, default_filename
);
default_filename.to_owned()
});
info!("Reading configuration from {}", path);
let file = File::open(path).map_err(ConfigLoadError::OpenFailed)?;
let reader = BufReader::new(file);
serde_yaml::from_reader(reader).map_err(ConfigLoadError::ReadError)
}
}
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
pub type Database = Arc<Mutex<SqliteConnection>>;
#[tokio::main(flavor = "current_thread")]
async fn main() {
pretty_env_logger::init();
let config = Config::load().unwrap();
info!("Connecting to database {}", config.data_path);
let mut db = SqliteConnection::establish(&config.data_path).unwrap();
db.run_pending_migrations(MIGRATIONS).unwrap();
let db = Arc::new(Mutex::new(db));
let bot = Bot::new(config.token).throttle(Limits::default());
{
let db = db.clone();
let bot = bot.clone();
tokio::task::spawn(async move {
let mut interval = interval(Duration::from_secs(config.poll_interval));
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
interval.tick().await;
// TODO Log the error and continue instead
check_task(&bot, config.reminder_time, &db).await.unwrap();
}
});
}
tokio::time::sleep(Duration::from_secs(10)).await;
bot::spawn(bot, db).await;
}
async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database) -> Result<()> {
let chats = db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
chat.load::<DbChat>(db)
})?;
let now = Utc::now().with_timezone(&Europe::Berlin);
let today = now.date_naive();
for chat in chats {
let mut chat_info = ChatInfo::from(chat);
fetch_and_announce_appointment(bot, &mut chat_info, db).await?;
let appointment = match chat_info.next_appointment {
Some(appointment) => appointment,
None => continue,
};
let appointment = appointment.with_timezone(&Europe::Berlin);
if appointment.start < now {
continue;
}
if appointment.start.date_naive() != today {
continue;
}
let reminder_date_time = now.date().and_time(reminder_time).unwrap();
if now < reminder_date_time {
continue;
}
if chat_info.last_reminder.is_some()
&& chat_info.last_reminder.unwrap() >= reminder_date_time
{
continue;
}
bot.send_message(
ChatId(chat_info.id),
format!(
"Heute um {} Uhr geht's wetier",
appointment.start.format("%H:%M")
),
)
.await?;
db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
diesel::update(chat.filter(telegram_id.eq(chat_info.id)))
.set((last_reminder.eq(now.timestamp()),))
.execute(db)
})?;
}
Ok(())
}