diff --git a/etl-pipeline/custom/construct_send_telegram_message.py b/etl-pipeline/custom/construct_send_telegram_message.py index 90a8b87..6f56ea2 100644 --- a/etl-pipeline/custom/construct_send_telegram_message.py +++ b/etl-pipeline/custom/construct_send_telegram_message.py @@ -66,19 +66,15 @@ def send_message(token, chat_id, msg=''): @custom def transform_custom(data, *args, **kwargs): - telegram_token = get_secret_value('TELEGRAM_BOT_TOKEN') - telegram_chat_id = get_secret_value('TELEGRAM_CHAT_ID') - aqi_summary = get_latest_aqi_summary(data) - if aqi_summary: - avg_aqi, max_aqi, min_aqi = aqi_summary - message = construct_message(avg_aqi, max_aqi, min_aqi) - send_message(token=telegram_token, chat_id=telegram_chat_id, msg=message) - return data - - -@test -def test_output(output, *args) -> None: - """ - Template code for testing the output of the block. - """ - assert output is not None, 'The output is undefined' + klogger = kwargs.get('logger') + try: + telegram_token = get_secret_value('TELEGRAM_BOT_TOKEN') + telegram_chat_id = get_secret_value('TELEGRAM_CHAT_ID') + aqi_summary = get_latest_aqi_summary(data) + if aqi_summary: + avg_aqi, max_aqi, min_aqi = aqi_summary + message = construct_message(avg_aqi, max_aqi, min_aqi) + response = send_message(token=telegram_token, chat_id=telegram_chat_id, msg=message) + klogger.info(f'Telegram response: {response}') + except Exception as e: + klogger.exception(f'An error occurred: {e}') \ No newline at end of file diff --git a/etl-pipeline/custom/construct_send_twitter_message.py b/etl-pipeline/custom/construct_send_twitter_message.py index 7d96092..c788be0 100644 --- a/etl-pipeline/custom/construct_send_twitter_message.py +++ b/etl-pipeline/custom/construct_send_twitter_message.py @@ -53,16 +53,18 @@ def send_message(msg=''): access_token=access_token, access_token_secret=access_token_secret) response = client.create_tweet(text=msg) - - return + return response @custom def transform_custom(data, *args, **kwargs): + klogger = kwargs.get('logger') + try: + aqi_summary = get_latest_aqi_summary(data) + if aqi_summary: + avg_aqi, max_aqi, min_aqi = aqi_summary + message = construct_message(avg_aqi, max_aqi, min_aqi) + response = send_message(msg=message) + klogger.info(f'Twitter response: {response}') + except Exception as e: + klogger.exception(f'An error occurred: {e}') - - aqi_summary = get_latest_aqi_summary(data) - if aqi_summary: - avg_aqi, max_aqi, min_aqi = aqi_summary - message = construct_message(avg_aqi, max_aqi, min_aqi) - send_message(msg=message) - return data diff --git a/etl-pipeline/pipelines/twitter_bot/metadata.yaml b/etl-pipeline/pipelines/twitter_bot/metadata.yaml index 75a5200..b74718e 100755 --- a/etl-pipeline/pipelines/twitter_bot/metadata.yaml +++ b/etl-pipeline/pipelines/twitter_bot/metadata.yaml @@ -54,7 +54,7 @@ blocks: language: python name: construct_send_twitter_message retry_config: null - status: updated + status: executed timeout: null type: custom upstream_blocks: