Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2763 dont trigger telegram flow if no infographics #2764

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions anyway/infographic_image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,35 @@ def download_infographics_images(generated_images_names, newsflash_id, local_inf


def upload_directory_to_s3(download_directory, newsflash_id):
s3uploader = S3Uploader()
path = f"{download_directory}/"
for filename in os.listdir(path):
s3uploader.upload_to_s3(f"{path}/{filename}", newsflash_id)
log_message = {
"event": "upload_to_s3",
"newsflash_id": newsflash_id
}
uploaded_files = []
try:
s3uploader = S3Uploader()
path = f"{download_directory}/"
filenames = os.listdir(path)
for filename in filenames:
s3uploader.upload_to_s3(f"{path}/{filename}", newsflash_id)
uploaded_files.append(filename)

log_message.update ({
"status": "success",
"file_count": len(filenames),
"files": filenames
})
logging.info(json.dumps(log_message))

except Exception as e:
log_message.update ({
"status": "failure",
"uploaded_files": uploaded_files,
"error": str(e),
"traceback": repr(e),
})
logging.error(json.dumps(log_message))
raise #raise exception again to fail dag

class S3Uploader(S3DataClass):
def __init__(self):
Expand Down
57 changes: 44 additions & 13 deletions anyway/telegram_accident_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from anyway.models import TelegramForwardedMessages
from anyway.utilities import trigger_airflow_dag
from anyway.app_and_db import db
from anyway.infographics_utils import get_infographics_data_by_newsflash
from anyway.infographics_utils import get_infographics_data_by_newsflash, WIDGETS
import telebot
import boto3
import time
Expand Down Expand Up @@ -97,14 +97,40 @@ def send_infographics_to_telegram(root_message_id, newsflash_id, channel_of_init
#to create a comment on the channel message, we need to send a reply to the
#forwareded message in the discussion group.
bot = telebot.TeleBot(secrets.get("BOT_TOKEN"))

linked_group = telegram_linked_group_by_channel[channel_of_initial_message]
items_for_send = get_items_for_send(newsflash_id)
for url, text in items_for_send:
bot.send_photo(linked_group, url, reply_to_message_id=root_message_id, caption=text)

send_after_infographics_message(bot, root_message_id, newsflash_id, linked_group)
logging.info("notification send done")
sent_items = []
channel_type = "post verification" if channel_of_initial_message == TELEGRAM_POST_VERIFICATION_CHANNEL_CHAT_ID \
else "pre verification"

log_message = {
"event": "send infographics to telegram",
"newsflash_id": newsflash_id,
"root_message_id": root_message_id,
"channel_of_initial_message": channel_of_initial_message,
"channel_type": channel_type
}
try:
linked_group = telegram_linked_group_by_channel[channel_of_initial_message]
items_for_send = get_items_for_send(newsflash_id)

for url, text in items_for_send:
bot.send_photo(linked_group, url, reply_to_message_id=root_message_id, caption=text)
sent_items.append((url, text))

send_after_infographics_message(bot, root_message_id, newsflash_id, linked_group)
log_message.update({
"status": "success",
"sent_count": len(sent_items)
})

except Exception as e:
log_message.update({
"sent_items": sent_items,
"sent_count": len(sent_items),
"status": "failure",
"error": str(e)
})
logging.error(json.dumps(log_message))
raise


def extract_infographic_name_from_s3_object(s3_object_name):
Expand All @@ -131,7 +157,12 @@ def create_public_urls_for_infographics_images(folder_name):


def trigger_generate_infographics_and_send_to_telegram(newsflash_id, pre_verification_chat=True):
dag_conf = {"news_flash_id": newsflash_id}
dag_conf["chat_id"] = TELEGRAM_CHANNEL_CHAT_ID if pre_verification_chat \
else TELEGRAM_POST_VERIFICATION_CHANNEL_CHAT_ID
trigger_airflow_dag("generate-and-send-infographics-images", dag_conf)
infographics_data = get_infographics_data_by_newsflash(newsflash_id)
if WIDGETS not in infographics_data:
Copy link
Collaborator

@atalyaalon atalyaalon Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a years_ago param so we'll be able to adjust from get_infographics_data_by_newsflash (and default will be current default - BE_CONST.DEFAULT_NUMBER_OF_YEARS_AGO)

logging.warning(f"no infographics to send for newsflash {newsflash_id}")
else:
logging.info(f"widgets found in json for {newsflash_id}, triggering dag")
dag_conf = {"news_flash_id": newsflash_id}
dag_conf["chat_id"] = TELEGRAM_CHANNEL_CHAT_ID if pre_verification_chat \
else TELEGRAM_POST_VERIFICATION_CHANNEL_CHAT_ID
trigger_airflow_dag("generate-and-send-infographics-images", dag_conf)
Copy link
Collaborator

@atalyaalon atalyaalon Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding the years_ago param to dag_conf to have this flexibility in this dag as well, can be useful in the future

Loading