feat(bot): add daily push notification subscription system

Add /suscribir and /desuscribir commands to the Telegram bot so users
can opt in to receive the next-day prediction automatically at 20:00
when the extended task fires. Subscribers are persisted in a new
notification_subscribers SQLite table with soft-delete semantics.
This commit is contained in:
dasemu committed 2026-03-17 17:46:02 +01:00
1 parent 5e2a22340f
commit 61b4d8334d
3 files changed
+200

No files matched your search

+94
View File
@@ -595,6 +595,92 @@ class SQLiteStore:
return df
# ------------------------------------------------------------------
# Notification subscribers
# ------------------------------------------------------------------
def subscribe_notifications(self, chat_id: int, username: str | None) -> bool:
"""Subscribe a Telegram chat to daily notifications.
Returns True if newly subscribed, False if already active.
Reactivates a previously unsubscribed chat.
"""
with self.connect() as conn:
row = conn.execute(
"SELECT active FROM notification_subscribers WHERE telegram_chat_id = ?",
(chat_id,),
).fetchone()
if row is not None:
if row["active"]:
return False
conn.execute(
"""
UPDATE notification_subscribers
SET active = 1, telegram_username = ?, subscribed_at = ?
WHERE telegram_chat_id = ?
""",
(username, self._now_iso(), chat_id),
)
return True
conn.execute(
"""
INSERT INTO notification_subscribers
(telegram_chat_id, telegram_username, subscribed_at, active)
VALUES (?, ?, ?, 1)
""",
(chat_id, username, self._now_iso()),
)
return True
def unsubscribe_notifications(self, chat_id: int) -> bool:
"""Unsubscribe a Telegram chat from daily notifications.
Returns True if the chat was actively subscribed, False otherwise.
"""
with self.connect() as conn:
row = conn.execute(
"SELECT active FROM notification_subscribers WHERE telegram_chat_id = ?",
(chat_id,),
).fetchone()
if row is None or not row["active"]:
return False
conn.execute(
"UPDATE notification_subscribers SET active = 0 WHERE telegram_chat_id = ?",
(chat_id,),
)
return True
def is_subscribed(self, chat_id: int) -> bool:
"""Return True when the chat has an active notification subscription."""
with self.connect() as conn:
row = conn.execute(
"SELECT active FROM notification_subscribers WHERE telegram_chat_id = ?",
(chat_id,),
).fetchone()
return row is not None and bool(row["active"])
def get_active_subscribers(self) -> list[dict]:
"""Return all active notification subscribers."""
with self.connect() as conn:
rows = conn.execute(
"""
SELECT telegram_chat_id, telegram_username
FROM notification_subscribers
WHERE active = 1
""",
).fetchall()
return [
{
"telegram_chat_id": row["telegram_chat_id"],
"telegram_username": row["telegram_username"],
}
for row in rows
]
# ------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------
@@ -687,6 +773,14 @@ class SQLiteStore:
created_at TEXT NOT NULL,
last_login_at TEXT
);
CREATE TABLE IF NOT EXISTS notification_subscribers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_chat_id INTEGER NOT NULL UNIQUE,
telegram_username TEXT,
subscribed_at TEXT NOT NULL,
active INTEGER NOT NULL DEFAULT 1
);
"""
)