1.슬랙 > 워크스페이스 > 전체 사용자 목록 정보를 가져오는 방법입니다.
특정 채널이 아닌 워크스페이스 전체의 사용자 목록을 가져오는 코드 입니다.
python slack_sdk 패키지를 이용합니다. ( https://pypi.org/project/slack-sdk/ )
2.사용자 목록 정보는 users.list API 를 이용합니다.
https://api.slack.com/methods/users.list
users.list API method
Lists all users in a Slack team.
api.slack.com
호출시 limit 파라미터에 조회 할 계정 개수 지정 합니다.
설정을 하지 않으면 기본값은 0 입니다. (0 은 제한 없이 모두 가져온다 의미 )
# The 'limit' parameter should be adjusted to match your environment. Default 0 (unlimited) # The maximum number of user lists to return. Fewer than the requested number of items may be returned |
기본값이 0 이므로 슬랙에 사용자 계정이 많으면 응답 시간이 지연 될 수 있습니다.
코드 작성 후 테스트 시에는 10, 20 등으로 조정 하여 사용 하고 (10개, 20개만 가져온다 의미)
전체 사용자 목록을 주기적으로 수집 해야 하는 경우 0 으로 사용하길 권장합니다.
3.작성된 코드
bot_token 에 생성한 Bot 의 토큰을 입력하고 실행합니다.
id 가 'USLACKBOT' 는 워크스페이스 내에서 생성된 봇 계정입니다. 휴먼 계정이 아니므로 'USLACKBOT' 는 제외 합니다.
응답되는 정보는 다양한 정보가 포함되어 있습니다. 필요한 정보는 파싱 하여 사용합니다.
__author__ = 'https://github.com/password123456/'
__version__ = '1.0.2-230828'
import sys
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
class Bcolors:
Black = '\033[30m'
Red = '\033[31m'
Green = '\033[32m'
Yellow = '\033[33m'
Blue = '\033[34m'
Magenta = '\033[35m'
Cyan = '\033[36m'
White = '\033[37m'
Endc = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def get_all_users_in_workspace(token):
client = WebClient(token=token)
try:
# https://api.slack.com/methods/users.list
# The 'limit' parameter should be adjusted to match your environment. Default 0 (unlimited)
# The maximum number of user lists to return. Fewer than the requested number of items may be returned
result = client.users_list(limit=100)
i = 0
for info in result['members']:
# Exclude Bot Accounts
if not info['id'] == 'USLACKBOT':
if not info['is_bot']:
i = i + 1
member_id = info['id']
team_id = info['team_id']
display_name = info['profile']['display_name']
real_name = info['profile']['real_name']
phone = info['profile']['phone']
email = info['profile']['email']
if not member_id:
member_id = 'null'
elif not team_id:
team_id = 'null'
elif not display_name:
display_name = 'null'
elif not real_name:
real_name = 'null'
elif not phone:
phone = 'null'
elif not email:
email = 'null'
print(f'{i},{real_name},{display_name},{team_id},{email},{member_id},{phone}')
except SlackApiError as e:
print(f'{Bcolors.Yellow}- Api Error:: {e} {Bcolors.Endc}')
def main():
bot_token = 'slack_apps_oauth_token(bot_token)'
get_all_users_in_workspace(bot_token)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
sys.exit(0)
except Exception as e:
print(
f'{Bcolors.Yellow}- ::Exception:: Func:[{__name__.__name__}] '
f'Line:[{sys.exc_info()[-1].tb_lineno}] [{type(e).__name__}] {e}{Bcolors.Endc}')
끝
'개발' 카테고리의 다른 글
[slack-sdk] 특정 채널에 업로드된 업로드 파일 목록 가져오기 (0) | 2023.09.13 |
---|---|
[slack-sdk] 특정 채널의 전체 사용자 목록 가져오기 (0) | 2023.09.12 |
봇 응답 제한(봇이 동작 하는 채팅방 제한) 하기 (0) | 2023.06.23 |
[미드저니] 미드저니 쇼케이스 페이지의 이미지 다운로드 위치를 알아보자 (0) | 2023.06.17 |
[정보보안] 취약한 패스워드 해시 알고리즘을 사용 계정 찾기 (0) | 2023.06.13 |