Skip to content

Commit 1d6e271

Browse files
feat: Slack support get user info (#3125)
Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
1 parent 23eddb4 commit 1d6e271

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

camel/toolkits/slack_toolkit.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def send_slack_message(
237237
) -> str:
238238
r"""Send a message to a Slack channel. When use this function you must
239239
call `get_slack_channel_information` function first to get the
240-
`channel id`.
240+
`channel id`. If use user, you must use `get_slack_user_list`
241+
function first to get the user id.
241242
242243
Args:
243244
message (str): The message to send.
@@ -306,6 +307,52 @@ def delete_slack_message(
306307
except SlackApiError as e:
307308
return f"Error deleting message: {e.response['error']}"
308309

310+
def get_slack_user_list(self) -> str:
311+
r"""Retrieve a list of all users in the Slack workspace.
312+
313+
Returns:
314+
str: A JSON string representing a list of users. Each user
315+
object contains 'id', 'name'.
316+
"""
317+
from slack_sdk.errors import SlackApiError
318+
319+
try:
320+
slack_client = self._login_slack()
321+
response = slack_client.users_list()
322+
users = response["members"]
323+
filtered_users = [
324+
{
325+
"id": user["id"],
326+
"name": user["name"],
327+
}
328+
for user in users
329+
]
330+
331+
return json.dumps(filtered_users, ensure_ascii=False)
332+
except SlackApiError as e:
333+
return f"Error retrieving user list: {e.response['error']}"
334+
335+
def get_slack_user_info(self, user_id: str) -> str:
336+
r"""Retrieve information about a specific user in the Slack workspace.
337+
normally, you don't need to use this method, when you need to get a
338+
user's detailed information, use this method. Use `get_slack_user_list`
339+
function first to get the user id.
340+
341+
Args:
342+
user_id (str): The ID of the user to retrieve information about.
343+
344+
Returns:
345+
str: A JSON string representing the user's information.
346+
"""
347+
from slack_sdk.errors import SlackApiError
348+
349+
try:
350+
slack_client = self._login_slack()
351+
response = slack_client.users_info(user=user_id)
352+
return json.dumps(response, ensure_ascii=False)
353+
except SlackApiError as e:
354+
return f"Error retrieving user info: {e.response['error']}"
355+
309356
def get_tools(self) -> List[FunctionTool]:
310357
r"""Returns a list of FunctionTool objects representing the
311358
functions in the toolkit.
@@ -322,4 +369,6 @@ def get_tools(self) -> List[FunctionTool]:
322369
FunctionTool(self.get_slack_channel_message),
323370
FunctionTool(self.send_slack_message),
324371
FunctionTool(self.delete_slack_message),
372+
FunctionTool(self.get_slack_user_list),
373+
FunctionTool(self.get_slack_user_info),
325374
]

test/toolkits/test_slack_functions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,31 @@ def test_delete_slack_message(slack_toolkit):
133133
mock_client.chat_delete.return_value = {}
134134
response = slack_toolkit.delete_slack_message("123", "123")
135135
assert response == "{}"
136+
137+
138+
def test_get_slack_user_list(slack_toolkit):
139+
with patch(
140+
"camel.toolkits.slack_toolkit.SlackToolkit._login_slack"
141+
) as mock_login:
142+
mock_client = MagicMock()
143+
mock_login.return_value = mock_client
144+
mock_client.users_list.return_value = {
145+
"members": [{"id": "123", "name": "test_user"}]
146+
}
147+
response = slack_toolkit.get_slack_user_list()
148+
assert response == '[{"id": "123", "name": "test_user"}]'
149+
150+
151+
def test_get_slack_user_info(slack_toolkit):
152+
with patch(
153+
"camel.toolkits.slack_toolkit.SlackToolkit._login_slack"
154+
) as mock_login:
155+
mock_client = MagicMock()
156+
mock_login.return_value = mock_client
157+
mock_client.users_info.return_value = {
158+
"user": {"id": "123", "name": "test_user"}
159+
}
160+
161+
response = slack_toolkit.get_slack_user_info("123")
162+
expected_response = '{"user": {"id": "123", "name": "test_user"}}'
163+
assert response == expected_response

0 commit comments

Comments
 (0)