【问题标题】:Slack API: Retrieve all member emails from a slack channelSlack API:从 Slack 频道检索所有成员电子邮件
【发布时间】:2017-05-24 15:49:06
【问题描述】:

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

【问题讨论】:

    标签: email channel slack-api slack


    【解决方案1】:

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

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

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

    2019 年更新

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        替换为conversations.listconversations.membersusers.info


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

        conversations.list - 获取 Channel Id 列表(公共或私有)

        conversations.members - 通过Channel Id获取Member Id的列表

        users.info - 通过Member Id获取Email

        【讨论】:

          【解决方案4】:

          我刚刚制作了一个小型 Rub​​y 脚本,用于从松弛通道中检索所有成员并以 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
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              【解决方案6】:

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

              【讨论】:

                【解决方案7】:

                使用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
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2018-03-29
                  • 2015-05-03
                  • 2018-12-18
                  • 2019-07-11
                  • 2023-04-08
                  • 2019-08-31
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多