Search code examples
pythonslackslack-api

How to use arguments in Slack API with python?


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.


Solution

  • 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