diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-29 21:53:53 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-29 21:53:53 -0400 |
| commit | 5b25c5155312f626813e0d36b7933f5eba801dd2 (patch) | |
| tree | 6e264a92dbe13ed77821a5c5bd536b6701768442 /scripts/webhook.py | |
| parent | e9b933ea6ce85f5d4f4653b7e5e6a7c836fcc893 (diff) | |
| download | dotfiles-5b25c5155312f626813e0d36b7933f5eba801dd2.tar.gz dotfiles-5b25c5155312f626813e0d36b7933f5eba801dd2.tar.xz | |
Major dotfile cleanup
Diffstat (limited to 'scripts/webhook.py')
| -rwxr-xr-x | scripts/webhook.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/scripts/webhook.py b/scripts/webhook.py new file mode 100755 index 0000000..3db70a8 --- /dev/null +++ b/scripts/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) + |
