Slack API:从 Slack 频道中检索所有成员的电子邮件

Slack API: Retrieve all member emails from a slack channel

给定一个 Slack 频道的名称,有没有办法检索该频道中所有成员的电子邮件列表?我尝试查看 slack api 文档,但找不到实现此目的所需的方法 (https://api.slack.com/methods)。

如果您有必要的范围,您可以检索以频道名称开头的频道的所有成员的电子邮件,如下所示:

  1. 调用channels.list获取所有频道的列表并将频道名称转换为其ID
  2. 使用频道 ID 调用所需频道的 channels.info 以获取其成员列表。
  3. 调用 users.list 检索所有 Slack 用户的列表,包括他们的个人资料信息和电子邮件
  4. 通过用户ID将频道成员列表与用户列表匹配以获得正确的用户和电子邮件

请注意,这也适用于使用 groups.list and groups.info 的私人频道,但前提是与访问令牌相关的用户或机器人是该私人频道的成员。

2019 年更新

强烈建议使用较新的 conversations.* 方法,而不是 channels.* 和 groups.*,因为它们更灵活,并且在某些情况下旧方法不起作用(例如转换的频道) .

我刚刚制作了一个小 Ruby 脚本,用于从松弛通道检索所有成员并 returns 以 CSV 格式检索它。

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

示例:

$ ./membersof.rb -t xoxp-123456789A-BCDEF01234-56789ABCDE-F012345678 -g QWERTYUIO
first_name,last_name,email
John,Doe,john.doe@example.com
Jane,Doe,jane.doe@example.com

这是 python 代码:

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'])

根据@Lam 的回答,我修改了它以使用 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'])

这是使用最新的 APIs 与 Python 2 或 3 一起使用的版本。

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'])

请注意,您需要使用 OAuth API 令牌创建一个 Slack 应用程序,并授权以下范围用于所有各种类型的对话:

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

此外,要从私人频道或聊天中阅读,您需要将您的应用程序添加到工作区,并为您感兴趣的每个频道添加“/invite appname”。

注意:channels.listchannels.infousers.list 已弃用(将于 2020 年 11 月 25 日停用并停止运行)。

替换为conversations.listconversations.membersusers.info


您可以通过以下方式获取电子邮件:

conversations.list - 获取 Channel Id 的列表(public 或私有)

conversations.members - 通过 Channel Id

获取 Member Id 的列表

users.info - 通过 Member Id

获取 Email

Ruby 解决方案使用 slack-ruby-client:

范围:

  • 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

我不确定这些是否都已过时,但我无法让它们中的任何一个发挥作用。我发现最好的方法是使用 client.conversations_members 方法查找所有用户 ID,然后获取这些用户的电子邮件。

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

一些值得注意的障碍是:

  1. slack 包实际上是 slackclient 所以用 pip install slackclient 代替

  2. channel_id 不是频道名称,而是代码 slack 给频道的。它可以在网络浏览器版本路径中找到,格式为 CXXXXXXXXXX.