user5844628
user5844628

Reputation: 419

Slack API: Retrieve all member emails from a slack channel

Given the name of a slack channel, is there a way to retrieve a list of emails of all the members in that channel? I tried looking in the slack api docs but couldn't find the method I need to make this happen (https://api.slack.com/methods).

Upvotes: 20

Views: 33792

Answers (10)

apple_mango
apple_mango

Reputation: 1

with python3 and package 'slackclient' HERE

pip3 install slackclient

def get_channel_emails(channel_id: str):
    slack_api_bot_token = 'YOUR_BOT_TOKEN'
    
    ## Require BOT permission ##
    
    # channels:read
    # groups:read
    # im:read
    # mpim:read
    # users:read

    client = slack.WebClient(token=slack_api_bot_token)
    result = client.conversations_members(channel=channel_id)
    i = 0
    for user in result['members']:
        #print(user)
        info = client.users_info(user=user).data
        i = i + 1
        #print(info)

        member_id = info['user']['id']
        team_id = info['user']['team_id']
        display_name = info['user']['name']
        real_name = info['user']['real_name']
        phone = info['user']['profile']['phone']
        email = info['user']['profile']['email']

        if not member_id:
            member_id = 'null'
        elif not team_id:
            team_id = 'null'
        elif not display_name:
            display_name = 'null'
        elif not real_name:
            real_name = 'null'
        elif not phone:
            phone = 'null'
        elif not email:
            email = 'null'

        print(f'{i},{real_name},{display_name},{team_id},{member_id},{email},{phone}')



def main():
    #channel id: https://app.slack.com/huddle/TB37ZG064/CB3CF4A7B
    #if end of URL string starts with "C", it means CHANNEL

    get_channel_emails('CB3CF4A7B')

Upvotes: 0

Amay Kulkarni
Amay Kulkarni

Reputation: 838

If without coding you require to get emails of all users from Slack channel:

Go to Channel settings, there is a option for "Copy member email address".

With Slack API:

conversations.list - Get the list of Channel Id (public or private)

conversations.members - Get the list of Member Id by Channel Id

users.info - Get the Email by Member Id

Upvotes: 0

Brian Whitney
Brian Whitney

Reputation: 11

I'm not sure if these are all outdated but I couldn't get any of them to work. The best way I found to do it was to use the client.conversations_members method to find all user IDs and then get emails for those users.

import slack

def get_channel_emails(channel_id:str)-> list:
    client = slack.WebClient(token=os.getenv("SLACK_TOKEN"))
    result = client.conversations_members(channel= channel_id)
    emails = []
    for user in result['members']:
        info = client.users_info(user = user).data
        if 'email' in info['user']['profile'].keys():
            emails.append(info['user']['profile']['email'])
    return emails

Some notable roadblocks are:

  1. The slack package is actually slackclient so use pip install slackclient instead

  2. The channel_id is not the channel name but the code slack gives to the channel. It can be found in the web browser version path and is formatted CXXXXXXXXXX.

Upvotes: 1

Jeremy
Jeremy

Reputation: 311

Ruby solution using slack-ruby-client:

Scopes:

  • channels:read
  • users.profile:read
  • users:read.email
  • users:read
require 'slack-ruby-client'

Slack.configure do |config|
  config.token = ENV['SLACK_TOKEN_IN_BASH_PROFILE']
end

client = Slack::Web::Client.new
CH = '#channel-name'

client.conversations_members(channel: CH).members.each do |user|
  puts client.users_profile_get(user: user).profile.email
end

Upvotes: 0

sky91
sky91

Reputation: 3170

Note: channels.list, channels.info, users.list are deprecated (retire and cease functioning on November 25, 2020).

Replace to conversations.list, conversations.members, users.info


You can get the email like this way:

conversations.list - Get the list of Channel Id (public or private)

conversations.members - Get the list of Member Id by Channel Id

users.info - Get the Email by Member Id

Upvotes: 4

JumpZero
JumpZero

Reputation: 71

Here's a version that works with Python 2 or 3 using up-to-date APIs.

import os
import requests

SLACK_API_TOKEN='xoxb-TOKENID' # Your token here
CHANNEL_NAME='general' # Your channel here

channel_list = requests.get('https://slack.com/api/conversations.list?token=%s&types=%s' % (SLACK_API_TOKEN, 'public_channel,private_channel,im,mpim')).json()['channels']
for c in channel_list:
    if 'name' in c and c['name'] == CHANNEL_NAME:
        channel = c

members = requests.get('https://slack.com/api/conversations.members?token=%s&channel=%s' % (SLACK_API_TOKEN, channel['id'])).json()['members']

