Created
June 29, 2022 09:17
-
-
Save kd0g/375792d4a46fb68853e5ba9d05c9a732 to your computer and use it in GitHub Desktop.
List up Slack Users into Vcard Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import os | |
from slack_sdk import WebClient | |
import vobject | |
import requests | |
import base64 | |
TMP_PATH = '/tmp/people.pkl' | |
def get_users_list(client): | |
cursor = None | |
i = 0 | |
records = [] | |
while True: | |
if cursor: | |
rs = client.users_list(cursor=cursor, limit=1000) | |
else: | |
rs = client.users_list(limit=1000) | |
cursor = rs["response_metadata"]["next_cursor"] | |
for u in rs["members"]: | |
i += 1 | |
# print(u) | |
if u['deleted'] or u['is_bot'] or not u['profile']['phone']: continue | |
print(i, u['id'], u['is_bot'], u['profile']['real_name'], u['profile']['phone'], u['profile']['image_512']) | |
record = [u['id'], u['profile']['real_name'], u['profile']['phone'], u['profile']['image_512']] | |
records.append(record) | |
if not cursor: | |
break | |
return records | |
def make_vcard_obj(pinfo): | |
def b64_image(rcon): | |
b64 = base64.b64encode(rcon) | |
return b64.decode('utf-8') | |
_, real_name, phone, img = pinfo | |
if '/' in real_name: | |
real_name = real_name.replace('/', '_') | |
if phone.startswith('010'): | |
phone = phone.replace('010', '+82 10', 1) | |
j = vobject.vCard() | |
o = j.add('fn') | |
o.value = real_name | |
o = j.add('tel') | |
o.type_param = "cell" | |
o.value = phone | |
o = j.add('PHOTO;ENCODING=b;TYPE=image/jpeg') | |
resp = requests.get(img) | |
o.value = b64_image(resp.content) | |
if not os.path.exists('people'): | |
os.makedirs('people') | |
open('people/%s.vcf' % real_name, 'w').write(j.serialize()) | |
def run(): | |
slack_token = os.environ["SLACK_BOT_TOKEN"] | |
client = WebClient(token=slack_token) | |
if 1: | |
rs = get_users_list(client=client) | |
open(TMP_PATH, 'wb').write(pickle.dumps(rs)) | |
else: | |
rs = pickle.loads(open(TMP_PATH, 'rb').read()) | |
for i, pinfo in enumerate(rs): | |
print("%s/%s" % (i, len(rs))) | |
make_vcard_obj(pinfo) | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
슬랙봇에 users.profile:read], users:read 권한 필요. xoxb-로 시작하는 oauth token을 환경변수로 넣어줘야 함.