From c7eaa8703bfb90554dfe119cb49afe6d8546975f Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Tue, 14 Aug 2018 19:15:23 +0900 Subject: [PATCH 1/8] ISSHA-1329 add members fanction add all bot help option mention name from real_name or display_name --- pyconjpbot/plugins/misc.py | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) mode change 100644 => 100755 pyconjpbot/plugins/misc.py diff --git a/pyconjpbot/plugins/misc.py b/pyconjpbot/plugins/misc.py old mode 100644 new mode 100755 index 0d48b85..b613009 --- a/pyconjpbot/plugins/misc.py +++ b/pyconjpbot/plugins/misc.py @@ -139,3 +139,67 @@ def cal_help(message): - `$cal 9`: 今年の指定された月のカレンダーを返す - `$cal 9 2016`: 指定された年月のカレンダーを返す ''') + + +@respond_to('^members$') +@respond_to('^members\s+(all|bot|help)$') +def members_command(message, subcommand=None): + """ + チャンネル参加者のメンション名の一覧を返す + + - https://github.com/os/slacker + - https://api.slack.com/methods/channels.info + - https://api.slack.com/methods/users.getPresence + - https://api.slack.com/methods/users.info + """ + + if subcommand == 'help': + botsend(message, '''- `$members`: チャンネルにいる通常の参加者のメンション名の一覧を返す +- `$members all`:チャンネルにいる全ての参加者のメンション名の一覧を返す +- `$members bot`:チャンネルにいるbotの参加者メンション名の一覧を返す +''') + return + + if subcommand == 'all': + desc = '全ての' + elif subcommand == 'bot': + desc = 'botの' + else: + desc = '通常の' + + # チャンネルのメンバー一覧を取得 + channel = message.body['channel'] + webapi = slacker.Slacker(settings.API_TOKEN) + cinfo = webapi.channels.info(channel) + members = cinfo.body['channel']['members'] + + # 作業用リスト初期化 + nameall = [] + + # メンバ一覧から順次処理 + for member_id in members: + user_info = webapi.users.info(member_id) + # real_nameまたはdisplay_nameにメンション用文字列が入っている推測 + basename = user_info.body['user']['profile']['real_name'] + display_name = user_info.body['user']['profile']['display_name'] + + # display_nameが設定されていればそれが優先されている推測 + if display_name != "": + basename = display_name + + if subcommand == 'all': + nameall.append(basename) + elif subcommand == 'bot': + if user_info.body['user']['is_bot']: + nameall.append(basename) + else: + # サブコマンドでなにも指定されなければ通常(botでない)ユーザのみ + if not user_info.body['user']['is_bot']: + nameall.append(basename) + + # 探しやすいように大小文字区別なしアルファベット順 + nameall.sort(key=str.lower) + + # 処理概要、一覧、Countを出力 + botsend(message, 'このチャンネルの{0}参加者一覧は\n{1}\n{2}参加者です。'.format( + desc, '\n'.join(nameall), len(nameall))) From f2c21440a236247dc4bc8d0e72401133d9130411 Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Sat, 2 Feb 2019 04:51:30 +0000 Subject: [PATCH 2/8] ISSHA-1329 ommit warning new command undefined messages --- pyconjpbot/plugins/term.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 pyconjpbot/plugins/term.py diff --git a/pyconjpbot/plugins/term.py b/pyconjpbot/plugins/term.py old mode 100644 new mode 100755 index 2bdd557..5486ea5 --- a/pyconjpbot/plugins/term.py +++ b/pyconjpbot/plugins/term.py @@ -12,7 +12,7 @@ 'translate', '翻訳', 'weather', '天気', 'term', - 'shuffle', 'help', 'choice', 'ping', 'version', 'random', 'cal', + 'shuffle', 'help', 'choice', 'ping', 'version', 'random', 'cal', 'members', 'google', 'image', 'map', 'gadmin', 'github', 'suddendeath', From 75e854447af33f16ac0d0ad31510dcb6659e3838 Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Sat, 2 Feb 2019 04:52:25 +0000 Subject: [PATCH 3/8] ISSHA-1329 update README --- README.md | 3 +++ 1 file changed, 3 insertions(+) mode change 100644 => 100755 README.md diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 50dc9d8..c0422e1 --- a/README.md +++ b/README.md @@ -235,6 +235,9 @@ BOT: @takanory おはようございます - `$cal`: 今月のカレンダーを返す - `$cal 9`: 今年の指定された月のカレンダーを返す - `$cal 9 2016`: 指定された年月のカレンダーを返す +- `$members`: チャンネルにいる通常の参加者のメンション名の一覧を返す +- `$members bot`: チャンネルにいるbotの参加者のメンション名の一覧を返す +- `$members all`: チャンネルにいる全ての参加者のメンション名の一覧を返す - [misc.py](https://github.com/pyconjp/pyconjpbot/blob/master/pyconjpbot/plugins/misc.py) ## How to build From 5d1a4e56400f89c360f983ffe5f75ae9b62ba2bf Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Sat, 2 Feb 2019 04:57:24 +0000 Subject: [PATCH 4/8] ISSHA-1329 update ChangeLog --- ChangeLog.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index db90286..7a4cd62 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,10 @@ Unreleased ---------- +Release Notes - 2019-02-02 +-------------------------- +- [ISSHA-1329] add members command + Release Notes - 2019-01-20 -------------------------- - [ISSHA-967] update lgtm command for top and bottom text From 6ce3684a2b5d6b66f617cfc6f35f1fbe6164f41e Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Sat, 2 Feb 2019 06:28:34 +0000 Subject: [PATCH 5/8] Remove member commands from misc.py --- pyconjpbot/plugins/misc.py | 65 -------------------------------------- 1 file changed, 65 deletions(-) diff --git a/pyconjpbot/plugins/misc.py b/pyconjpbot/plugins/misc.py index b613009..54988fa 100755 --- a/pyconjpbot/plugins/misc.py +++ b/pyconjpbot/plugins/misc.py @@ -9,7 +9,6 @@ from ..botmessage import botsend, botreply, botwebapi - @respond_to('^help$') def help(message): """ @@ -139,67 +138,3 @@ def cal_help(message): - `$cal 9`: 今年の指定された月のカレンダーを返す - `$cal 9 2016`: 指定された年月のカレンダーを返す ''') - - -@respond_to('^members$') -@respond_to('^members\s+(all|bot|help)$') -def members_command(message, subcommand=None): - """ - チャンネル参加者のメンション名の一覧を返す - - - https://github.com/os/slacker - - https://api.slack.com/methods/channels.info - - https://api.slack.com/methods/users.getPresence - - https://api.slack.com/methods/users.info - """ - - if subcommand == 'help': - botsend(message, '''- `$members`: チャンネルにいる通常の参加者のメンション名の一覧を返す -- `$members all`:チャンネルにいる全ての参加者のメンション名の一覧を返す -- `$members bot`:チャンネルにいるbotの参加者メンション名の一覧を返す -''') - return - - if subcommand == 'all': - desc = '全ての' - elif subcommand == 'bot': - desc = 'botの' - else: - desc = '通常の' - - # チャンネルのメンバー一覧を取得 - channel = message.body['channel'] - webapi = slacker.Slacker(settings.API_TOKEN) - cinfo = webapi.channels.info(channel) - members = cinfo.body['channel']['members'] - - # 作業用リスト初期化 - nameall = [] - - # メンバ一覧から順次処理 - for member_id in members: - user_info = webapi.users.info(member_id) - # real_nameまたはdisplay_nameにメンション用文字列が入っている推測 - basename = user_info.body['user']['profile']['real_name'] - display_name = user_info.body['user']['profile']['display_name'] - - # display_nameが設定されていればそれが優先されている推測 - if display_name != "": - basename = display_name - - if subcommand == 'all': - nameall.append(basename) - elif subcommand == 'bot': - if user_info.body['user']['is_bot']: - nameall.append(basename) - else: - # サブコマンドでなにも指定されなければ通常(botでない)ユーザのみ - if not user_info.body['user']['is_bot']: - nameall.append(basename) - - # 探しやすいように大小文字区別なしアルファベット順 - nameall.sort(key=str.lower) - - # 処理概要、一覧、Countを出力 - botsend(message, 'このチャンネルの{0}参加者一覧は\n{1}\n{2}参加者です。'.format( - desc, '\n'.join(nameall), len(nameall))) From 86a6ecf7687eb100824b49ac14611be3d94f9b1a Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Sat, 14 Dec 2019 08:31:28 +0000 Subject: [PATCH 6/8] Added member command to member.py --- pyconjpbot/plugins/members.py | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 pyconjpbot/plugins/members.py diff --git a/pyconjpbot/plugins/members.py b/pyconjpbot/plugins/members.py new file mode 100755 index 0000000..82ba787 --- /dev/null +++ b/pyconjpbot/plugins/members.py @@ -0,0 +1,95 @@ +from slackbot.bot import respond_to +from slackbot import settings +import slacker +import json + +from ..botmessage import botsend, botreply, botwebapi + + +@respond_to('^members$') +@respond_to(r'^members\s+(all|bot|help)$') +def members_command(message, subcommand=None): + """ + チャンネル参加者のメンション名の一覧を返す + + - https://github.com/os/slacker + - https://api.slack.com/methods/channels.info + - https://api.slack.com/methods/users.getPresence + - https://api.slack.com/methods/users.info + """ + + if subcommand == 'help': + botsend(message, '''- `$members`: チャンネルにいる通常の参加者のメンション名の一覧を返す +- `$members all`:チャンネルにいる全ての参加者のメンション名の一覧を返す +- `$members bot`:チャンネルにいるbotの参加者メンション名の一覧を返す +''') + return + + if subcommand == 'all': + desc = '全ての' + elif subcommand == 'bot': + desc = 'botの' + else: + desc = '通常の' + + # チャンネルのメンバー一覧を取得 + channel = message.body['channel'] + webapi = slacker.Slacker(settings.API_TOKEN) + cinfo = webapi.channels.info(channel) + members = cinfo.body['channel']['members'] + + # 全メンバーを取得 + all_user_info = webapi.users.list() + + # 作業用リスト初期化 + member_list = [] + + # 警告文字列初期化 + warn_str = "" + + # このチャンネルのメンバーを順次処理 + for member_id in members: + # 全ユーザー情報リストから該当するユーザで抽出 + memberkeys = [x for x in all_user_info.body['members'] + if x['id'] == member_id] + # 全ユーザ情報リストに該当無いケースは無視 + if memberkeys == []: + continue + + # idが複数ヒットした場合警告を出す + if len(memberkeys) > 1: + warn_str += "\nidが複数ヒットしたので注意\n" + warn_str += json.dumps(memberkeys) + + if subcommand == 'all': + # allは全て通す対象 + pass + elif subcommand == 'bot' and not memberkeys[0]['is_bot']: + # bot指定時通常ユーザはskip + continue + elif subcommand is None and memberkeys[0]['is_bot']: + # 通常時はbotをskip + continue + + # real_nameまたはdisplay_nameにメンション用文字列が入っている推測 + basename = memberkeys[0]['profile']['real_name'] + display_name = memberkeys[0]['profile']['display_name'] + + # display_nameが設定されていればそれを優先 + if display_name != "": + basename = display_name + + member_list.append(basename) + + # 探しやすいように大小文字区別なしアルファベット順 + member_list.sort(key=str.lower) + + pretext = 'このチャンネルの{0}参加者は以下{1}名です。'.format(desc, len(member_list)) + maintext = '{0}\n{1}'.format('\n'.join(member_list), warn_str) + + attachments = [{ + 'pretext': pretext, + 'text': maintext, + 'color': '#59afe1'}] + + message.reply_webapi('', json.dumps(attachments), in_thread=True) From 8f2a9f448017521a034b83a752e83b44b6a3852e Mon Sep 17 00:00:00 2001 From: Yoshida Shunsuke Date: Sat, 13 Feb 2021 08:43:37 +0000 Subject: [PATCH 7/8] fix misc.py to old version --- pyconjpbot/plugins/misc.py | 1 + 1 file changed, 1 insertion(+) mode change 100755 => 100644 pyconjpbot/plugins/misc.py diff --git a/pyconjpbot/plugins/misc.py b/pyconjpbot/plugins/misc.py old mode 100755 new mode 100644 index 54988fa..0d48b85 --- a/pyconjpbot/plugins/misc.py +++ b/pyconjpbot/plugins/misc.py @@ -9,6 +9,7 @@ from ..botmessage import botsend, botreply, botwebapi + @respond_to('^help$') def help(message): """ From 88ac45b84f0ce8fb6fd590bc32d3ac1ca64f55c2 Mon Sep 17 00:00:00 2001 From: Takanori Suzuki Date: Sun, 28 Feb 2021 18:25:28 +0900 Subject: [PATCH 8/8] ISSHA-1329 change deprecated API --- pyconjpbot/plugins/members.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyconjpbot/plugins/members.py b/pyconjpbot/plugins/members.py index 82ba787..4bcadc1 100755 --- a/pyconjpbot/plugins/members.py +++ b/pyconjpbot/plugins/members.py @@ -13,7 +13,8 @@ def members_command(message, subcommand=None): チャンネル参加者のメンション名の一覧を返す - https://github.com/os/slacker - - https://api.slack.com/methods/channels.info + - https://api.slack.com/methods/conversations.members + - https://api.slack.com/methods/users.list - https://api.slack.com/methods/users.getPresence - https://api.slack.com/methods/users.info """ @@ -35,8 +36,8 @@ def members_command(message, subcommand=None): # チャンネルのメンバー一覧を取得 channel = message.body['channel'] webapi = slacker.Slacker(settings.API_TOKEN) - cinfo = webapi.channels.info(channel) - members = cinfo.body['channel']['members'] + cinfo = webapi.conversations.members(channel) + members = cinfo.body['members'] # 全メンバーを取得 all_user_info = webapi.users.list()