1#!/usr/bin/env python3
2"""Post a message to a Discord-style webhook.
3
4usage: send-webhook.py URL_FILE [MESSAGE...] (message from stdin when omitted)
5
6The URL carries the token, so it is read from a private file, never argv.
7"""
8import json
9import sys
10import urllib.request
11from pathlib import Path
12
13if len(sys.argv) < 2:
14 sys.exit(__doc__)
15url = Path(sys.argv[1]).read_text().strip()
16content = " ".join(sys.argv[2:]) or sys.stdin.read().strip()
17if not content:
18 sys.exit("empty message")
19
20request = urllib.request.Request(
21 url,
22 json.dumps({"content": content}).encode(),
23 {"Content-Type": "application/json", "User-Agent": "snowglobe-webhook/1.0"},
24)
25with urllib.request.urlopen(request, timeout=10) as response:
26 print(response.status)