Telegram: factory alert switch and night mode
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
738b39584d
commit
9913a202ec
7 files changed
+169
-7
No files matched your search
@@ -51,6 +51,48 @@ pub struct Settings {
|
||||
/// Reasoning effort for the next messages (/effort).
|
||||
#[serde(default)]
|
||||
pub effort: Option<String>,
|
||||
/// No factory alerts (question / failed) on Telegram. Inverted so that the default is "on".
|
||||
#[serde(default)]
|
||||
pub factory_alerts_off: bool,
|
||||
/// Night mode: no unsolicited messages between `quiet_from` and `quiet_to` (server time).
|
||||
#[serde(default)]
|
||||
pub quiet: bool,
|
||||
/// "HH:MM"; empty = 22:00.
|
||||
#[serde(default)]
|
||||
pub quiet_from: String,
|
||||
/// "HH:MM"; empty = 06:00.
|
||||
#[serde(default)]
|
||||
pub quiet_to: String,
|
||||
}
|
||||
|
||||
/// Minutes since midnight of an "HH:MM" string.
|
||||
pub fn parse_hhmm(s: &str) -> Option<u32> {
|
||||
let (h, m) = s.trim().split_once(':')?;
|
||||
let (h, m): (u32, u32) = (h.parse().ok()?, m.parse().ok()?);
|
||||
(h < 24 && m < 60).then_some(h * 60 + m)
|
||||
}
|
||||
|
||||
/// Is `now` (minutes since midnight) inside the quiet window? The window may span midnight.
|
||||
pub fn in_quiet_window(from: u32, to: u32, now: u32) -> bool {
|
||||
match from.cmp(&to) {
|
||||
std::cmp::Ordering::Equal => false,
|
||||
std::cmp::Ordering::Less => now >= from && now < to,
|
||||
std::cmp::Ordering::Greater => now >= from || now < to,
|
||||
}
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// Night mode is on and it is night now.
|
||||
pub fn quiet_now(&self) -> bool {
|
||||
use chrono::Timelike;
|
||||
if !self.quiet {
|
||||
return false;
|
||||
}
|
||||
let from = parse_hhmm(if self.quiet_from.is_empty() { "22:00" } else { &self.quiet_from }).unwrap_or(22 * 60);
|
||||
let to = parse_hhmm(if self.quiet_to.is_empty() { "06:00" } else { &self.quiet_to }).unwrap_or(6 * 60);
|
||||
let t = chrono::Local::now();
|
||||
in_quiet_window(from, to, t.hour() * 60 + t.minute())
|
||||
}
|
||||
}
|
||||
|
||||
fn api_base() -> String {
|
||||
@@ -264,6 +306,9 @@ fn user(app: &App, user_id: &str) -> Result<User> {
|
||||
pub async fn notify_user(app: &App, user_id: &str, text: &str) {
|
||||
let s = load(app, user_id);
|
||||
let (true, Some(chat)) = (!s.token.is_empty(), s.chat_id) else { return };
|
||||
if s.factory_alerts_off || s.quiet_now() {
|
||||
return;
|
||||
}
|
||||
if let Err(e) = (Bot { token: s.token }).send(chat, &esc(text), None).await {
|
||||
tracing::warn!("telegram: notification not sent: {e:#}");
|
||||
}
|
||||
@@ -884,6 +929,10 @@ async fn listen(app: Arc<App>) {
|
||||
let Some(chat) = st.chat_id else { continue };
|
||||
let de = german(&app, &owner);
|
||||
let is_current = st.session.as_deref() == Some(session_id.as_str());
|
||||
// Night mode: only the chat the user is talking to in Telegram still reports.
|
||||
if !is_current && st.quiet_now() {
|
||||
continue;
|
||||
}
|
||||
// Chats with "not to Telegram": their approvals and questions stay in the web UI
|
||||
// (subagents follow their top-level chat; the chat open in Telegram always reports).
|
||||
if !is_current && matches!(ev, Event::PermissionRequest { .. } | Event::QuestionRequest { .. }) {
|
||||
@@ -1213,3 +1262,30 @@ mod tests {
|
||||
assert_eq!(step_line("todo_write", &json!({})), "todo_write");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod quiet_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn quiet_window_spans_midnight() {
|
||||
let (from, to) = (parse_hhmm("22:00").unwrap(), parse_hhmm("06:00").unwrap());
|
||||
assert!(in_quiet_window(from, to, 23 * 60));
|
||||
assert!(in_quiet_window(from, to, 0));
|
||||
assert!(in_quiet_window(from, to, 5 * 60 + 59));
|
||||
assert!(!in_quiet_window(from, to, 6 * 60));
|
||||
assert!(!in_quiet_window(from, to, 12 * 60));
|
||||
assert!(!in_quiet_window(from, to, 21 * 60 + 59));
|
||||
assert!(in_quiet_window(from, to, 22 * 60));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quiet_window_within_a_day_and_degenerate() {
|
||||
assert!(in_quiet_window(13 * 60, 14 * 60, 13 * 60 + 30));
|
||||
assert!(!in_quiet_window(13 * 60, 14 * 60, 15 * 60));
|
||||
assert!(!in_quiet_window(600, 600, 600));
|
||||
assert_eq!(parse_hhmm("7:05"), Some(425));
|
||||
assert_eq!(parse_hhmm("24:00"), None);
|
||||
assert_eq!(parse_hhmm("x"), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user