Last active
March 17, 2020 01:01
-
-
Save ian-whitestone/4b8045c6d88ed6369dcd6d9085fe730d to your computer and use it in GitHub Desktop.
Script for sending failure notifications in slack
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
""" | |
Trigger slack notifications | |
""" | |
import argparse | |
import logging | |
import os | |
from slack.web.client import WebClient | |
LOGGER = logging.getLogger(__name__) | |
logging.basicConfig(level=logging.INFO) | |
SLACK_TOKEN = os.environ['SLACK_API_TOKEN'] | |
ALERTS_CHANNEL = os.environ['ALERTS_CHANNEL'] | |
SLACK_WEB_CLIENT = WebClient(token=SLACK_TOKEN) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("run_id") | |
parser.add_argument("log_filepath") | |
args = parser.parse_args() | |
LOGGER.info("Sending slack alert") | |
response = SLACK_WEB_CLIENT.chat_postMessage( | |
channel='alerts', | |
text=f"⚠️ Failed run: {args.run_id}" | |
) | |
if not response["ok"]: | |
LOGGER.warning(f"Error occured while posting to slack:\n{response}") | |
LOGGER.info("Uploading log file") | |
response = SLACK_WEB_CLIENT.files_upload( | |
channels=ALERTS_CHANNEL, | |
file=args.log_filepath, | |
title=f"Log file for {args.run_id}" | |
) | |
if not response["ok"]: | |
LOGGER.warning(f"Error occured while uploading file to slack:\n{response}") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment