Skip to content
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

Multiple links in message - Slack sanitization #4817

Merged
merged 10 commits into from
Feb 3, 2020
2 changes: 2 additions & 0 deletions changelog/4817.improvement.rst
Original file line number Diff line number Diff line change
@@ -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 <http://eemdb.net|eemdb.net> and <http://eemdb1.net|eemdb1.net> to health check of eemdb.net and eemdb1.net``
14 changes: 7 additions & 7 deletions rasa/core/channels/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mailto:[email protected]|[email protected]> or '<http://url.com|url.com>in text and substitute it with original content
"""Find multiple mailto or http links like <mailto:[email protected]|[email protected]> or '<http://url.com|url.com>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
Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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 <http://vicdb-f.net|vicdb-f.net>",
"convert multiple garbled url to <http://vicdb-f.net|vicdb-f.net>. Also <http://eemdb-p.net|eemdb-p.net>",
]
]

Expand All @@ -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 = [
Expand Down