Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discord notifications #107

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

digital-pedrocosta
Copy link

New feature to add Discord notifications (same way as the already implemented msgs to Telegram)
The user needs to provide the Discord's webhook url

@Linus045
Copy link
Collaborator

Linus045 commented Dec 8, 2021

I think at this point we should refactor this somehow.
I tried to do some changes in #103 but if there are gonna be more and more notification channels later it will get annoying to provide the extras to the logging functions.
I think a general 'channel' param should be added and the forwarding to discord/telegram/whatever should be handled on the logger level, so instead of
logger.info("new-coin-bot online", extra={'TELEGRAM': 'STARTUP', 'DISCORD': 'STARTUP'})
its just
logger.info("new-coin-bot online", channel='STARTUP')

@Linus045
Copy link
Collaborator

Linus045 commented Dec 8, 2021

also the package needs to be added to the requirements.txt, otherwise people will get errors when trying to use the bot.

@nitad54448

This comment has been minimized.

@Linus045

This comment has been minimized.

@nitad54448

This comment has been minimized.

@Linus045

This comment has been minimized.

@nitad54448

This comment has been minimized.

@@ -0,0 +1,42 @@
import logging
from discordwebhook import Discord
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add package to requirements.txt

try:
auth = yaml.load(file, Loader=yaml.FullLoader)
discord_webhook_url = str(auth['discord_webhook_url'])
valid_auth = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module/class "private" variables should be marked with an underscore
_valid_auth and _discord_webhook_url


config = load_config('config.yml')

with open('auth/auth.yml') as file:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole block should be part of the DiscordHandler class.
I would suggest an init() method to handle this initialization (not the constructor because its bad practice to handle File/IO stuff there IMO).

key = getattr(record, 'DISCORD')

# unknown message key
if not key in config['DISCORD']['NOTIFICATIONS']:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not key in config['DISCORD']['NOTIFICATIONS']:
if key not in config['DISCORD']['NOTIFICATIONS']:

@@ -30,6 +31,11 @@
except KeyError:
pass

try:
log_discord = config['DISCORD']['ENABLED']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log_discord should get initialized to false in the beginning (similar to log_to_console), otherwise you will get an NameError if the value does not exist in the config.yaml file, as soon as you try to access it in the if-statement below

@Linus045 Linus045 changed the base branch from master to develop January 3, 2022 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants