File size: 1,232 Bytes
0daf510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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()