Compare commits

..

No commits in common. "407cbddae31095acbccebd9e7ba77fe3604812eb" and "ec1bfefbb908136e050b0d1b4c058721c04abfb6" have entirely different histories.

View File

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