mirror of
https://github.com/Sea-Haven-Industries/afterhours-shift-manager.git
synced 2026-10-01 15:23:11 +00:00
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
|
|
"""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)
|
||
|
|
|
||
|
|
# Show this week (Monday = today)
|
||
|
|
blocks = build_week_schedule(schedule, start_date=now)
|
||
|
|
|
||
|
|
slack.chat_postMessage(
|
||
|
|
channel=channel_id,
|
||
|
|
blocks=blocks,
|
||
|
|
text=f"After-Hours Schedule — Week of {now.strftime('%b %-d')}",
|
||
|
|
)
|
||
|
|
|
||
|
|
logger.info("Posted weekly schedule to channel %s", channel_id)
|
||
|
|
return {"posted": True, "channel": channel_id}
|