给定一个空闲通道的名称,是否有方法检索该通道中所有成员的电子邮件列表?我试着查看slack api文档,但找不到实现这一点所需的方法(https://api.slack.com/methods)。
如果你有必要的作用域,你可以检索以频道名称开头的频道所有成员的电子邮件,如下所示:
- 调用channels.list获取所有频道的列表并将频道名称转换为其ID
- 使用频道ID调用所需频道的channels.info以获取其成员列表
- 致电users.list检索所有Slack用户的列表,包括他们的个人资料信息和电子邮件
- 通过用户ID将渠道成员列表与用户列表进行匹配,以获得正确的用户和电子邮件
请注意,这也适用于使用groups.list和groups.info的专用通道,但前提是与访问令牌相关的用户或机器人程序是该专用通道的成员。
更新2019
强烈建议使用较新的对话.*方法,而不是频道.*和组.*,因为它们更灵活,而且在某些情况下,旧方法不起作用(例如转换的频道)。
这里有一个使用最新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
此外,要从私人频道或聊天中阅读,您需要将您的应用程序添加到工作区,并为您感兴趣的每个频道添加"/init-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
以下是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'])
我刚刚制作了一个小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
根据@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客户端的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
我不确定这些是否都过时了,但我无法让其中任何一个工作。我发现最好的方法是使用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
一些值得注意的障碍是:
slack
包实际上是slackclient
,因此使用pip install slackclient
而不是channel_id不是通道名称,而是给通道的代码松弛。它可以在web浏览器版本路径中找到,格式为
CXXXXXXXXXX
。
如果没有编码,您需要从Slack频道获取所有用户的电子邮件:
转到频道设置;复制成员电子邮件地址";。
使用Slack API:
对话列表-获取通道Id(公共或私人)列表
对话。成员-通过通道Id 获取成员Id列表
users.info-按会员Id 获取电子邮件
使用python3和包"slackclient"这里
pip3安装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')