Repository: opsdroid/opsdroid

Issue:
Match @mention regex
Hi,

This is a general question, and I couldn't find any information in the documentation.
I would like the bot to capture a user/team mention in slack - "@devopsteam" for example.

The channel user mention a group (which is not the bot), and I would like to capture the message and create a ticket based on that. I tried to do a @match_regex(r"@devopsteam"), and It does not work as expected.

Appreciate your comments


Discussion:
Hello, @omers  thank you for raising this issue, could I ask you what you get from the logs if you set this as debug? From your example things should work, I am keen on knowing if we need to escape the `@` although I don't think that's the case 🤔 

If you don't have the time, I'll try to test things and debug the issue tomorrow 👍 
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Turns out I have the same issue here.
What I have noticed if I extend the regular expression to match the mention is that this is logged (our bot's name in this case is `@guido`):

```
2023-02-24 17:19:26,436 DEBUG slack_sdk.socket_mode.aiohttp A new message enqueued (current queue size: 1, session: s_8783087079229)
23
2023-02-24 17:19:26,436 DEBUG slack_sdk.socket_mode.aiohttp Message processing started (type: events_api, envelope_id: 99a20b47-8dda-4fc4-a819-cdedd2801019, session: s_8783087079229)
22
2023-02-24 17:19:26,436 DEBUG slack_sdk.socket_mode.aiohttp Sending a message: {"envelope_id": "99a20b47-8dda-4fc4-a819-cdedd2801019"} from session: s_8783087079229
21
2023-02-24 17:19:26,436 DEBUG opsdroid.connector.slack.create_events Replacing userids in message with usernames
20
2023-02-24 17:19:26,436 DEBUG opsdroid.connector.slack.connector Got slack event: <opsdroid.events.Message(text=guido my message is here)>
19
2023-02-24 17:19:26,436 DEBUG opsdroid.core Parsing input: <opsdroid.events.Message(text=guido my message is here)>.
18
2023-02-24 17:19:26,437 DEBUG opsdroid.core Processing parsers...
17
2023-02-24 17:19:26,437 INFO root my_skill: message <opsdroid.events.Message(text=guido my message is here)>
```

I wrote in a slack channel: `@guido my message is here` and what I get back as a result of the invocation of my skill method as `message` attribute is `guido my message is here`, so the `@` character is stripped away.

I'm not sure why this is the behaviour, but this is what I'm seeing. There seems to be no way to distinguish the `@guido do something` from `guido do something`. The version of opsdroid we're using based on the helm chart is v0.25.0.

Perhaps this patch could retain the `@` characters for usernames before replacing them:

```
diff --git a/opsdroid/connector/slack/connector.py b/opsdroid/connector/slack/connector.py
index c35adf3..93fbfb0 100644
--- a/opsdroid/connector/slack/connector.py
+++ b/opsdroid/connector/slack/connector.py
@@ -431,31 +431,32 @@ class ConnectorSlack(Connector):
                     self.known_users[userid] = user_info
                 return user_info
             elif "bot" in response.data:
                 bot_info = response.data["bot"]
                 if isinstance(bot_info, dict):
                     self.known_bots[userid] = bot_info
                 return bot_info
 
     async def replace_usernames(self, message):
         """Replace User ID with username in message text."""
         userids = re.findall(r"\<\@([A-Z0-9]+)(?:\|.+)?\>", message)
 
         for userid in userids:
             user_info = await self.lookup_username(userid)
             message = message.replace(
-                "<@{userid}>".format(userid=userid), user_info["name"]
+                "<@{userid}>".format(userid=userid),
+                "@{username}".format(username=user_info["name"])
             )
 
         return message
 
     @register_event(opsdroid.events.Message)
     async def _send_message(self, message):
         """Respond with a message."""
         _LOGGER.debug(
             _("Responding with: '%s' in room  %s."), message.text, message.target
         )
 
         data = self._generate_base_data(message)
         data["text"] = message.text
 
         return await self.slack_web_client.api_call(
```


Resolving patch:
diff --git a/opsdroid/connector/slack/connector.py b/opsdroid/connector/slack/connector.py
--- a/opsdroid/connector/slack/connector.py
+++ b/opsdroid/connector/slack/connector.py
@@ -444,7 +444,8 @@ async def replace_usernames(self, message):
         for userid in userids:
             user_info = await self.lookup_username(userid)
             message = message.replace(
-                "<@{userid}>".format(userid=userid), user_info["name"]
+                "<@{userid}>".format(userid=userid),
+                "@{username}".format(username=user_info["name"]),
             )
 
         return message


Test patch:
diff --git a/opsdroid/connector/slack/tests/test_connector.py b/opsdroid/connector/slack/tests/test_connector.py
--- a/opsdroid/connector/slack/tests/test_connector.py
+++ b/opsdroid/connector/slack/tests/test_connector.py
@@ -295,7 +295,7 @@ async def test_replace_usernames(connector):
     connector.known_users = {"U01NK1K9L68": {"name": "Test User"}}
     message = "hello <@U01NK1K9L68>"
     replaced_message = await connector.replace_usernames(message)
-    assert replaced_message == "hello Test User"
+    assert replaced_message == "hello @Test User"
 
 
 @pytest.mark.anyio