Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Slack support get user info (#3125)

Tao Sun1d6e2714d890
diff --git a/camel/toolkits/slack_toolkit.py b/camel/toolkits/slack_toolkit.py
index f6fa2f8ad..051db2bfd 100644
--- a/camel/toolkits/slack_toolkit.py
+++ b/camel/toolkits/slack_toolkit.py
@@ -237,7 +237,8 @@ class SlackToolkit(BaseToolkit):
) -> str:
r"""Send a message to a Slack channel. When use this function you must
call `get_slack_channel_information` function first to get the
- `channel id`.
+ `channel id`. If use user, you must use `get_slack_user_list`
+ function first to get the user id.
Args:
message (str): The message to send.
@@ -306,6 +307,52 @@ class SlackToolkit(BaseToolkit):
except SlackApiError as e:
return f"Error deleting message: {e.response['error']}"
+ def get_slack_user_list(self) -> str:
+ r"""Retrieve a list of all users in the Slack workspace.
+
+ Returns:
+ str: A JSON string representing a list of users. Each user
+ object contains 'id', 'name'.
+ """
+ from slack_sdk.errors import SlackApiError
+
+ try:
+ slack_client = self._login_slack()
+ response = slack_client.users_list()
+ users = response["members"]
+ filtered_users = [
+ {
+ "id": user["id"],
+ "name": user["name"],
+ }
+ for user in users
+ ]
+
+ return json.dumps(filtered_users, ensure_ascii=False)
+ except SlackApiError as e:
+ return f"Error retrieving user list: {e.response['error']}"
+
+ def get_slack_user_info(self, user_id: str) -> str:
+ r"""Retrieve information about a specific user in the Slack workspace.
+ normally, you don't need to use this method, when you need to get a
+ user's detailed information, use this method. Use `get_slack_user_list`
+ function first to get the user id.
+
+ Args:
+ user_id (str): The ID of the user to retrieve information about.
+
+ Returns:
+ str: A JSON string representing the user's information.
+ """
+ from slack_sdk.errors import SlackApiError
+
+ try:
+ slack_client = self._login_slack()
+ response = slack_client.users_info(user=user_id)
+ return json.dumps(response, ensure_ascii=False)
+ except SlackApiError as e:
+ return f"Error retrieving user info: {e.response['error']}"
+
def get_tools(self) -> List[FunctionTool]:
r"""Returns a list of FunctionTool objects representing the
functions in the toolkit.
@@ -322,4 +369,6 @@ class SlackToolkit(BaseToolkit):
FunctionTool(self.get_slack_channel_message),
FunctionTool(self.send_slack_message),
FunctionTool(self.delete_slack_message),
+ FunctionTool(self.get_slack_user_list),
+ FunctionTool(self.get_slack_user_info),
]
diff --git a/test/toolkits/test_slack_functions.py b/test/toolkits/test_slack_functions.py
index e4ec7e4cf..7777b7123 100644
--- a/test/toolkits/test_slack_functions.py
+++ b/test/toolkits/test_slack_functions.py
@@ -133,3 +133,31 @@ def test_delete_slack_message(slack_toolkit):
mock_client.chat_delete.return_value = {}
response = slack_toolkit.delete_slack_message("123", "123")
assert response == "{}"
+
+
+def test_get_slack_user_list(slack_toolkit):
+ with patch(
+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"
+ ) as mock_login:
+ mock_client = MagicMock()
+ mock_login.return_value = mock_client
+ mock_client.users_list.return_value = {
+ "members": [{"id": "123", "name": "test_user"}]
+ }
+ response = slack_toolkit.get_slack_user_list()
+ assert response == '[{"id": "123", "name": "test_user"}]'
+
+
+def test_get_slack_user_info(slack_toolkit):
+ with patch(
+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"
+ ) as mock_login:
+ mock_client = MagicMock()
+ mock_login.return_value = mock_client
+ mock_client.users_info.return_value = {
+ "user": {"id": "123", "name": "test_user"}
+ }
+
+ response = slack_toolkit.get_slack_user_info("123")
+ expected_response = '{"user": {"id": "123", "name": "test_user"}}'
+ assert response == expected_response