|
import os |
|
import json |
|
from slack_sdk import WebClient |
|
from slack_sdk.errors import SlackApiError |
|
|
|
def get_user_id_to_name_mapping(token): |
|
client = WebClient(token=token) |
|
try: |
|
response = client.users_list() |
|
users = response['members'] |
|
user_id_to_name = {user['id']: user['name'] for user in users} |
|
return user_id_to_name |
|
except SlackApiError as e: |
|
print(f"Error fetching user list: {e}") |
|
return {} |
|
|
|
def save_mapping_to_json(mapping, filepath): |
|
with open(filepath, 'w') as file: |
|
json.dump(mapping, file) |
|
|
|
def load_mapping_from_json(filepath): |
|
if not os.path.exists(filepath): |
|
return {} |
|
with open(filepath, 'r') as file: |
|
return json.load(file) |
|
|
|
if __name__ == "__main__": |
|
|
|
slack_token = os.environ.get('SLACK_API_TOKEN') |
|
|
|
|
|
user_id_to_name_mapping = get_user_id_to_name_mapping(slack_token) |
|
|
|
|
|
save_mapping_to_json(user_id_to_name_mapping, 'user_id_to_name_mapping.json') |
|
|
|
|
|
loaded_mapping = load_mapping_from_json('user_id_to_name_mapping.json') |
|
print(loaded_mapping) |
|
|