From a62252ed15c17fbd5d0f64ceb617950b15fa0024 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 28 Sep 2023 19:15:01 -0400 Subject: [PATCH] Allow custom bot naming --- app/classes/web/webhooks/discord_webhook.py | 7 ++++--- app/classes/web/webhooks/mattermost_webhook.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/classes/web/webhooks/discord_webhook.py b/app/classes/web/webhooks/discord_webhook.py index e63649a1..a60d63d0 100644 --- a/app/classes/web/webhooks/discord_webhook.py +++ b/app/classes/web/webhooks/discord_webhook.py @@ -3,7 +3,7 @@ from app.classes.web.webhooks.base_webhook import WebhookProvider class DiscordWebhook(WebhookProvider): - def _construct_discord_payload(self, server_name, title, message, color): + def _construct_discord_payload(self, server_name, title, message, color, bot_name): """ Constructs the payload required for sending a Discord webhook notification. @@ -30,7 +30,7 @@ class DiscordWebhook(WebhookProvider): headers = {"Content-type": "application/json"} payload = { - "username": self.WEBHOOK_USERNAME, + "username": bot_name, "avatar_url": self.WEBHOOK_PFP_URL, "embeds": [ { @@ -69,7 +69,8 @@ class DiscordWebhook(WebhookProvider): Exception: If there's an error in dispatching the webhook. """ 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( - server_name, title, message, color + server_name, title, message, color, bot_name ) return self._send_request(url, payload, headers) diff --git a/app/classes/web/webhooks/mattermost_webhook.py b/app/classes/web/webhooks/mattermost_webhook.py index ecd3b3db..68e33f11 100644 --- a/app/classes/web/webhooks/mattermost_webhook.py +++ b/app/classes/web/webhooks/mattermost_webhook.py @@ -2,7 +2,7 @@ from app.classes.web.webhooks.base_webhook import WebhookProvider class MattermostWebhook(WebhookProvider): - def _construct_mattermost_payload(self, server_name, title, message): + def _construct_mattermost_payload(self, server_name, title, message, bot_name): """ Constructs the payload required for sending a Mattermost webhook notification. @@ -28,7 +28,7 @@ class MattermostWebhook(WebhookProvider): headers = {"Content-Type": "application/json"} payload = { "text": formatted_text, - "username": self.WEBHOOK_USERNAME, + "username": bot_name, "icon_url": self.WEBHOOK_PFP_URL, "props": { "card": ( @@ -65,7 +65,8 @@ class MattermostWebhook(WebhookProvider): - Mattermost's `config.json` setting is `"EnablePostUsernameOverride": true` - Mattermost's `config.json` setting is `"EnablePostIconOverride": true` """ + bot_name = kwargs.get("bot_name", self.WEBHOOK_USERNAME) payload, headers = self._construct_mattermost_payload( - server_name, title, message + server_name, title, message, bot_name ) return self._send_request(url, payload, headers)