-
Notifications
You must be signed in to change notification settings - Fork 244
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
tkalir
wants to merge
2
commits into
data-for-change:dev
Choose a base branch
from
tkalir:2763-dont-trigger-telegram-flow-if-no-infographics
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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): | ||
|
@@ -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: | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest adding the |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
)