Last active
February 24, 2024 13:09
-
-
Save sophea/2f981e621bc02df77bb327f1adaf3f40 to your computer and use it in GitHub Desktop.
Lambda function alert the message to ms-team
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import os | |
from urllib.request import Request, urlopen | |
from urllib.error import URLError, HTTPError | |
HOOK_URL = os.environ['HookUrl'] | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def lambda_handler(event, context): | |
logger.info("Event: " + str(event)) | |
message = json.loads(event['Records'][0]['Sns']['Message']) | |
logger.info("Message: " + str(message)) | |
alarm_name = message['AlarmName'] | |
old_state = message['OldStateValue'] | |
new_state = message['NewStateValue'] | |
reason = message['NewStateReason'] | |
data = { | |
"colour": "64a837", | |
"title": "**%s** is resolved" % alarm_name, | |
"text": "**%s** has changed from %s to %s - %s" % (alarm_name, old_state, new_state, reason) | |
} | |
if new_state.lower() == 'alarm': | |
data = { | |
"colour": "d63333", | |
"title": "Red Alert - There is an issue %s" % alarm_name, | |
"text": "**%s** has changed from %s to %s - %s" % (alarm_name, old_state, new_state, reason) | |
} | |
message = { | |
"@context": "https://schema.org/extensions", | |
"@type": "MessageCard", | |
"themeColor": data["colour"], | |
"title": data["title"], | |
"text": data["text"] | |
} | |
req = Request(HOOK_URL, json.dumps(message).encode('utf-8')) | |
try: | |
response = urlopen(req) | |
response.read() | |
logger.info("Message posted") | |
return { "status": "200 OK"} | |
except HTTPError as e: | |
logger.error("Request failed: %d %s", e.code, e.reason) | |
except URLError as e: | |
logger.error("Server connection failed: %s", e.reason) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding
req.add_header('Content-Type', 'application/json')
was required to make this run