meal-order-manager/scripts/notify_slack.py
Adam Moussa 440117c9a1
Fix ruff lint and format violations, update README (#5)
Apply ruff check --fix and ruff format across all Python files to
pass CI pipeline. Remove unused imports (os, sys), fix f-strings
without placeholders. Update README to reflect sync-roster Lambda,
corrected shared layer path, and current project structure.
2026-05-12 19:31:27 -04:00

83 lines
2.1 KiB
Python

"""
Posts the weekly menu notification to Slack.
Used by GitHub Actions after uploading the form to S3.
Usage: python3 scripts/notify_slack.py <form_url>
"""
import json
import os
import sys
import urllib.request
SLACK_BOT_TOKEN_SECRET = os.environ.get(
"SLACK_BOT_TOKEN_SECRET", "meal-order-manager/slack-bot-token"
)
SLACK_CHANNEL_PARAM = os.environ.get(
"SLACK_CHANNEL_PARAM", "/meal-order-manager/slack-channel-id"
)
def get_secret(secret_id):
import boto3
return boto3.client("secretsmanager").get_secret_value(SecretId=secret_id)[
"SecretString"
]
def get_parameter(name):
import boto3
return boto3.client("ssm").get_parameter(Name=name, WithDecryption=True)[
"Parameter"
]["Value"]
def main():
if len(sys.argv) < 2:
print("Usage: notify_slack.py <form_url>", file=sys.stderr)
sys.exit(1)
form_url = sys.argv[1]
token = get_secret(SLACK_BOT_TOKEN_SECRET)
channel = get_parameter(SLACK_CHANNEL_PARAM)
text = "This week's meal order is open! Deadline: Thursday 6pm."
blocks = [
{
"type": "header",
"text": {"type": "plain_text", "text": "Meal Order Open"},
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": (
f"*<{form_url}|Place your order>*\n\n*Deadline:* Thursday 6pm\n"
),
},
},
]
payload = json.dumps({"channel": channel, "text": text, "blocks": blocks}).encode()
req = urllib.request.Request(
"https://slack.com/api/chat.postMessage",
data=payload,
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(req) as resp:
result = json.loads(resp.read())
if result.get("ok"):
print(f"Slack notification sent to channel {channel}")
else:
print(f"Slack API error: {result.get('error')}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()