Email Slack API:从Slack通道检索所有成员电子邮件
给定空闲频道的名称,是否有方法检索该频道中所有成员的电子邮件列表?我尝试查找slack api文档,但找不到实现此目的所需的方法()。如果您具有必要的作用域,您可以检索以频道名称开头的频道所有成员的电子邮件,如下所示:Email Slack API:从Slack通道检索所有成员电子邮件,email,channel,slack-api,slack,Email,Channel,Slack Api,Slack,给定空闲频道的名称,是否有方法检索该频道中所有成员的电子邮件列表?我尝试查找slack api文档,但找不到实现此目的所需的方法()。如果您具有必要的作用域,您可以检索以频道名称开头的频道所有成员的电子邮件,如下所示: 调用以获取所有频道的列表,并将频道名称转换为其ID 使用通道ID调用所需通道以获取其成员列表 调用以检索所有Slack用户的列表,包括其配置文件信息和电子邮件 按用户ID将频道成员列表与用户列表匹配,以获得正确的用户和电子邮件 请注意,这也适用于使用和的专用通道,但仅当与访问令牌
强烈建议使用更新的对话。*方法,而不是频道。*和组。*,因为它们更灵活,而且在某些情况下,旧方法无法工作(例如转换频道)。我刚刚编写了一个小的Ruby脚本,从松弛通道检索所有成员并以CSV格式返回的内容 脚本: 例如:
$ ./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'])
下面是一个使用最新API与Python2或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
此外,要阅读私人频道或聊天,您需要将应用程序添加到工作区,并为您感兴趣的每个频道“邀请appname”。注意:
频道。列表,频道。信息,,用户。列表
已弃用(2020年11月25日退休并停止运行)
替换为对话。列表
,对话。成员
,用户。信息
您可以通过以下方式获取电子邮件:
conversations.list
-获取频道Id的列表(公共或私人)
对话。成员
-通过频道Id
users.info
-通过会员Id