1

I am trying to make a Slack Bot using python, and I have a issue, I am not able to get the users from a specific channel, I only succeed if I take all of the users. Basically I want only those from (eg. random channel). Until now I tried to get the team's ID from every user and compare it to a channel ID, but that failed because everyone is having the same ID and I can't figure out why. Here is the snippet of the code:

def users_of_the_channel():
    global slack_client
    #this is the variable which is initialized with SlackClient(BOT_TOKEN)
    api_call = slack_client.api_call( "users.list",channel="C0XXXXXXX") 
    if api_call.get('ok'):
        channels = api_call.get('members')
        for channel in channels:
            print ("this is cool : ", channel['team_id'])

The issue I believe is that when I initialize the api_call variable I call the function with the users.list argument, I tried with usergroups.list and usergroups.users.list but with no success. Basically to keep it short I need the list with the users from a channel, and the documentation hasn't helped me.

JohnnyOnPc
  • 386
  • 1
  • 5
  • 18

2 Answers2

3

users.list does not take channel id as input, which you are providing in your API call.

Slack provide different bot token for different teams. Simply call api with bot token of team of which you require user list and you will get the list of members.

client = SlackClient(SLACK_BOT_TOKEN)
request = client.api_call("users.list")
if request['ok']:
    for item in request['members']:
        print item['name']

For more details about optional arguments you can provide with api call, refer documentation(https://api.slack.com/methods/users.list).

0

You probably can use the new conversations API to retrieve that (specifically conversations.members endpoint): https://api.slack.com/methods/conversations.members

amit_saxena
  • 7,450
  • 5
  • 49
  • 64