v3.1.0
===================================================================== --- Changes ------------------------------ - improved capability of py/discord/post-message to include username flag, and defaults for each value in env config
This commit is contained in:
parent
9783119a7d
commit
6c546ebb6f
@ -6,8 +6,12 @@ export AWS__EFS__LOCAL_MOUNT_POINT=
|
|||||||
export DIRECTUS__API_TOKEN=
|
export DIRECTUS__API_TOKEN=
|
||||||
export DIRECTUS__BASE_URL=
|
export DIRECTUS__BASE_URL=
|
||||||
export DISCORD__BOT_TOKEN=
|
export DISCORD__BOT_TOKEN=
|
||||||
|
export DISCORD__CONTENT_FOOTER=
|
||||||
|
export DISCORD__CONTENT_HEADER=
|
||||||
export DISCORD__DEFAULT_AVATAR_URL=
|
export DISCORD__DEFAULT_AVATAR_URL=
|
||||||
export DISCORD__DEFAULT_CHANNEL_ID=
|
export DISCORD__DEFAULT_CHANNEL_ID=
|
||||||
|
export DISCORD__DEFAULT_USERNAME=
|
||||||
|
export DISCORD__DEFAULT_WEBHOOK=
|
||||||
export I3__BORDER_PIXEL_SIZE=
|
export I3__BORDER_PIXEL_SIZE=
|
||||||
export I3__DMENU_FONT_SIZE=
|
export I3__DMENU_FONT_SIZE=
|
||||||
export I3__GLOBAL_FONT_SIZE=
|
export I3__GLOBAL_FONT_SIZE=
|
||||||
|
@ -8,8 +8,12 @@ DIRECTUS__API_TOKEN | details for a directus instance
|
|||||||
DIRECTUS__BASE_URL |
|
DIRECTUS__BASE_URL |
|
||||||
|
|
||||||
DISCORD__BOT_TOKEN | details for discord bot
|
DISCORD__BOT_TOKEN | details for discord bot
|
||||||
|
DISCORD__CONTENT_HEADER |
|
||||||
|
DISCORD__CONTENT_FOOTER |
|
||||||
DISCORD__DEFAULT_AVATAR_URL |
|
DISCORD__DEFAULT_AVATAR_URL |
|
||||||
DISCORD__DEFAULT_CHANNEL_ID |
|
DISCORD__DEFAULT_CHANNEL_ID |
|
||||||
|
DISCORD__DEFAULT_USERNAME |
|
||||||
|
DISCORD__DEFAULT_WEBHOOK |
|
||||||
|
|
||||||
I3__BORDER_PIXEL_SIZE | custom i3 configuration settings
|
I3__BORDER_PIXEL_SIZE | custom i3 configuration settings
|
||||||
I3__DMENU_FONT_SIZE |
|
I3__DMENU_FONT_SIZE |
|
||||||
|
@ -13,19 +13,14 @@ if __name__ != '__main__':
|
|||||||
#####################################################################
|
#####################################################################
|
||||||
|
|
||||||
def main(args, stream):
|
def main(args, stream):
|
||||||
if args.body is None:
|
if args.content is None:
|
||||||
print(f'reading input from {stream.input.name}', file=stderr)
|
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:
|
if len(args.content) == 0:
|
||||||
args.body = 'PING'
|
args.content = 'PING'
|
||||||
|
|
||||||
response = discord.send_message(
|
response = discord.send_message(**vars(args))
|
||||||
content = args.body,
|
|
||||||
channel_id = args.channel_id,
|
|
||||||
webhook = args.webhook,
|
|
||||||
avatar_url = args.avatar_url,
|
|
||||||
)
|
|
||||||
|
|
||||||
stream.writeline(dumps({
|
stream.writeline(dumps({
|
||||||
**(response.json() if response.text != '' else {'message': 'OK'}),
|
**(response.json() if response.text != '' else {'message': 'OK'}),
|
||||||
@ -38,23 +33,28 @@ execute(main,
|
|||||||
description = 'post a message to the indicated discord channel',
|
description = 'post a message to the indicated discord channel',
|
||||||
parse_args = [
|
parse_args = [
|
||||||
( ['-b', '--body'], {
|
( ['-b', '--body'], {
|
||||||
'dest' : 'body',
|
'dest' : 'content',
|
||||||
'help' : 'message body',
|
'help' : 'message body',
|
||||||
'required' : False,
|
'required' : False,
|
||||||
}),
|
}),
|
||||||
( ['-c', '--channel-id'], {
|
( ['-c', '--channel-id'], {
|
||||||
'dest' : 'channel_id',
|
'dest' : 'channel_id',
|
||||||
'help' : 'target channel id',
|
'help' : 'override default target channel id',
|
||||||
'required' : False,
|
'required' : False,
|
||||||
}),
|
}),
|
||||||
( ['-w', '--webhook'], {
|
( ['-w', '--webhook'], {
|
||||||
'dest' : 'webhook',
|
'dest' : 'webhook',
|
||||||
'help' : 'target webhook (takes precedence over -c)',
|
'help' : 'override default target webhook (takes precedence over -c)',
|
||||||
'required' : False,
|
'required' : False,
|
||||||
}),
|
}),
|
||||||
( ['--avatar-url'], {
|
( ['--avatar-url'], {
|
||||||
'dest' : '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,
|
'required' : False,
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
from py.lib.scwrypts import getenv
|
from py.lib.scwrypts import getenv
|
||||||
from py.lib.http.discord import request
|
from py.lib.http.discord import request
|
||||||
|
|
||||||
def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwargs):
|
def send_message(content, channel_id=None, webhook=None, username=None, avatar_url=None, **kwargs):
|
||||||
if channel_id is None:
|
if username is None:
|
||||||
channel_id = getenv('DISCORD__DEFAULT_CHANNEL_ID', required=False)
|
username = getenv('DISCORD__DEFAULT_USERNAME', required=False)
|
||||||
|
|
||||||
if avatar_url is None:
|
if avatar_url is None:
|
||||||
avatar_url = getenv('DISCORD__DEFAULT_AVATAR_URL', required=False)
|
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:
|
if webhook is not None:
|
||||||
endpoint = f'webhooks/{webhook}'
|
endpoint = f'webhooks/{webhook}'
|
||||||
|
|
||||||
elif channel_id is not None:
|
elif channel_id is not None:
|
||||||
endpoint = f'channels/{channel_id}/messages'
|
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:
|
else:
|
||||||
raise ValueError('must provide target channel_id or webhook')
|
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(
|
return request(
|
||||||
method = 'POST',
|
method = 'POST',
|
||||||
@ -25,7 +39,7 @@ def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwar
|
|||||||
key: value
|
key: value
|
||||||
for key, value in {
|
for key, value in {
|
||||||
'content': content,
|
'content': content,
|
||||||
'username': 'wrobot',
|
'username': username,
|
||||||
'avatar_url': avatar_url,
|
'avatar_url': avatar_url,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
}.items()
|
}.items()
|
||||||
|
Loading…
Reference in New Issue
Block a user