Merge pull request #9 from djangoflow/main

cleaner refactor
This commit is contained in:
Julian-Samuel Gebühr 2023-12-20 15:44:22 +01:00 committed by GitHub
commit 407cbddae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,38 +20,41 @@ More information is on [Github](https://github.com/moan0s/alertbot)
def convert_slack_webhook_to_markdown(data): def convert_slack_webhook_to_markdown(data):
markdown_message = "" # Error Handling: Check if data is a dictionary
if not isinstance(data, dict):
return ["Input data must be a dictionary"]
markdown_parts = []
attachment_titles = [] attachment_titles = []
if "text" in data: if "text" in data:
markdown_message += f"{data['text']}\n" markdown_parts.append(f"{data['text']}")
if "attachments" in data: if "attachments" in data:
for attach in data["attachments"]: for attach in data["attachments"]:
attachment_md = "> "
if "title" in attach: if "title" in attach:
attachment_titles.append(attach['title']) attachment_titles.append(attach['title'])
if "title" in attach and "title_link" in attach: title_md = f"## {attach['title']}" if "title_link" not in attach else f"[{attach['title']}]({attach['title_link']})"
attachment_md += f"[{attach['title']}]({attach['title_link']})\n> " markdown_parts.append(f"> {title_md}")
elif "title" in attach:
attachment_md += f"## {attach['title']}\n> " for key in ["text", "image_url"]:
if "text" in attach: if key in attach and attach[key] is not None:
attachment_md += f"{attach['text']}\n> " extra_md = attach[key] if key == "text" else f"![Image]({attach[key]})"
if "image_url" in attach and attach["image_url"] is not None: markdown_parts.append(f"> {extra_md}")
attachment_md += f"![Image]({attach['image_url']})\n> "
if 'fields' in attach: if 'fields' in attach:
for field in attach['fields']: field_parts = [f"- **{field['title']}** : {field['value']}" for field in attach['fields']]
attachment_md += f"- **{field['title']}** : {field['value']}\n> " markdown_parts.extend([f"> {part}" for part in field_parts])
markdown_message += attachment_md + "\n"
if "sections" in data: if "sections" in data:
markdown_parts.append("")
for section in data["sections"]: for section in data["sections"]:
if "activityTitle" in section and section['activityTitle'] not in attachment_titles: if "activityTitle" in section and section['activityTitle'] not in attachment_titles:
markdown_message += f"## {section['activityTitle']}\n" markdown_parts.append(f"## {section['activityTitle']}")
if "activitySubtitle" in section: if "activitySubtitle" in section:
markdown_message += f"{section['activitySubtitle']}\n" markdown_parts.append(section['activitySubtitle'])
return [markdown_message] return ['\n'.join(markdown_parts)]
def get_alert_type(data): def get_alert_type(data):