2026-04-03 18:32:32 -04:00
|
|
|
"""Lambda handler — posts the weekly on-call schedule to Slack every Monday at 7am ET."""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
|
from slack_sdk import WebClient
|
|
|
|
|
|
|
|
|
|
from src.blocks import build_week_schedule
|
|
|
|
|
from src.schedule import ShiftSchedule
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
|
EASTERN = ZoneInfo("America/New_York")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handler(event, context):
|
|
|
|
|
now = datetime.now(EASTERN)
|
|
|
|
|
|
|
|
|
|
# DST guard — same pattern as the 3CX scheduler
|
|
|
|
|
if now.hour != 7:
|
|
|
|
|
logger.info("ET hour is %d, not 7am — skipping (wrong DST rule fired)", now.hour)
|
|
|
|
|
return {"skipped": True}
|
|
|
|
|
|
|
|
|
|
ssm = boto3.client("ssm")
|
|
|
|
|
bot_token = ssm.get_parameter(
|
|
|
|
|
Name=os.environ["SLACK_BOT_TOKEN_PARAM"], WithDecryption=True
|
|
|
|
|
)["Parameter"]["Value"]
|
|
|
|
|
channel_id = ssm.get_parameter(
|
|
|
|
|
Name=os.environ["SHIFT_CHANNEL_PARAM"], WithDecryption=True
|
|
|
|
|
)["Parameter"]["Value"]
|
|
|
|
|
|
|
|
|
|
schedule = ShiftSchedule()
|
|
|
|
|
slack = WebClient(token=bot_token)
|
|
|
|
|
|
2026-04-07 18:01:10 -04:00
|
|
|
# Show two weeks starting from today (Monday)
|
2026-04-03 18:32:32 -04:00
|
|
|
blocks = build_week_schedule(schedule, start_date=now)
|
|
|
|
|
|
2026-04-07 18:01:10 -04:00
|
|
|
end_date = now + timedelta(days=13)
|
2026-04-03 18:32:32 -04:00
|
|
|
slack.chat_postMessage(
|
|
|
|
|
channel=channel_id,
|
|
|
|
|
blocks=blocks,
|
2026-04-07 18:01:10 -04:00
|
|
|
text=f"After-Hours Schedule — {now.strftime('%b %-d')} to {end_date.strftime('%b %-d')}",
|
2026-04-03 18:32:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger.info("Posted weekly schedule to channel %s", channel_id)
|
|
|
|
|
return {"posted": True, "channel": channel_id}
|