Skip to content

ISSHA-1329 チャンネルにいるメンバーの一覧を返すコマンドを追加 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased
----------

Release Notes - 2019-02-02
--------------------------
- [ISSHA-1329] add members command

Release Notes - 2018-06-29
--------------------------
- [ISSHA-1286] add verification of event title to pycamp summary command
Expand Down
3 changes: 3 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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
Expand Down
64 changes: 64 additions & 0 deletions pyconjpbot/plugins/misc.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
2 changes: 1 addition & 1 deletion pyconjpbot/plugins/term.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down