slackdemo / get_users.py
svummidi's picture
POC for passive monitoring
a31ba66
raw
history blame contribute delete
No virus
1.31 kB
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__":
# Replace 'YOUR_SLACK_API_TOKEN' with your actual Slack API token
slack_token = os.environ.get('SLACK_API_TOKEN')
# Fetch the user ID to name mapping from Slack
user_id_to_name_mapping = get_user_id_to_name_mapping(slack_token)
# Save the mapping to a JSON file
save_mapping_to_json(user_id_to_name_mapping, 'user_id_to_name_mapping.json')
# Load the mapping from the JSON file (just to demonstrate loading)
loaded_mapping = load_mapping_from_json('user_id_to_name_mapping.json')
print(loaded_mapping)