1
votes

Im trying to get a list of all users on the slack with python. Using users.list gets me the first 1000 users. To get the rest I need to pass in a cursor value. The Cursor value works when I use slack's own tester on it's website.

The users.list method returns 1000 records and if I use the cursor variable( In the slack dev website to test url calls), each call gets me another thousand users.

This is the code that I'm using right now to get a list of users:

client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
request=client.api_call("users.list")

According to the slack documentation users.list has an optional argument called cursor to get the next 10000 users. But I cant figure out how to pass the cursor variable along with the users.list command. I've looked at stack overflow to see how others have used users.list but I cant get any instances of that.

2

2 Answers

3
votes

Let me try to help you out.

client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
request=client.api_call("users.list")
while True:
if request['response_metadata']:
   if 'next_cursor' in request['response_metadata']:
      request = client.api_call("users.list" , data={'cursor':request['response_metadata']['next_cursor']})
   else:
      break

I am sure about it because I recently did slack integration with odoo using python sdk. May be it require some changes

2
votes

https://github.com/slackapi/python-slackclient/blob/master/slack/web/base_client.py#L97

Create a request and execute the API call to Slack.
        Args:
            api_method (str): The target Slack API method.
                e.g. 'chat.postMessage'
            http_verb (str): HTTP Verb. e.g. 'POST'
            files (dict): Files to multipart upload.
                e.g. {imageORfile: file_objectORfile_path}
            data: The body to attach to the request. If a dictionary is
                provided, form-encoding will take place.
                e.g. {'key1': 'value1', 'key2': 'value2'}
            params (dict): The URL parameters to append to the URL.
                e.g. {'key1': 'value1', 'key2': 'value2'}
            json (dict): JSON for the body to attach to the request
                (if files or data is not specified).
                e.g. {'key1': 'value1', 'key2': 'value2'}

Since Accepted content types is application/x-www-form-urlencoded, we have to use 'data'

client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
request=client.api_call("users.list", data={'cursor': 'dXNlcjpVMDYxTkZUVDI='})