Send a reminder when the event starts
This commit is contained in:
41
src/main.rs
41
src/main.rs
@@ -9,7 +9,7 @@ use std::{env, fs::File, io::BufReader, sync::Arc, time::Duration};
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_mutex::Mutex;
|
use async_mutex::Mutex;
|
||||||
use bot::fetch_and_announce_appointment;
|
use bot::fetch_and_announce_appointment;
|
||||||
use chrono::{NaiveTime, Utc};
|
use chrono::{DateTime, NaiveTime, TimeZone, Utc};
|
||||||
use chrono_tz::Europe;
|
use chrono_tz::Europe;
|
||||||
use db::ChatInfo;
|
use db::ChatInfo;
|
||||||
use diesel::{Connection, RunQueryDsl, SqliteConnection};
|
use diesel::{Connection, RunQueryDsl, SqliteConnection};
|
||||||
@@ -93,6 +93,11 @@ async fn main() {
|
|||||||
bot::spawn(bot, db).await;
|
bot::spawn(bot, db).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Reminder<Tz: TimeZone> {
|
||||||
|
time: DateTime<Tz>,
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database) -> Result<()> {
|
async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database) -> Result<()> {
|
||||||
let chats = db.lock().await.transaction(|db| {
|
let chats = db.lock().await.transaction(|db| {
|
||||||
use schema::chat::dsl::*;
|
use schema::chat::dsl::*;
|
||||||
@@ -112,32 +117,37 @@ async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database
|
|||||||
};
|
};
|
||||||
let appointment = appointment.with_timezone(&Europe::Berlin);
|
let appointment = appointment.with_timezone(&Europe::Berlin);
|
||||||
|
|
||||||
if appointment.start < now {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if appointment.start.date_naive() != today {
|
if appointment.start.date_naive() != today {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut reminder = None;
|
||||||
|
if now >= appointment.start {
|
||||||
|
reminder = Some(Reminder {
|
||||||
|
time: appointment.start,
|
||||||
|
text: "Jetzt geht's weiter".into(),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
let reminder_date_time = now.date().and_time(reminder_time).unwrap();
|
let reminder_date_time = now.date().and_time(reminder_time).unwrap();
|
||||||
|
if now >= reminder_date_time {
|
||||||
if now < reminder_date_time {
|
reminder = Some(Reminder {
|
||||||
continue;
|
time: reminder_date_time,
|
||||||
|
text: format!(
|
||||||
|
"Heute um {} Uhr geht's weiter",
|
||||||
|
appointment.start.format("%H:%M")
|
||||||
|
),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(reminder) = reminder {
|
||||||
if chat_info.last_reminder.is_some()
|
if chat_info.last_reminder.is_some()
|
||||||
&& chat_info.last_reminder.unwrap() >= reminder_date_time
|
&& chat_info.last_reminder.unwrap() >= reminder.time
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.send_message(
|
bot.send_message(ChatId(chat_info.id), reminder.text)
|
||||||
ChatId(chat_info.id),
|
|
||||||
format!(
|
|
||||||
"Heute um {} Uhr geht's weiter",
|
|
||||||
appointment.start.format("%H:%M")
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
db.lock().await.transaction(|db| {
|
db.lock().await.transaction(|db| {
|
||||||
@@ -147,6 +157,7 @@ async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database
|
|||||||
.execute(db)
|
.execute(db)
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user