From 6c546ebb6fac66b28e1c1d5588a88ca5f78798aa Mon Sep 17 00:00:00 2001 From: yage Date: Sat, 24 Jun 2023 07:40:31 -0600 Subject: [PATCH] v3.1.0 ===================================================================== --- Changes ------------------------------ - improved capability of py/discord/post-message to include username flag, and defaults for each value in env config --- .env.template | 4 ++++ .env.template.descriptions | 4 ++++ py/discord/post-message.py | 28 ++++++++++++++-------------- py/lib/http/discord/send_message.py | 22 ++++++++++++++++++---- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/.env.template b/.env.template index 162e9c8..2bf5e5b 100644 --- a/.env.template +++ b/.env.template @@ -6,8 +6,12 @@ export AWS__EFS__LOCAL_MOUNT_POINT= export DIRECTUS__API_TOKEN= export DIRECTUS__BASE_URL= export DISCORD__BOT_TOKEN= +export DISCORD__CONTENT_FOOTER= +export DISCORD__CONTENT_HEADER= export DISCORD__DEFAULT_AVATAR_URL= export DISCORD__DEFAULT_CHANNEL_ID= +export DISCORD__DEFAULT_USERNAME= +export DISCORD__DEFAULT_WEBHOOK= export I3__BORDER_PIXEL_SIZE= export I3__DMENU_FONT_SIZE= export I3__GLOBAL_FONT_SIZE= diff --git a/.env.template.descriptions b/.env.template.descriptions index 94cee42..67b8d22 100644 --- a/.env.template.descriptions +++ b/.env.template.descriptions @@ -8,8 +8,12 @@ DIRECTUS__API_TOKEN | details for a directus instance DIRECTUS__BASE_URL | DISCORD__BOT_TOKEN | details for discord bot +DISCORD__CONTENT_HEADER | +DISCORD__CONTENT_FOOTER | DISCORD__DEFAULT_AVATAR_URL | DISCORD__DEFAULT_CHANNEL_ID | +DISCORD__DEFAULT_USERNAME | +DISCORD__DEFAULT_WEBHOOK | I3__BORDER_PIXEL_SIZE | custom i3 configuration settings I3__DMENU_FONT_SIZE | diff --git a/py/discord/post-message.py b/py/discord/post-message.py index b18d846..88b5f5b 100755 --- a/py/discord/post-message.py +++ b/py/discord/post-message.py @@ -13,19 +13,14 @@ if __name__ != '__main__': ##################################################################### def main(args, stream): - if args.body is None: + if args.content is None: print(f'reading input from {stream.input.name}', file=stderr) - args.body = ''.join(stream.readlines()).strip() + args.content = ''.join(stream.readlines()).strip() - if len(args.body) == 0: - args.body = 'PING' + if len(args.content) == 0: + args.content = 'PING' - response = discord.send_message( - content = args.body, - channel_id = args.channel_id, - webhook = args.webhook, - avatar_url = args.avatar_url, - ) + response = discord.send_message(**vars(args)) stream.writeline(dumps({ **(response.json() if response.text != '' else {'message': 'OK'}), @@ -38,23 +33,28 @@ execute(main, description = 'post a message to the indicated discord channel', parse_args = [ ( ['-b', '--body'], { - 'dest' : 'body', + 'dest' : 'content', 'help' : 'message body', 'required' : False, }), ( ['-c', '--channel-id'], { 'dest' : 'channel_id', - 'help' : 'target channel id', + 'help' : 'override default target channel id', 'required' : False, }), ( ['-w', '--webhook'], { 'dest' : 'webhook', - 'help' : 'target webhook (takes precedence over -c)', + 'help' : 'override default target webhook (takes precedence over -c)', 'required' : False, }), ( ['--avatar-url'], { 'dest' : 'avatar_url', - 'help' : 'replace default avatar_url', + 'help' : 'override default avatar_url', + 'required' : False, + }), + ( ['--username'], { + 'dest' : 'username', + 'help' : 'override default username', 'required' : False, }), ] diff --git a/py/lib/http/discord/send_message.py b/py/lib/http/discord/send_message.py index d7b9715..6385a8d 100644 --- a/py/lib/http/discord/send_message.py +++ b/py/lib/http/discord/send_message.py @@ -1,9 +1,9 @@ from py.lib.scwrypts import getenv from py.lib.http.discord import request -def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwargs): - if channel_id is None: - channel_id = getenv('DISCORD__DEFAULT_CHANNEL_ID', required=False) +def send_message(content, channel_id=None, webhook=None, username=None, avatar_url=None, **kwargs): + if username is None: + username = getenv('DISCORD__DEFAULT_USERNAME', required=False) if avatar_url is None: avatar_url = getenv('DISCORD__DEFAULT_AVATAR_URL', required=False) @@ -12,11 +12,25 @@ def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwar if webhook is not None: endpoint = f'webhooks/{webhook}' + elif channel_id is not None: endpoint = f'channels/{channel_id}/messages' + + elif (webhook := getenv('DISCORD__DEFAULT_WEBHOOK', required=False)) is not None: + endpoint = f'webhooks/{webhook}' + + elif (channel_id := getenv('DISCORD__DEFAULT_CHANNEL_ID', required=False)) is not None: + endpoint = f'channels/{channel_id}/messages' + else: raise ValueError('must provide target channel_id or webhook') + if (header := getenv('DISCORD__CONTENT_HEADER', required=False)) is not None: + content = f'{header}{content}' + + if (footer := getenv('DISCORD__CONTENT_FOOTER', required=False)) is not None: + content = f'{content}{footer}' + return request( method = 'POST', @@ -25,7 +39,7 @@ def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwar key: value for key, value in { 'content': content, - 'username': 'wrobot', + 'username': username, 'avatar_url': avatar_url, **kwargs, }.items()