| #!/usr/bin/env python3 | |
| """Post a concise update to the team updates Slack webhook. | |
| Usage: | |
| python mlops/slack/post_update.py --text "message" --webhook $SLACK_WEBHOOK_UPDATES | |
| If --webhook omitted, reads SLACK_WEBHOOK_UPDATES from env. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--text", required=True) | |
| ap.add_argument("--webhook", default=os.getenv("SLACK_WEBHOOK_UPDATES", "")) | |
| args = ap.parse_args() | |
| if not args.webhook: | |
| raise SystemExit("Missing updates webhook: set SLACK_WEBHOOK_UPDATES or pass --webhook") | |
| payload = json.dumps({"text": args.text}).encode("utf-8") | |
| try: | |
| try: | |
| import requests # type: ignore | |
| import requests | |
| requests.post(args.webhook, json={"text": args.text}, timeout=5) | |
| except Exception: | |
| import urllib.request | |
| req = urllib.request.Request(args.webhook, data=payload, headers={"Content-Type": "application/json"}) | |
| urllib.request.urlopen(req, timeout=5).read() | |
| except Exception as e: | |
| raise SystemExit(f"Slack post failed: {e}") | |
| if __name__ == "__main__": | |
| main() | |