Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}


Expand Down
36 changes: 23 additions & 13 deletions ubuntu_auto_upgrade/notify/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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