Created
June 25, 2021 05:40
-
-
Save tpdns90321/eb8c80eb35867a9e91a9916ff3abc15c to your computer and use it in GitHub Desktop.
aws-sns-lambda-slcakbot-or-webhook
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 urllib3 | |
import json | |
import os | |
from datetime import datetime, timezone, timedelta | |
seoul = timezone(timedelta(hours=9) ,name="Asia/Seoul") | |
http = urllib3.PoolManager() | |
url = os.environ["SLACK_WEBHOOK_URL"] | |
slack_channel = os.environ["SLACK_CHANNEL"] | |
slack_username = os.environ["SLACK_USERNAME"] | |
text_format = "{}부터 API 서버가 작동하지 않는 것 같습니다!" | |
time_format = "%Y-%m-%dT%H:%M:%S.%f%z" | |
def lambda_handler(event, context): | |
sns_message = event['Records'][0]['Sns']['Message'] | |
detect_time = datetime.strptime( | |
sns_message["StateChangeTime"], | |
time_format | |
) | |
detect_time = detect_time.astimezone(seoul) | |
text = text_format.format(detect_time) | |
msg = { | |
"channel": slack_channel, | |
"username": slack_username, | |
"text": text, | |
"icon_emoji": "" | |
} | |
encoded_msg = json.dumps(msg).encode('utf-8') | |
resp = http.request('POST', url, body=encoded_msg) | |
print({ | |
"message": sns_message, | |
"response": resp.data | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment