slack_list_channels.py 805 B

123456789101112131415161718192021222324252627
  1. import os
  2. import pickle
  3. from slack_sdk import WebClient
  4. from slack_sdk.errors import SlackApiError
  5. def list_channels():
  6. # channel is a string start with @
  7. # Load Slack credentials
  8. credentials_path = './credentials/slack/token.pickle'
  9. if os.path.exists(credentials_path):
  10. with open(credentials_path, 'rb') as token_file:
  11. slack_token = pickle.load(token_file)
  12. else:
  13. raise FileNotFoundError("Slack token file not found.")
  14. client = WebClient(token=slack_token)
  15. try:
  16. # Call the conversations.list method using the WebClient
  17. result = client.conversations_list()
  18. print(result["channels"])
  19. except SlackApiError as e:
  20. # Print the error
  21. print(f"Error sending message: {e.response['error']}")
  22. raise