Allow notifications to be sent days ahead of time
This commit is contained in:
@@ -1,4 +1,10 @@
|
|||||||
messages:
|
messages:
|
||||||
next_appointment: "Nächster Termin: %{weekday}, %{date}"
|
next_appointment: "Nächster Termin: %{weekday}, %{date}"
|
||||||
appointment_today: "Heute um %{start_time} Uhr geht's weiter"
|
appointment_today: "Heute um %{start_time} Uhr geht's weiter"
|
||||||
|
appointment_tomorrow: "Morgen um %{start_time} Uhr geht's weiter"
|
||||||
|
appointment_soon: "In %{no_days} um %{start_time} Uhr geht's weiter"
|
||||||
starting_now: "Jetzt geht's weiter"
|
starting_now: "Jetzt geht's weiter"
|
||||||
|
|
||||||
|
errors:
|
||||||
|
param_no_days: "Bitte gib die Anzahl Tage an, die du vorab erinnert werden möchtest, als Parameter an"
|
||||||
|
param_no_days_nonnegative: "Die Anzahl der Tage darf nicht negativ sein"
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
messages:
|
messages:
|
||||||
next_appointment: "Next meeting: %{weekday}, %{date} (%{uk_time} UK time)"
|
next_appointment: "Next meeting: %{weekday}, %{date} (%{uk_time} UK time)"
|
||||||
appointment_today: "Next meeting is today at %{start_time} (%{uk_time} UK time)"
|
appointment_today: "Next meeting is today at %{start_time} (%{uk_time} UK time)"
|
||||||
|
appointment_tomorrow: "Next meeting is tomorrow at %{start_time} (%{uk_time} UK time)"
|
||||||
|
appointment_soon: "Next meeting is in %{no_days} at %{start_time} (%{uk_time} UK time)"
|
||||||
starting_now: "The meeting starts now"
|
starting_now: "The meeting starts now"
|
||||||
|
|
||||||
|
errors:
|
||||||
|
param_no_days: "Please specify the number of days that you want to be reminded in advance as parameter"
|
||||||
|
param_no_days_nonnegative: "The number of days may not be negative"
|
||||||
1
migrations/2024-01-11-091208_remind_days_ahead/down.sql
Normal file
1
migrations/2024-01-11-091208_remind_days_ahead/down.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE chat DROP remind_days_ahead;
|
||||||
1
migrations/2024-01-11-091208_remind_days_ahead/up.sql
Normal file
1
migrations/2024-01-11-091208_remind_days_ahead/up.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE chat ADD remind_days_ahead BIGINT NOT NULL DEFAULT 0;
|
||||||
62
src/bot.rs
62
src/bot.rs
@@ -11,7 +11,9 @@ use teloxide::dispatching::dialogue;
|
|||||||
use teloxide::dispatching::dialogue::InMemStorage;
|
use teloxide::dispatching::dialogue::InMemStorage;
|
||||||
use teloxide::dispatching::{UpdateFilterExt, UpdateHandler};
|
use teloxide::dispatching::{UpdateFilterExt, UpdateHandler};
|
||||||
use teloxide::dptree::{case, deps};
|
use teloxide::dptree::{case, deps};
|
||||||
|
use teloxide::payloads::SendMessageSetters;
|
||||||
use teloxide::prelude::Dispatcher;
|
use teloxide::prelude::Dispatcher;
|
||||||
|
use teloxide::requests::Request;
|
||||||
use teloxide::requests::Requester;
|
use teloxide::requests::Requester;
|
||||||
use teloxide::types::ChatId;
|
use teloxide::types::ChatId;
|
||||||
use teloxide::types::ChatMemberKind;
|
use teloxide::types::ChatMemberKind;
|
||||||
@@ -32,6 +34,7 @@ pub enum Command {
|
|||||||
#[command()]
|
#[command()]
|
||||||
SetCalendar,
|
SetCalendar,
|
||||||
SetLocale,
|
SetLocale,
|
||||||
|
RemindDaysAhead,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn spawn(bot: Throttle<Bot>, db: Database) {
|
pub async fn spawn(bot: Throttle<Bot>, db: Database) {
|
||||||
@@ -45,7 +48,8 @@ pub async fn spawn(bot: Throttle<Bot>, db: Database) {
|
|||||||
fn build_handler_chain() -> UpdateHandler<Error> {
|
fn build_handler_chain() -> UpdateHandler<Error> {
|
||||||
let command_handler = teloxide::filter_command::<Command, _>()
|
let command_handler = teloxide::filter_command::<Command, _>()
|
||||||
.branch(case![Command::SetCalendar].endpoint(set_calendar))
|
.branch(case![Command::SetCalendar].endpoint(set_calendar))
|
||||||
.branch(case![Command::SetLocale].endpoint(set_locale));
|
.branch(case![Command::SetLocale].endpoint(set_locale))
|
||||||
|
.branch(case![Command::RemindDaysAhead].endpoint(set_remind_days_ahead));
|
||||||
|
|
||||||
let my_chat_member_handler = Update::filter_my_chat_member().endpoint(handle_my_chat_member);
|
let my_chat_member_handler = Update::filter_my_chat_member().endpoint(handle_my_chat_member);
|
||||||
|
|
||||||
@@ -96,6 +100,7 @@ async fn set_calendar(bot: Throttle<Bot>, msg: Message, db: Database) -> Result<
|
|||||||
last_reminder: None,
|
last_reminder: None,
|
||||||
pinned_message_id: None,
|
pinned_message_id: None,
|
||||||
locale: "de".into(),
|
locale: "de".into(),
|
||||||
|
remind_days_ahead: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
fetch_and_announce_appointment(&bot, &mut chat_info, &db).await?;
|
fetch_and_announce_appointment(&bot, &mut chat_info, &db).await?;
|
||||||
@@ -123,6 +128,61 @@ async fn set_locale(_bot: Throttle<Bot>, msg: Message, db: Database) -> Result<(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn set_remind_days_ahead(
|
||||||
|
bot: Throttle<Bot>,
|
||||||
|
msg: Message,
|
||||||
|
db: Database,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let chat = db.lock().await.transaction(|db| {
|
||||||
|
use schema::chat::dsl::*;
|
||||||
|
chat.filter(telegram_id.eq(msg.chat.id.0))
|
||||||
|
.first::<DbChat>(db)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let chat = ChatInfo::<Utc>::from(chat);
|
||||||
|
|
||||||
|
let days_ahead = msg
|
||||||
|
.text()
|
||||||
|
.map(|text| text.splitn(2, " ").nth(1).map(|param| param.parse().ok()))
|
||||||
|
.flatten()
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
if days_ahead.is_none() {
|
||||||
|
bot.send_message(
|
||||||
|
msg.chat.id,
|
||||||
|
t!("errors.param_no_days", locale = &chat.locale),
|
||||||
|
)
|
||||||
|
.reply_to_message_id(msg.id)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let days_ahead: i64 = days_ahead.unwrap();
|
||||||
|
|
||||||
|
if days_ahead < 0 {
|
||||||
|
bot.send_message(
|
||||||
|
msg.chat.id,
|
||||||
|
t!("errors.param_no_days_nonnegative", locale = &chat.locale),
|
||||||
|
)
|
||||||
|
.reply_to_message_id(msg.id)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
db.lock().await.transaction::<_, Error, _>(|db| {
|
||||||
|
use schema::chat::dsl::*;
|
||||||
|
diesel::update(chat)
|
||||||
|
.filter(telegram_id.eq(msg.chat.id.0))
|
||||||
|
.set(remind_days_ahead.eq(days_ahead))
|
||||||
|
.execute(db)?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn fetch_and_announce_appointment(
|
pub async fn fetch_and_announce_appointment(
|
||||||
bot: &Throttle<Bot>,
|
bot: &Throttle<Bot>,
|
||||||
chat_info: &mut ChatInfo<Utc>,
|
chat_info: &mut ChatInfo<Utc>,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pub struct DbChat {
|
|||||||
last_reminder: Option<i64>,
|
last_reminder: Option<i64>,
|
||||||
pinned_message: Option<i32>,
|
pinned_message: Option<i32>,
|
||||||
locale: Option<String>,
|
locale: Option<String>,
|
||||||
|
remind_days_ahead: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ChatInfo<Tz: TimeZone> {
|
pub struct ChatInfo<Tz: TimeZone> {
|
||||||
@@ -22,6 +23,7 @@ pub struct ChatInfo<Tz: TimeZone> {
|
|||||||
pub last_reminder: Option<DateTime<Tz>>,
|
pub last_reminder: Option<DateTime<Tz>>,
|
||||||
pub pinned_message_id: Option<i32>,
|
pub pinned_message_id: Option<i32>,
|
||||||
pub locale: String,
|
pub locale: String,
|
||||||
|
pub remind_days_ahead: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DbChat> for ChatInfo<Utc> {
|
impl From<DbChat> for ChatInfo<Utc> {
|
||||||
@@ -41,6 +43,8 @@ impl From<DbChat> for ChatInfo<Utc> {
|
|||||||
|
|
||||||
let locale = db_chat.locale.unwrap_or("de".into());
|
let locale = db_chat.locale.unwrap_or("de".into());
|
||||||
|
|
||||||
|
let remind_days_ahead = db_chat.remind_days_ahead.try_into().unwrap_or(0);
|
||||||
|
|
||||||
ChatInfo {
|
ChatInfo {
|
||||||
id: db_chat.telegram_id,
|
id: db_chat.telegram_id,
|
||||||
calendar: db_chat.calendar,
|
calendar: db_chat.calendar,
|
||||||
@@ -48,6 +52,7 @@ impl From<DbChat> for ChatInfo<Utc> {
|
|||||||
last_reminder,
|
last_reminder,
|
||||||
pinned_message_id: db_chat.pinned_message,
|
pinned_message_id: db_chat.pinned_message,
|
||||||
locale,
|
locale,
|
||||||
|
remind_days_ahead,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
54
src/main.rs
54
src/main.rs
@@ -10,7 +10,7 @@ use std::{env, fs::File, io::BufReader, sync::Arc};
|
|||||||
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::{DateTime, NaiveTime, TimeZone, Utc};
|
use chrono::{DateTime, Days, NaiveTime, TimeZone, Utc};
|
||||||
use chrono_tz::Europe;
|
use chrono_tz::Europe;
|
||||||
use db::ChatInfo;
|
use db::ChatInfo;
|
||||||
use diesel::result::Error::NotFound;
|
use diesel::result::Error::NotFound;
|
||||||
@@ -41,6 +41,8 @@ pub struct Config {
|
|||||||
poll_interval: u64,
|
poll_interval: u64,
|
||||||
#[serde(deserialize_with = "deserialize_time")]
|
#[serde(deserialize_with = "deserialize_time")]
|
||||||
reminder_time: NaiveTime,
|
reminder_time: NaiveTime,
|
||||||
|
#[serde(deserialize_with = "deserialize_time")]
|
||||||
|
preceeding_day_reminder_time: NaiveTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_time<'de, D: Deserializer<'de>>(deserializer: D) -> Result<NaiveTime, D::Error> {
|
fn deserialize_time<'de, D: Deserializer<'de>>(deserializer: D) -> Result<NaiveTime, D::Error> {
|
||||||
@@ -79,7 +81,7 @@ async fn main() {
|
|||||||
db.run_pending_migrations(MIGRATIONS).unwrap();
|
db.run_pending_migrations(MIGRATIONS).unwrap();
|
||||||
let db = Arc::new(Mutex::new(db));
|
let db = Arc::new(Mutex::new(db));
|
||||||
|
|
||||||
let bot = Bot::new(config.token).throttle(Limits::default());
|
let bot = Bot::new(&config.token).throttle(Limits::default());
|
||||||
|
|
||||||
{
|
{
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
@@ -104,16 +106,18 @@ async fn main() {
|
|||||||
Err(e) => Err(e).unwrap(),
|
Err(e) => Err(e).unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let next_appointment =
|
||||||
|
next_appointment.map(|timestamp| Utc.timestamp_opt(timestamp, 0).unwrap());
|
||||||
|
|
||||||
let sleep_duration = next_appointment
|
let sleep_duration = next_appointment
|
||||||
.map(|timestamp| Utc.timestamp_opt(timestamp, 0).unwrap())
|
.map(|next_appointment| next_appointment - now)
|
||||||
.map(|date_time| date_time - now)
|
|
||||||
.map(|duration| duration.to_std().unwrap())
|
.map(|duration| duration.to_std().unwrap())
|
||||||
.filter(|duration| *duration < poll_duration)
|
.filter(|duration| *duration < poll_duration)
|
||||||
.unwrap_or(poll_duration);
|
.unwrap_or(poll_duration);
|
||||||
|
|
||||||
sleep(sleep_duration).await;
|
sleep(sleep_duration).await;
|
||||||
|
|
||||||
let result = check_task(&bot, config.reminder_time, &db).await;
|
let result = check_task(&bot, &config, &db).await;
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
error!("{}\nBacktrace:\n{}", e, e.backtrace());
|
error!("{}\nBacktrace:\n{}", e, e.backtrace());
|
||||||
}
|
}
|
||||||
@@ -131,14 +135,15 @@ struct Reminder<Tz: TimeZone> {
|
|||||||
text: String,
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database) -> Result<()> {
|
// Checks if the date of the next appointment has changed (and announces if so)
|
||||||
|
// Additionally, checks if it is time for a reminder and sends that reminder if necessary
|
||||||
|
async fn check_task(bot: &Throttle<Bot>, config: &Config, 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::*;
|
||||||
chat.load::<DbChat>(db)
|
chat.load::<DbChat>(db)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let now = Utc::now().with_timezone(&Europe::Berlin);
|
let now = Utc::now().with_timezone(&Europe::Berlin);
|
||||||
let today = now.date_naive();
|
|
||||||
|
|
||||||
for chat in chats {
|
for chat in chats {
|
||||||
let mut chat_info = ChatInfo::from(chat);
|
let mut chat_info = ChatInfo::from(chat);
|
||||||
@@ -150,10 +155,6 @@ 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.date_naive() != today {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut reminder = None;
|
let mut reminder = None;
|
||||||
if now >= appointment.start {
|
if now >= appointment.start {
|
||||||
reminder = Some(Reminder {
|
reminder = Some(Reminder {
|
||||||
@@ -161,24 +162,39 @@ async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database
|
|||||||
text: t!("messages.starting_now", locale = &chat_info.locale),
|
text: t!("messages.starting_now", locale = &chat_info.locale),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let reminder_date_time = now
|
let reminder_day =
|
||||||
.date_naive()
|
appointment.start.date_naive() - Days::new(chat_info.remind_days_ahead);
|
||||||
.and_time(reminder_time)
|
let reminder_date_time = if chat_info.remind_days_ahead == 0 {
|
||||||
|
reminder_day.and_time(config.reminder_time)
|
||||||
|
} else {
|
||||||
|
reminder_day.and_time(config.preceeding_day_reminder_time)
|
||||||
|
};
|
||||||
|
let reminder_date_time = reminder_date_time
|
||||||
.and_local_timezone(now.timezone())
|
.and_local_timezone(now.timezone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if now >= reminder_date_time {
|
if now >= reminder_date_time {
|
||||||
reminder = Some(Reminder {
|
// TODO This can have weird effects if it's happenig around midnight, since it's not timezone aware (and may even mix multiple timezones)
|
||||||
time: reminder_date_time,
|
let remaining_time = appointment.start.date_naive() - now.date_naive();
|
||||||
text: t!(
|
let remaining_days = remaining_time.num_days();
|
||||||
"messages.appointment_today",
|
let message_id = match remaining_days {
|
||||||
|
0 => "messages.appointment_today",
|
||||||
|
1 => "messages.appointment_tomorrow",
|
||||||
|
_ => "messages.appointment_soon",
|
||||||
|
};
|
||||||
|
let reminder_text = t!(
|
||||||
|
message_id,
|
||||||
locale = &chat_info.locale,
|
locale = &chat_info.locale,
|
||||||
|
no_days = remaining_days,
|
||||||
start_time = &appointment.start.format("%H:%M").to_string(),
|
start_time = &appointment.start.format("%H:%M").to_string(),
|
||||||
uk_time = &appointment
|
uk_time = &appointment
|
||||||
.start
|
.start
|
||||||
.with_timezone(&Europe::London)
|
.with_timezone(&Europe::London)
|
||||||
.format("%H:%M")
|
.format("%H:%M")
|
||||||
.to_string()
|
.to_string()
|
||||||
),
|
);
|
||||||
|
reminder = Some(Reminder {
|
||||||
|
time: reminder_date_time,
|
||||||
|
text: reminder_text,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ diesel::table! {
|
|||||||
last_reminder -> Nullable<BigInt>,
|
last_reminder -> Nullable<BigInt>,
|
||||||
pinned_message_id -> Nullable<Integer>,
|
pinned_message_id -> Nullable<Integer>,
|
||||||
locale -> Nullable<Text>,
|
locale -> Nullable<Text>,
|
||||||
|
remind_days_ahead -> BigInt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user