From 0df858b9ca255662cb04ac4462fc7cd02da3d145 Mon Sep 17 00:00:00 2001 From: Josh Gomez <114548659+jgomez720@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:34:57 -0700 Subject: [PATCH] update discord announce under 2000 char limit (#1628) * update discord announce under 2000 char limit * updating URL handling --- public/announce_release.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/public/announce_release.py b/public/announce_release.py index cdcff4ef8..f023cf956 100644 --- a/public/announce_release.py +++ b/public/announce_release.py @@ -1,16 +1,34 @@ -import requests +import re import os +import requests webhook_url = os.getenv('DISCORD_WEBHOOK_URL') release_version = os.getenv('RELEASE_VERSION') release_body = os.getenv('RELEASE_BODY') -# message to send to Discord +# Regular expression to match URLs +url_pattern = r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)' + +# Function to encase URLs in <> +def encase_urls_with_angle_brackets(match): + url = match.group(0) + return f'<{url}>' + +# Replace all URLs in the release_body with their <> enclosed version +modified_release_body = re.sub(url_pattern, encase_urls_with_angle_brackets, release_body) + +# Ensure the modified_release_body does not exceed Discord's character limit +max_length = 500 # Adjust as needed +if len(modified_release_body) > max_length: + modified_release_body = modified_release_body[:max_length].rsplit(' ', 1)[0] # Avoid cutting off in the middle of a word + modified_release_body += "... for full changelog, check out the link above." + +# Message to send to Discord data = { "content": f''' - **{release_version}** is now available! Check out the latest features and improvements here: https://zoo.dev/modeling-app/download - {release_body} + **{release_version}** is now available! Check out the latest features and improvements here: + {modified_release_body} ''', "username": "Modeling App Release Updates", "avatar_url": "https://raw.githubusercontent.com/KittyCAD/modeling-app/main/public/discord-avatar.png" @@ -23,4 +41,7 @@ response = requests.post(webhook_url, json=data) if response.status_code == 204: print("Successfully sent the message to Discord.") else: - print("Failed to send the message to Discord.") \ No newline at end of file + print(f"Failed to send the message to Discord. Status code: {response.status_code}, Response: {response.text}") + +print(modified_release_body) +print(data["content"])