aboutsummaryrefslogtreecommitdiff
path: root/scripts/webhook.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/webhook.py')
-rwxr-xr-xscripts/webhook.py70
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)
+