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 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 diff --git a/pyconjpbot/plugins/misc.py b/pyconjpbot/plugins/misc.py old mode 100644 new mode 100755 index 0d48b85..bdd687a --- 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))) 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',