【发布时间】:2017-05-24 15:49:06
【问题描述】:
给定一个松弛频道的名称,有没有办法检索该频道中所有成员的电子邮件列表?我尝试查看 slack api 文档,但找不到实现这一点所需的方法 (https://api.slack.com/methods)。
【问题讨论】:
标签: email channel slack-api slack
给定一个松弛频道的名称,有没有办法检索该频道中所有成员的电子邮件列表?我尝试查看 slack api 文档,但找不到实现这一点所需的方法 (https://api.slack.com/methods)。
【问题讨论】:
标签: email channel slack-api slack
如果您有必要的范围,您可以检索以频道名称开头的频道所有成员的电子邮件,如下所示:
请注意,这也适用于使用 groups.list 和 groups.info 的私人频道,但前提是与访问令牌相关的用户或机器人是该私人频道的成员。
强烈建议使用较新的对话。* 方法,而不是频道。* 和组。*,因为它们更灵活,并且在某些情况下旧方法不起作用(例如转换的频道)。
【讨论】:
这是一个使用最新 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
此外,要从私人频道或聊天中阅读,您需要将您的应用添加到工作区并为您感兴趣的每个频道添加“/invite appname”。
【讨论】:
注意:channels.list、channels.info、users.list 已弃用(将于 2020 年 11 月 25 日停用并停止运行)。
替换为conversations.list、conversations.members、users.info
您可以通过以下方式获取电子邮件:
conversations.list - 获取 Channel Id 列表(公共或私有)
conversations.members - 通过Channel Id获取Member Id的列表
users.info - 通过Member Id获取Email
【讨论】:
我刚刚制作了一个小型 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
【讨论】:
这是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'])
【讨论】:
使用slack-ruby-client 的Ruby 解决方案:
范围:
channels:readusers.profile:readusers:read.emailusers:readrequire '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
【讨论】: