Implement Jinja into callback function and discord provider

Will add to the other providers later after testing
This commit is contained in:
Zedifus 2024-04-21 18:04:32 +01:00
parent 915245bbff
commit d5ae2c606b
2 changed files with 25 additions and 7 deletions

View File

@ -56,29 +56,46 @@ def callback(called_func):
try:
res = called_func(*args, **kwargs)
finally:
events = WebhookFactory.get_monitored_events()
if called_func.__name__ in events:
event_type = called_func.__name__
# For send_command, Retrieve command from args or kwargs
# TODO Test Properly
command = args[1] if len(args) > 1 else kwargs.get("command", "")
if event_type in WebhookFactory.get_monitored_events():
server_webhooks = HelpersWebhooks.get_webhooks_by_server(
args[0].server_id, True
)
for swebhook in server_webhooks:
if called_func.__name__ in str(swebhook.trigger).split(","):
if event_type in str(swebhook.trigger).split(","):
logger.info(
f"Found callback for event {called_func.__name__}"
f"Found callback for event {event_type}"
f" for server {args[0].server_id}"
)
webhook = HelpersWebhooks.get_webhook_by_id(swebhook.id)
webhook_provider = WebhookFactory.create_provider(
webhook["webhook_type"]
)
event_data = {
"server_name": args[0].name,
"server_id": args[0].server_id,
"user": "",
"user_id" "command": command,
"timestamp": datetime.datetime.utcnow().strftime(
"%Y-%m-%d %H:%M:%S"
),
}
if res is not False and swebhook.enabled:
webhook_provider.send(
bot_name=webhook["bot_name"],
server_name=args[0].name,
title=webhook["name"],
url=webhook["url"],
message=webhook["body"],
message_template=webhook["body"],
event_data=event_data,
color=webhook["color"],
bot_name=webhook["bot_name"],
)
return res

View File

@ -51,7 +51,7 @@ class DiscordWebhook(WebhookProvider):
return payload, headers
def send(self, server_name, title, url, message, **kwargs):
def send(self, server_name, title, url, message_template, event_data, **kwargs):
"""
Sends a Discord webhook notification using the given details.
@ -74,6 +74,7 @@ class DiscordWebhook(WebhookProvider):
Raises:
Exception: If there's an error in dispatching the webhook.
"""
message = self.render_template(message_template, event_data)
color = kwargs.get("color", "#005cd1") # Default to a color if not provided.
bot_name = kwargs.get("bot_name", self.WEBHOOK_USERNAME)
payload, headers = self._construct_discord_payload(