Initial signal-cal-bot skeleton: iCalendar feed to Signal group
- feed fetch with ETag/Last-Modified caching - iCalendar parsing/expansion with TEC series detection - posting-window scheduler with 3-day reminders and day-of fallback - image attachments from ATTACH, downscaled via Pillow - SQLite outbox with idempotent deliveries - watchdog alerts to Note to Self - Docker Compose with signal-cli-rest-api
This commit is contained in:
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
PRODID:-//MCR Commons Calendar - ECPv6.18.2//NONSGML v1.0//EN
|
||||
BEGIN:VTIMEZONE
|
||||
TZID:Europe/London
|
||||
BEGIN:STANDARD
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0000
|
||||
TZNAME:GMT
|
||||
DTSTART:20251026T010000
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZOFFSETFROM:+0000
|
||||
TZOFFSETTO:+0100
|
||||
TZNAME:BST
|
||||
DTSTART:20260329T010000
|
||||
END:DAYLIGHT
|
||||
END:VTIMEZONE
|
||||
BEGIN:VEVENT
|
||||
DTSTART;TZID=Europe/London:20261120T090000
|
||||
DTEND;TZID=Europe/London:20261120T100000
|
||||
DTSTAMP:20261116T192444Z
|
||||
UID:1839-1795165200-1795168800@commonscalendar.uk
|
||||
SUMMARY:Clean up Greater Manchester Pension fund
|
||||
DESCRIPTION:Join us to lobby for GM Pensions free from arms and fossil fuel holdings.
|
||||
URL:https://commonscalendar.uk/event/clean-up-greater-manchester-pension-fund/
|
||||
CATEGORIES:Demonstration
|
||||
ATTACH;FMTTYPE=image/png:https://commonscalendar.uk/wp-content/uploads/2026/11/inbound5975249048432321891.png
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
DTSTART;VALUE=DATE:20261121
|
||||
DTEND;VALUE=DATE:20261122
|
||||
DTSTAMP:20261116T192444Z
|
||||
UID:1902-1795257600-1795344000@commonscalendar.uk
|
||||
SUMMARY:All day gathering
|
||||
DESCRIPTION:Come along any time.
|
||||
URL:https://commonscalendar.uk/event/all-day-gathering/
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
DTSTART;TZID=Europe/London:20261127T090000
|
||||
DTEND;TZID=Europe/London:20261127T100000
|
||||
DTSTAMP:20261116T192444Z
|
||||
UID:1839-1795770000-1795773600@commonscalendar.uk
|
||||
SUMMARY:Clean up Greater Manchester Pension fund
|
||||
DESCRIPTION:Second occurrence, same series.
|
||||
URL:https://commonscalendar.uk/event/clean-up-greater-manchester-pension-fund/
|
||||
ATTACH;FMTTYPE=image/webp:https://commonscalendar.uk/wp-content/uploads/2026/11/poster.webp
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
||||
@@ -0,0 +1,49 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from calbot.expand import parse_events
|
||||
|
||||
FIXTURE = Path(__file__).parent / "fixtures" / "tec-like.ics.txt"
|
||||
|
||||
|
||||
def load_bytes() -> bytes:
|
||||
return FIXTURE.read_bytes()
|
||||
|
||||
|
||||
def test_parses_tec_style_feed():
|
||||
events = parse_events(
|
||||
load_bytes(),
|
||||
datetime(2026, 11, 19, tzinfo=timezone.utc),
|
||||
datetime(2026, 12, 1, tzinfo=timezone.utc),
|
||||
"Europe/London",
|
||||
)
|
||||
assert len(events) == 3
|
||||
|
||||
first, all_day, third = events
|
||||
|
||||
assert first.summary == "Clean up Greater Manchester Pension fund"
|
||||
assert first.series_id == "tec:1839"
|
||||
assert not first.all_day
|
||||
assert first.image_url.endswith("inbound5975249048432321891.png")
|
||||
assert first.url.endswith("clean-up-greater-manchester-pension-fund/")
|
||||
# 09:00 London on 20 Nov 2026 is GMT (UTC).
|
||||
assert first.start == datetime(2026, 11, 20, 9, 0, tzinfo=timezone.utc)
|
||||
|
||||
assert all_day.all_day
|
||||
assert all_day.series_id == "tec:1902"
|
||||
assert all_day.start == datetime(2026, 11, 21, 0, 0, tzinfo=timezone.utc)
|
||||
|
||||
assert third.series_id == "tec:1839"
|
||||
assert third.image_url.endswith("poster.webp")
|
||||
|
||||
|
||||
def test_window_excludes_far_future():
|
||||
events = parse_events(
|
||||
load_bytes(),
|
||||
datetime(2026, 11, 19, tzinfo=timezone.utc),
|
||||
datetime(2026, 11, 20, tzinfo=timezone.utc),
|
||||
"Europe/London",
|
||||
)
|
||||
assert len(events) <= 1
|
||||
@@ -0,0 +1,98 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from calbot.config import FormatConfig
|
||||
from calbot.expand import Event
|
||||
from calbot.format import (
|
||||
find_postcode,
|
||||
format_hashtags,
|
||||
format_time_range,
|
||||
render_post,
|
||||
venue_from_description,
|
||||
venue_from_location,
|
||||
)
|
||||
|
||||
LONDON = ZoneInfo("Europe/London")
|
||||
|
||||
|
||||
def make_event(**overrides) -> Event:
|
||||
defaults = dict(
|
||||
key="uid|2026-11-14T16:30:00+00:00",
|
||||
uid="uid",
|
||||
series_id="tec:1",
|
||||
summary="Folk Night",
|
||||
start=datetime(2026, 11, 14, 16, 30, tzinfo=LONDON).astimezone(timezone.utc),
|
||||
end=datetime(2026, 11, 14, 18, 30, tzinfo=LONDON).astimezone(timezone.utc),
|
||||
all_day=False,
|
||||
location="Old Print Works\n12 High St\nBirmingham\nB12 9AH",
|
||||
description="",
|
||||
url="https://commonscalendar.uk/event/folk-night/",
|
||||
image_url="",
|
||||
tz="Europe/London",
|
||||
)
|
||||
defaults.update(overrides)
|
||||
return Event(**defaults)
|
||||
|
||||
|
||||
def test_render_matches_agreed_format():
|
||||
text = render_post(
|
||||
make_event(),
|
||||
FormatConfig(),
|
||||
LONDON,
|
||||
venue="Old Print Works",
|
||||
postcode="B12 9AH",
|
||||
link="https://commonscalendar.uk/event/folk-night/",
|
||||
)
|
||||
assert text == (
|
||||
"Folk Night\n"
|
||||
"#November #November14\n"
|
||||
"4.30-6.30pm\n"
|
||||
"Old Print Works B12 9AH\n"
|
||||
"https://commonscalendar.uk/event/folk-night/"
|
||||
)
|
||||
|
||||
|
||||
def test_time_range_same_meridiem():
|
||||
start = datetime(2026, 11, 14, 16, 30, tzinfo=LONDON)
|
||||
end = datetime(2026, 11, 14, 18, 30, tzinfo=LONDON)
|
||||
assert format_time_range(start, end) == "4.30-6.30pm"
|
||||
|
||||
|
||||
def test_time_range_crosses_meridiem():
|
||||
start = datetime(2026, 11, 14, 11, 30, tzinfo=LONDON)
|
||||
end = datetime(2026, 11, 14, 13, 30, tzinfo=LONDON)
|
||||
assert format_time_range(start, end) == "11.30am-1.30pm"
|
||||
|
||||
|
||||
def test_hashtags():
|
||||
assert format_hashtags(datetime(2026, 11, 14, 10, 0, tzinfo=LONDON)) == (
|
||||
"#November #November14"
|
||||
)
|
||||
assert format_hashtags(datetime(2026, 1, 1, 10, 0, tzinfo=LONDON)) == "#January #January1"
|
||||
|
||||
|
||||
def test_all_day_event_says_all_day():
|
||||
event = make_event(
|
||||
all_day=True,
|
||||
start=datetime(2026, 11, 21, tzinfo=LONDON).astimezone(timezone.utc),
|
||||
end=datetime(2026, 11, 22, tzinfo=LONDON).astimezone(timezone.utc),
|
||||
)
|
||||
text = render_post(event, FormatConfig(), LONDON, venue="", postcode="", link="")
|
||||
assert "All day" in text
|
||||
assert "#November #November21" in text
|
||||
|
||||
|
||||
def test_postcode_and_venue_helpers():
|
||||
assert find_postcode("Old Print Works, B12 9AH") == "B12 9AH"
|
||||
assert find_postcode("no code here") == ""
|
||||
assert venue_from_location("Old Print Works\nB12 9AH") == "Old Print Works"
|
||||
assert venue_from_description("Collier Room, Methodist Central Hall, M1 1JQ") == "Collier Room"
|
||||
|
||||
|
||||
def test_missing_fields_are_omitted():
|
||||
event = make_event(location="", url="")
|
||||
text = render_post(event, FormatConfig(), LONDON, venue="", postcode="", link="")
|
||||
lines = text.splitlines()
|
||||
assert lines == ["Folk Night", "#November #November14", "4.30-6.30pm"]
|
||||
@@ -0,0 +1,246 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, time, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import httpx
|
||||
|
||||
from calbot.config import (
|
||||
AnnounceConfig,
|
||||
CalendarConfig,
|
||||
Config,
|
||||
FormatConfig,
|
||||
ImagesConfig,
|
||||
PostingConfig,
|
||||
RemindersConfig,
|
||||
SignalConfig,
|
||||
WatchdogConfig,
|
||||
)
|
||||
from calbot.expand import Event
|
||||
from calbot.feed import Feed, normalize_feed_url
|
||||
from calbot.scheduler import (
|
||||
Bot,
|
||||
clamp_send_at,
|
||||
in_window,
|
||||
next_window_start,
|
||||
reminder_time,
|
||||
)
|
||||
from calbot.store import PageInfo, Store
|
||||
from calbot.watchdog import Watchdog
|
||||
|
||||
LONDON = ZoneInfo("Europe/London")
|
||||
UTC = timezone.utc
|
||||
WINDOWS = ((time(8, 0), time(10, 0)), (time(18, 0), time(21, 0)))
|
||||
GUARD = timedelta(hours=1)
|
||||
|
||||
|
||||
def local(*args) -> datetime:
|
||||
return datetime(*args, tzinfo=LONDON)
|
||||
|
||||
|
||||
def make_event(
|
||||
*,
|
||||
uid: str = "1",
|
||||
series_id: str = "tec:1",
|
||||
summary: str = "Folk Night",
|
||||
start: datetime,
|
||||
end: datetime | None = None,
|
||||
all_day: bool = False,
|
||||
) -> Event:
|
||||
return Event(
|
||||
key=f"{uid}|{start.astimezone(UTC).isoformat()}",
|
||||
uid=uid,
|
||||
series_id=series_id,
|
||||
summary=summary,
|
||||
start=start.astimezone(UTC),
|
||||
end=(end or start + timedelta(hours=2)).astimezone(UTC),
|
||||
all_day=all_day,
|
||||
location="Old Print Works\nB12 9AH",
|
||||
url="https://commonscalendar.uk/event/folk-night/",
|
||||
tz="Europe/London",
|
||||
)
|
||||
|
||||
|
||||
def make_config(tmp_path, **overrides) -> Config:
|
||||
defaults = dict(
|
||||
signal=SignalConfig(
|
||||
api_url="http://signal-api:8080", number="+447700900000", group_id="group.test"
|
||||
),
|
||||
calendar=CalendarConfig(url="https://example.test/feed.ics", timezone="Europe/London"),
|
||||
posting=PostingConfig(),
|
||||
announce=AnnounceConfig(),
|
||||
reminders=RemindersConfig(),
|
||||
images=ImagesConfig(),
|
||||
format=FormatConfig(),
|
||||
watchdog=WatchdogConfig(enabled=False),
|
||||
db_path=tmp_path / "db.sqlite3",
|
||||
cache_dir=tmp_path / "cache",
|
||||
)
|
||||
defaults.update(overrides)
|
||||
return Config(**defaults)
|
||||
|
||||
|
||||
# -- window helpers ---------------------------------------------------------
|
||||
|
||||
|
||||
def test_webcal_is_fetched_over_https():
|
||||
assert normalize_feed_url("webcal://example.test/x.ics") == "https://example.test/x.ics"
|
||||
assert normalize_feed_url("https://example.test/x.ics") == "https://example.test/x.ics"
|
||||
|
||||
|
||||
def test_in_window():
|
||||
assert in_window(local(2026, 11, 18, 9, 0), WINDOWS)
|
||||
assert not in_window(local(2026, 11, 18, 15, 0), WINDOWS)
|
||||
assert not in_window(local(2026, 11, 18, 21, 0), WINDOWS)
|
||||
|
||||
|
||||
def test_next_window_start():
|
||||
assert next_window_start(local(2026, 11, 18, 7, 0), WINDOWS) == local(2026, 11, 18, 8, 0)
|
||||
assert next_window_start(local(2026, 11, 18, 10, 30), WINDOWS) == local(2026, 11, 18, 18, 0)
|
||||
assert next_window_start(local(2026, 11, 18, 21, 30), WINDOWS) == local(2026, 11, 19, 8, 0)
|
||||
|
||||
|
||||
def test_clamp_defers_to_evening():
|
||||
due = local(2026, 11, 18, 14, 0)
|
||||
assert clamp_send_at(due, due, local(2026, 11, 25, 18, 0), WINDOWS, GUARD) == local(
|
||||
2026, 11, 18, 18, 0
|
||||
)
|
||||
|
||||
|
||||
def test_clamp_respects_urgent_guard():
|
||||
due = local(2026, 11, 18, 18, 30)
|
||||
start = local(2026, 11, 18, 19, 0)
|
||||
# Next window is tomorrow 08:00, which is after the event: send now.
|
||||
assert clamp_send_at(due, due, start, WINDOWS, GUARD) == due
|
||||
|
||||
|
||||
# -- reminder planning ------------------------------------------------------
|
||||
|
||||
|
||||
def test_reminder_three_days_before_lands_in_window():
|
||||
event = make_event(start=local(2026, 11, 21, 14, 0))
|
||||
config = make_config(Path("/tmp"), posting=PostingConfig())
|
||||
due = reminder_time(event, "3d", local(2026, 11, 10, 12, 0), config, LONDON)
|
||||
assert due == local(2026, 11, 18, 18, 0)
|
||||
|
||||
|
||||
def test_reminder_falls_back_to_day_of():
|
||||
event = make_event(start=local(2026, 11, 20, 14, 0))
|
||||
config = make_config(Path("/tmp"))
|
||||
due = reminder_time(event, "3d", local(2026, 11, 19, 12, 0), config, LONDON)
|
||||
assert due == local(2026, 11, 20, 9, 0)
|
||||
|
||||
|
||||
def test_reminder_day_of_moves_before_early_event():
|
||||
event = make_event(start=local(2026, 11, 20, 8, 30))
|
||||
config = make_config(Path("/tmp"))
|
||||
due = reminder_time(event, "3d", local(2026, 11, 19, 12, 0), config, LONDON)
|
||||
assert due == local(2026, 11, 20, 7, 30)
|
||||
|
||||
|
||||
def test_reminder_disabled_fallback_returns_none():
|
||||
event = make_event(start=local(2026, 11, 20, 14, 0))
|
||||
config = make_config(
|
||||
Path("/tmp"), reminders=RemindersConfig(fallback_to_day_of=False)
|
||||
)
|
||||
assert reminder_time(event, "3d", local(2026, 11, 19, 12, 0), config, LONDON) is None
|
||||
|
||||
|
||||
# -- end to end -------------------------------------------------------------
|
||||
|
||||
|
||||
class FakeSender:
|
||||
def __init__(self):
|
||||
self.sent: list[dict] = []
|
||||
|
||||
def send(self, message, recipients, attachments=None, text_mode="normal"):
|
||||
self.sent.append(
|
||||
{"message": message, "recipients": recipients, "attachments": attachments}
|
||||
)
|
||||
|
||||
|
||||
class FakeImages:
|
||||
def data_uri(self, url):
|
||||
return None
|
||||
|
||||
|
||||
class FakeScraper:
|
||||
def details(self, url):
|
||||
return PageInfo(image_url="", venue="", postcode="")
|
||||
|
||||
|
||||
def build_bot(tmp_path, sender):
|
||||
config = make_config(tmp_path)
|
||||
store = Store(config.db_path)
|
||||
http = httpx.Client()
|
||||
bot = Bot(
|
||||
config=config,
|
||||
store=store,
|
||||
feed=Feed(config.calendar.url, store, 15, http),
|
||||
sender=sender,
|
||||
images=FakeImages(),
|
||||
scraper=FakeScraper(),
|
||||
watchdog=Watchdog(config.watchdog, sender, "+447700900000"),
|
||||
http=http,
|
||||
)
|
||||
return bot, store
|
||||
|
||||
|
||||
def test_first_run_seeds_silently_then_announces_new_series(tmp_path):
|
||||
sender = FakeSender()
|
||||
bot, store = build_bot(tmp_path, sender)
|
||||
|
||||
existing = make_event(uid="1", series_id="tec:1", start=local(2026, 11, 20, 9, 0))
|
||||
bot._sync([existing], local(2026, 11, 18, 12, 0))
|
||||
assert store.due_outbox(local(2026, 11, 18, 18, 0)) == []
|
||||
|
||||
newcomer = make_event(
|
||||
uid="2", series_id="tec:2", summary="Film Night", start=local(2026, 11, 25, 19, 0)
|
||||
)
|
||||
bot._sync([existing, newcomer], local(2026, 11, 18, 12, 1))
|
||||
pending = store.due_outbox(local(2026, 11, 18, 18, 0))
|
||||
assert [item.kind for item in pending] == ["announce"]
|
||||
|
||||
bot._dispatch(local(2026, 11, 18, 18, 0))
|
||||
assert len(sender.sent) == 1
|
||||
assert sender.sent[0]["recipients"] == ["group.test"]
|
||||
assert sender.sent[0]["message"].startswith("Film Night")
|
||||
assert "#November #November25" in sender.sent[0]["message"]
|
||||
|
||||
# A later occurrence of the same series must not be announced again.
|
||||
third = make_event(
|
||||
uid="3", series_id="tec:2", summary="Film Night", start=local(2026, 12, 2, 19, 0)
|
||||
)
|
||||
bot._sync([existing, newcomer, third], local(2026, 11, 18, 18, 5))
|
||||
assert store.due_outbox(local(2026, 11, 18, 18, 10)) == []
|
||||
|
||||
|
||||
def test_reminder_is_sent_at_its_window(tmp_path):
|
||||
sender = FakeSender()
|
||||
bot, store = build_bot(tmp_path, sender)
|
||||
|
||||
event = make_event(uid="9", series_id="tec:9", start=local(2026, 11, 25, 14, 0))
|
||||
bot._sync([event], local(2026, 11, 18, 12, 0)) # seeds; still plans reminders
|
||||
bot._dispatch(local(2026, 11, 18, 18, 0))
|
||||
assert sender.sent == []
|
||||
|
||||
# Three days before is 22 Nov 14:00, deferred to the 18:00 window.
|
||||
bot._dispatch(local(2026, 11, 22, 18, 5))
|
||||
assert len(sender.sent) == 1
|
||||
assert sender.sent[0]["message"].startswith("Folk Night")
|
||||
|
||||
# Idempotent: dispatching again must not resend.
|
||||
bot._dispatch(local(2026, 11, 22, 18, 10))
|
||||
assert len(sender.sent) == 1
|
||||
|
||||
|
||||
def test_stale_reminder_is_dropped_not_sent(tmp_path):
|
||||
sender = FakeSender()
|
||||
bot, store = build_bot(tmp_path, sender)
|
||||
|
||||
event = make_event(uid="10", series_id="tec:10", start=local(2026, 11, 18, 15, 0))
|
||||
bot._sync([event], local(2026, 11, 18, 12, 0))
|
||||
bot._dispatch(local(2026, 11, 18, 18, 0)) # event already started
|
||||
assert sender.sent == []
|
||||
assert store.due_outbox(local(2026, 11, 18, 18, 5)) == []
|
||||
Reference in New Issue
Block a user