From 99fd917dfafcfa916979306283474bdc352e2068 Mon Sep 17 00:00:00 2001 From: Dennis Iversen Date: Tue, 30 Sep 2025 11:18:13 +0200 Subject: [PATCH] update to use a channel and a token --- config_dist.py | 5 +++- ubuntu_auto_upgrade/notify/mattermost.py | 36 +++++++++++++++--------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/config_dist.py b/config_dist.py index a4244ec..17562cd 100644 --- a/config_dist.py +++ b/config_dist.py @@ -25,9 +25,12 @@ "slack_channel": "SLACK_CHANNEL_ID", } +# Mattermost specific # Mattermost specific CONFIG_MATTERMOST = { - "mattermost_webhook": "https://chat.openaws.dk/hooks/webhook-id", + "base_url": "https://my-mattermost-instance.com", + "token": "your-token-here", + "channel_id": "your-channel-id-here", } diff --git a/ubuntu_auto_upgrade/notify/mattermost.py b/ubuntu_auto_upgrade/notify/mattermost.py index c5f6c12..74bfc78 100644 --- a/ubuntu_auto_upgrade/notify/mattermost.py +++ b/ubuntu_auto_upgrade/notify/mattermost.py @@ -3,24 +3,34 @@ import logging import json - logger: logging.Logger = logging.getLogger(__name__) -def send_mattermost_message(subject, message): - mattermost_webhook_url = CONFIG_MATTERMOST["mattermost_webhook"] - - payload = {"text": f"{message}"} - headers = {"Content-Type": "application/json"} +def send_mattermost_message(message: str) -> bool: try: - response = requests.post( - mattermost_webhook_url, + base_url = CONFIG_MATTERMOST.get("base_url", "").rstrip("/") + token = CONFIG_MATTERMOST.get("token") + channel_id = CONFIG_MATTERMOST.get("channel_id") + payload = {"channel_id": channel_id, "message": message} + + headers = { + "Authorization": f"Bearer {token}", + "Content-Type": "application/json", + "Accept": "application/json", + } + + url = f"{base_url}/api/v4/posts?set_online=false" + resp = requests.post( + url, headers=headers, data=json.dumps(payload), + timeout=10, ) - response.raise_for_status() - - except Exception as e: - logging.error(f"Failed to send email: {e}") - logging.exception(e) + resp.raise_for_status() + return True + except requests.RequestException as e: + # Mirror your previous logging style but correct the message + logger.error(f"Failed to send Mattermost message: {e}") + logger.exception(e) + return False