diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-10-11 10:26:57 -0400 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-10-11 11:48:18 -0400 |
| commit | 68bbcb3f57400c4a34e28a8dfb60749e9970913b (patch) | |
| tree | 1218e1edcdd30ebafde73f58065189e8db5e8e55 | |
| parent | faa918fc684023aa290f5cd1da4b32ef3609ae59 (diff) | |
| download | dotfiles-68bbcb3f57400c4a34e28a8dfb60749e9970913b.tar.gz dotfiles-68bbcb3f57400c4a34e28a8dfb60749e9970913b.tar.xz | |
Add my webhook script
| -rw-r--r-- | webhook.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/webhook.py b/webhook.py new file mode 100644 index 0000000..3db70a8 --- /dev/null +++ b/webhook.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +import requests, json, sys + +expected_status = 204 +url = "" +# Default for discord, easy enough to override +msg = { + "username": "{hostname} script", + "content": "```{stdin}```", +} + +customVars = {} + +def merge_dicts(*dict_args): + result = {} + for dictionary in dict_args: + result.update(dictionary) + return result + +def template(msg, fmt): + out = {} + for k, v in msg.items(): + if isinstance(v, dict): + out[k] = template(v, fmt) + else: + out[k] = v.format(**fmt) + return out + + +def help(): + print("Usage: {} [-m msg] [-u url] [-s expected_status] [-V var content]...".format(sys.argv[0])) + print("A small python script to send webhooks easily from the command line utilizing arguments and stdin") + print("Only requrement is the requests library") + print("Defaults:") + print("\tmsg: " + json.dumps(msg)) + print("\turl: " + url) + print("\texpected_status: ", expected_status) + print("") + print("The [-V var content] arguments allow you to specifiy custom vars to be overridden in the json msg.") + sys.exit(2) + +n = 1 +while n < len(sys.argv): + if sys.argv[n] == "-u": + url = sys.argv[n+1] + n += 2 + elif sys.argv[n] == "-m": + msg = json.loads(sys.argv[n+1]) + n += 2 + elif sys.argv[n] == "-s": + expected_status = int(sys.argv[n+1]) + n += 2 + elif sys.argv[n] == "-V": + customVars[sys.argv[n+1]] = sys.argv[n+2] + n += 3 + else: + help() + +if url == "": + sys.stderr.write("No URL provided\n") + help() + +msg2 = template(msg, merge_dicts(customVars , {"stdin": sys.stdin.read()})) +r = requests.post(url, json=msg2) + +if r.status_code != expected_status: + print(r) + print(r.text) + sys.exit(1) + |
