Slack API:从松弛通道中检索所有成员电子邮件

时间:2017-01-10 08:10:37

标签: email channel slack-api slack

鉴于松弛频道的名称,有没有办法检索该频道中所有成员的电子邮件列表?我试着查看松弛的api文档,但找不到实现这一点所需的方法(https://api.slack.com/methods)。

7 个答案:

答案 0 :(得分:5)

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

  1. 致电channels.list以获取所有频道的列表并将频道名称转换为其ID
  2. 使用频道ID调用所需频道的channels.info以获取其成员列表。
  3. 致电users.list以检索所有Slack用户的列表,包括他们的个人资料信息和电子邮件
  4. 使用用户ID匹配频道成员列表和用户列表,以获取正确的用户和电子邮件
  5. 请注意,这也适用于使用groups.listgroups.info的私人频道,但前提是与访问令牌相关的用户或机器人是该私有频道的成员。

答案 1 :(得分:3)

这是一个使用最新API与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

此外,要从私人频道或聊天中读取内容,您需要将应用添加到工作区,并为感兴趣的每个频道添加“ /邀请应用名称”。

答案 2 :(得分:2)

注意:int gymId = user.gym.gymID; user.gym = _context.gyms.Find(gymId); channels.listchannels.info已过时(将于2020年11月25日退休并停止运行)。

替换为users.listconversations.listconversations.members


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

users.info-获取 conversations.list 的列表(公共或私有)

Channel Id-通过conversations.members

获取 Member Id 的列表

Channel Id-通过users.info

获取 Email

答案 3 :(得分:0)

我刚刚创建了一个小的Ruby脚本,它从一个松弛的通道中检索所有成员并以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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

根据@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'])

答案 6 :(得分:0)

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

范围:

  • 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