Last active
August 22, 2021 21:04
-
-
Save nothub/d0d5c6e749582e35093ea505dd082f35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import json | |
import select | |
import socket | |
import sys | |
from urllib import request as urllib_request | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(prog='discord-webhook', epilog='Message content must be passed via stdin.') | |
parser.add_argument_group('required arguments').add_argument( | |
'-u', '--url', | |
action='store', | |
type=str, | |
required=True, | |
metavar='URL', | |
help='url' | |
) | |
args = parser.parse_args() | |
if not select.select([sys.stdin, ], [], [], 0.0)[0]: | |
print('message is empty!') | |
exit(1) | |
message = ''.join([x for x in sys.stdin if x]) | |
request = urllib_request.Request(args.url, method="POST") | |
request.add_header('Content-Type', 'application/json') | |
request.add_header('User-Agent', 'discord-webhook.py') | |
data = json.dumps({'username': socket.gethostname(), 'content': message.lstrip().rstrip()}).encode() | |
response = urllib_request.urlopen(request, data=data).read().decode('utf-8') | |
if response: | |
print('response: ' + response) | |
else: | |
print('success') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment