Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Make users.list accept pagination args #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions examples/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@
# https://api.slack.com/methods

import os
import functools
from slacker import Slacker


def paginated(list_func):
# See https://api.slack.com/docs/pagination
next_cursor = None
while True:
response = list_func(cursor=next_cursor)
yield response
next_cursor = response.body['response_metadata']['next_cursor']
if not next_cursor:
break


def list_slack():
"""List channels & users in slack."""
try:
token = os.environ['SLACK_TOKEN']
slack = Slacker(token)

# Get channel list
response = slack.channels.list()
channels = response.body['channels']
for channel in channels:
print(channel['id'], channel['name'])
# if not channel['is_archived']:
# slack.channels.join(channel['name'])
print()

# Get users list
response = slack.users.list()
users = response.body['members']
for user in users:
if not user['deleted']:
print(user['id'], user['name'], user['is_admin'], user[
'is_owner'])
# Get users list, with pagination
for response in paginated(functools.partial(slack.users.list, limit=150)):
print("=== page === ")
users = response.body['members']
for user in users:
if not user['deleted']:
print(user['id'], user['name'], user['is_admin'], user[
'is_owner'])
print()
except KeyError as ex:
print('Environment variable %s not set.' % str(ex))
Expand Down
11 changes: 9 additions & 2 deletions slacker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,15 @@ def info(self, user, include_locale=False):
return self.get('users.info',
params={'user': user, 'include_locale': include_locale})

def list(self, presence=False):
return self.get('users.list', params={'presence': int(presence)})
def list(self, presence=False, cursor=None, limit=None):
return self.get(
'users.list',
params={
'presence': int(presence),
'cursor': cursor,
'limit': limit,
}
)

def identity(self):
return self.get('users.identity')
Expand Down