users_list = requests.get('https://slack.com/api/users.list?token=%s' % SLACK_API_TOKEN).json()['members']

for user in users_list:
    if "email" in user['profile'] and user['id'] in members:
        print(user['profile']['email'])

Note that you'll need to create a Slack App with an OAuth API token and the following scopes authorized for this to work for all of the various types of conversations:

channels:read
groups:read
im:read
mpim:read
users:read
users:read.email

Also, to read from private channels or chats, you'll need to add your app to the Workspace and "/invite appname" for each channel you're interested in.

Upvotes: 7

Erik Kalkoken
Erik Kalkoken

Reputation: 32737

Provided you have the necessary scopes you can retrieved the emails of all members of a channel starting with the channel name as follows:

  1. Call channels.list to get the list of all channels and to convert the channel name to its ID
  2. Call channels.info of the desired channel with channel ID to get the list of its members.
  3. Call users.list to retrieve the list of all Slack users including their profile information and email
  4. Match the channel member list with the user list by user ID to get the correct users and emails

Note that this also works for private channels using groups.list and groups.info, but only if the user or bot related to the access token is a member of that private channel.

Update 2019

Would strongly recommend to rather use the newer conversations.* methods, instead of channels.* and groups.*, because they are more flexible and they are some cases where the older methods will not work (e.g. converted channels).

Upvotes: 20

Costa Huang
Costa Huang

Reputation: 1476

Based on the answer by @Lam, I modified it to work with python3.

import requests

SLACK_API_TOKEN = "" # get one from https://api.slack.com/docs/oauth-test-tokens
CHANNEL_NAME = ""

# channel_list = requests.get('https://slack.com/api/channels.list?token=%s' % SLACK_API_TOKEN).json()['channels']
# channel = filter(lambda c: c['name'] == CHANNEL_NAME, channel_list)[0]

# channel_info = requests.get('https://slack.com/api/channels.info?token=%s&channel=%s' % (SLACK_API_TOKEN, channel['id'])).json()['channel']
# members = channel_info['members']

channel_list = requests.get('https://slack.com/api/groups.list?token=%s' % SLACK_API_TOKEN).json()['groups']
for c in channel_list:
    if c['name'] == CHANNEL_NAME:
        channel = c

channel_info = requests.get('https://slack.com/api/groups.info?token=%s&channel=%s' % (SLACK_API_TOKEN, channel['id'])).json()['group']
print(channel_info)
members = channel_info['members']

users_list = requests.get('https://slack.com/api/users.list?token=%s' % SLACK_API_TOKEN).json()['members']

for user in users_list:
    if "email" in user['profile']:
        print(user['profile']['email'])

Upvotes: 0

Lam
Lam

Reputation: 436

Here's the python code:

import requests

SLACK_API_TOKEN = "" # get one from https://api.slack.com/docs/oauth-test-tokens
CHANNEL_NAME = ""

# channel_list = requests.get('https://slack.com/api/channels.list?token=%s' % SLACK_API_TOKEN).json()['channels']
# channel = filter(lambda c: c['name'] == CHANNEL_NAME, channel_list)[0]

# channel_info = requests.get('https://slack.com/api/channels.info?token=%s&channel=%s' % (SLACK_API_TOKEN, channel['id'])).json()['channel']
# members = channel_info['members']

channel_list = requests.get('https://slack.com/api/groups.list?token=%s' % SLACK_API_TOKEN).json()['groups']
channel = filter(lambda c: c['name'] == CHANNEL_NAME, channel_list)[0]

channel_info = requests.get('https://slack.com/api/groups.info?token=%s&channel=%s' % (SLACK_API_TOKEN, channel['id'])).json()['group']
print channel_info
members = channel_info['members']

users_list = requests.get('https://slack.com/api/users.list?token=%s' % SLACK_API_TOKEN).json()['members']
users = filter(lambda u: u['id'] in members, users_list)

for user in users:
    first_name, last_name = '', ''

    if user['real_name']:
        first_name = user['real_name']

        if ' ' in user['real_name']:
            first_name, last_name = user['real_name'].split()
    # print "%s,%s,%s" % (first_name, last_name, user['profile']['email'])
    print "%s" % (user['profile']['email'])

Upvotes: 3

oliver nadj
oliver nadj

Reputation: 858

I just made a small Ruby script, what retrieves all members from a slack channel and returns it in CSV format.

Script: https://github.com/olivernadj/toolbox/tree/master/slack-members

Example:

$ ./membersof.rb -t xoxp-123456789A-BCDEF01234-56789ABCDE-F012345678 -g QWERTYUIO
first_name,last_name,email
John,Doe,[email protected]
Jane,Doe,[email protected]

Upvotes: 2

Related Questions