diff --git a/changelog/4817.improvement.rst b/changelog/4817.improvement.rst new file mode 100644 index 000000000000..71fb53e94139 --- /dev/null +++ b/changelog/4817.improvement.rst @@ -0,0 +1,2 @@ +Part of Slack sanitization: +Multiple garbled URL's in a string coming from slack will be converted into actual strings. ``Example: health check of and to health check of eemdb.net and eemdb1.net`` diff --git a/rasa/core/channels/slack.py b/rasa/core/channels/slack.py index 6aa4b556fbcf..73bf823ef2bc 100644 --- a/rasa/core/channels/slack.py +++ b/rasa/core/channels/slack.py @@ -209,17 +209,17 @@ def _sanitize_user_message(text, uids_to_remove) -> Text: ]: text = re.sub(regex, replacement, text) - """Find mailto or http links like or 'in text and substitute it with original content + """Find multiple mailto or http links like or 'in text and substitute it with original content """ - pattern = r"\<(mailto:|(http|https):\/\/).*\|.*\>" - match = re.search(pattern, text) + pattern = r"(\<(?:mailto|http|https):\/\/.*?\|.*?\>)" + match = re.findall(pattern, text) if match: - regex = match.group(0) - replacement = regex.split("|")[1] - replacement = replacement.replace(">", "") - text = text.replace(regex, replacement) + for remove in match: + replacement = remove.split("|")[1] + replacement = replacement.replace(">", "") + text = text.replace(remove, replacement) return text.strip() @staticmethod diff --git a/tests/core/test_channels.py b/tests/core/test_channels.py index c8af169ddad3..4570f756eb2d 100644 --- a/tests/core/test_channels.py +++ b/tests/core/test_channels.py @@ -469,6 +469,8 @@ def test_slack_message_sanitization(): target_message_1 = "You can sit here if you want" target_message_2 = "Hey, you can sit here if you want !" target_message_3 = "Hey, you can sit here if you want!" + target_message_4 = "convert garbled url to vicdb-f.net" + target_message_5 = "convert multiple garbled url to vicdb-f.net. Also eemdb-p.net" uid_token = f"<@{test_uid}>" raw_messages = [ @@ -484,6 +486,8 @@ def test_slack_message_sanitization(): "You can sit here{uid}if you want", "Hey {uid}, you can sit here if you want{uid}!", "Hey{uid} , you can sit here if you want {uid}!", + "convert garbled url to ", + "convert multiple garbled url to . Also ", ] ] @@ -494,6 +498,8 @@ def test_slack_message_sanitization(): target_message_1, target_message_2, target_message_3, + target_message_4, + target_message_5, ] sanitized_messages = [