Skip to content

Commit 815c0ec

Browse files
authored
Merge pull request #11 from diversen7/main
Allow to set channel id when using mattermost
2 parents ff642a1 + 99fd917 commit 815c0ec

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

config_dist.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
"slack_channel": "SLACK_CHANNEL_ID",
2626
}
2727

28+
# Mattermost specific
2829
# Mattermost specific
2930
CONFIG_MATTERMOST = {
30-
"mattermost_webhook": "https://chat.openaws.dk/hooks/webhook-id",
31+
"base_url": "https://my-mattermost-instance.com",
32+
"token": "your-token-here",
33+
"channel_id": "your-channel-id-here",
3134
}
3235

3336

ubuntu_auto_upgrade/notify/mattermost.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@
33
import logging
44
import json
55

6-
76
logger: logging.Logger = logging.getLogger(__name__)
87

98

10-
def send_mattermost_message(subject, message):
11-
mattermost_webhook_url = CONFIG_MATTERMOST["mattermost_webhook"]
12-
13-
payload = {"text": f"{message}"}
14-
headers = {"Content-Type": "application/json"}
9+
def send_mattermost_message(message: str) -> bool:
1510

1611
try:
17-
response = requests.post(
18-
mattermost_webhook_url,
12+
base_url = CONFIG_MATTERMOST.get("base_url", "").rstrip("/")
13+
token = CONFIG_MATTERMOST.get("token")
14+
channel_id = CONFIG_MATTERMOST.get("channel_id")
15+
payload = {"channel_id": channel_id, "message": message}
16+
17+
headers = {
18+
"Authorization": f"Bearer {token}",
19+
"Content-Type": "application/json",
20+
"Accept": "application/json",
21+
}
22+
23+
url = f"{base_url}/api/v4/posts?set_online=false"
24+
resp = requests.post(
25+
url,
1926
headers=headers,
2027
data=json.dumps(payload),
28+
timeout=10,
2129
)
22-
response.raise_for_status()
23-
24-
except Exception as e:
25-
logging.error(f"Failed to send email: {e}")
26-
logging.exception(e)
30+
resp.raise_for_status()
31+
return True
32+
except requests.RequestException as e:
33+
# Mirror your previous logging style but correct the message
34+
logger.error(f"Failed to send Mattermost message: {e}")
35+
logger.exception(e)
36+
return False

0 commit comments

Comments
 (0)