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