Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 482
user5844628 Avatar asked Jan 10 '17 08:01

user5844628


People also ask

How do I extract emails from Slack channel?

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

Can you export list of members from Slack channel?

Export a member list. From your desktop, click your workspace name in the top left. Select Settings & administration from the menu, then click Manage Members. Click Export Member List.

Can I download a Slack channel message history without admin permission?

Slack does allow you to export and save a message archive, but only if you are the channel owner or admin.


2 Answers

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).

like image 192
Erik Kalkoken Avatar answered Sep 20 '22 15:09

Erik Kalkoken


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.

like image 29
JumpZero Avatar answered Sep 22 '22 15:09

JumpZero