meal-order-manager/scripts/notify_slack.py
Adam Moussa d332affd56
Initial commit: meal ordering automation system (#1)
Playwright-based menu scraper for Redefine Meals, self-contained HTML
order form with S3/CloudFront hosting, DynamoDB-backed order submission
via API Gateway, and automated payroll deduction reports via SES.
2026-05-12 18:25:15 -04:00

74 lines
2 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 = f"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"
f"*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()