from slack_sdk import WebClient from slack_sdk.errors import SlackApiError import os import json import logging # # SLACK credentials from environment variables # token = os.environ.get("SLACK_TOKEN") # channel_id = os.environ.get("SLACK_CHANNEL_ID") # SLACK credentials from config json with open('config.json', 'r') as f: config = json.load(f) token = config['slack']["token"] channel_id = config['slack']["channel_id"] def prettify_text(text_dict: dict = {}): etl_message = "" for report_name in text_dict['ETL'].keys(): etl_message += f"\n\n*{report_name.capitalize()}*\n" for value in text_dict['ETL'][report_name]: etl_message += f"• {value['date']}: {value['message']}\n" ae_message = "\n\n*Appreciation Engine*\n" if text_dict['Appreciation_Engine']['status'] == 'OK': ae_message += f"• {text_dict['Appreciation_Engine']['date']}: {text_dict['Appreciation_Engine']['info']}\n" elif text_dict['Appreciation_Engine']['step'] == '1': for date,report in text_dict['Appreciation_Engine']['details']['PG'].items(): ae_message += f"• {date}: {text_dict['Appreciation_Engine']['info']} Missing reports: {report}\n" elif text_dict['Appreciation_Engine']['step'] == '1_1': for date,report in text_dict['Appreciation_Engine']['details']['PG_1_1'].items(): ae_message += f"• {date}: {text_dict['Appreciation_Engine']['info']} Not fully disassembled reports: {report}\n" elif text_dict['Appreciation_Engine']['step'] == '1_2': for date,context in text_dict['Appreciation_Engine']['details']['PG_1_2'].items(): ae_message += f"• {date}: {text_dict['Appreciation_Engine']['info']} Missing contexts: {context}\n" else: for date,report in text_dict['Appreciation_Engine']['details']['SF'].items(): ae_message += f"• {date}: {text_dict['Appreciation_Engine']['info']} Missing reports: {report}\n" youtube_trending_charts = "\n\n*Youtube Trending Charts*\n" youtube_trending_charts += f"• {text_dict['Youtube_Trending_charts']['date']}: {text_dict['Youtube_Trending_charts']['info']}\n" shazam_charts = "\n\n*Shazam Charts*\n" shazam_charts += f"• {text_dict['Shazam_Charts']['date']}: {text_dict['Shazam_Charts']['info']}\n" fivetran_facebook_google_ads = "\n\n*Fivetran Facebook/Google Ads*\n" fivetran_facebook_google_ads += f"• {text_dict['Fivetran_Facebook_Google_Ads']['date']}: {text_dict['Fivetran_Facebook_Google_Ads']['info']}\n" lf_message = "\n\n*Linkfire*\n" if text_dict['Linkfire']['status'] == 'OK': lf_message += f"• {text_dict['Linkfire']['date']}: {text_dict['Linkfire']['info']}\n" elif text_dict['Linkfire']['check'] == '1': for date,report in text_dict['Linkfire']['details']['SLZ_STATUS_CHECK_1'].items(): lf_message += f"• {date}: {text_dict['Linkfire']['info']}\n" else: lf_message += f"• {text_dict['Linkfire']['date']}: {text_dict['Linkfire']['info']}\n" apple_message = "\n\n*Apple Charts*\n" if text_dict['Apple']['status'] == 'OK': apple_message += f"• {text_dict['Apple']['date']}: {text_dict['Apple']['info']}\n" elif text_dict['Apple']['check'] == '1': for date,report in text_dict['Apple']['details']['SLZ_STATUS_CHECK_1'].items(): apple_message += f"• {date}: {text_dict['Apple']['info']}\n" else: apple_message += f"• {text_dict['Apple']['date']}: {text_dict['Apple']['info']}\n" sf_message = "\n\n*Spotify Charts*\n" if text_dict['Spotify']['status'] == 'OK': sf_message += f"• {text_dict['Spotify']['date']}: {text_dict['Spotify']['info']} ***\n" elif text_dict['Spotify']['check'] == '1': for date,report in text_dict['Spotify']['details']['SLZ_STATUS_CHECK_1'].items(): sf_message += f"• {date}: {text_dict['Spotify']['info']} ***\n" else: sf_message += f"• {text_dict['Spotify']['date']}: {text_dict['Spotify']['info']} ***\n" sf_message += f"*** Charts for Cyprus, Russian Federation might be missing due to the data source issues\n" chartmetric_message = "\n\n\n*Chartmetric*\n" chartmetric_message += f"We observe lag for artist(s) available through RTI\n" # chartmetric_message += f"{text_dict['Chartmetric']['message']}\n" # for cm_artist, value in text_dict['Chartmetric']['details'].items(): # chartmetric_message += f" - {value['ARTIST_NAME']}: {value['lags']}\n" end_text = "Report can be found in attachment" return etl_message + ae_message + youtube_trending_charts + shazam_charts + lf_message + apple_message + sf_message + chartmetric_message + end_text def post_message(text: str = ''): client = WebClient(token=token) try: client.chat_postMessage(channel=channel_id, text=text) except SlackApiError as e: logging.error(f"Error posting message: {e}") def files_upload(path: str = ''): client = WebClient(token=token) files_list = os.listdir(path) # unfortunately below is the only way to upload multiple files via Slack API sdk # here is even the issue about it https://github.com/slackapi/python-slack-sdk/issues/442 # and that probably won't be solved in nearest future for file in files_list: try: client.files_upload(channels=channel_id, file=f'{path}/{file}') except SlackApiError as e: logging.error(f"Error uploading file: {e}") if __name__ == '__main__': pass # files_upload('file_test/test') # message = prettify_text(text_dict=test_dict) # post_message(text=message) # print(prettify_text(text_dict=test_dict))