-
Notifications
You must be signed in to change notification settings - Fork 4
ISSHA-1329 チャンネルにいる参加者の一覧を返すコマンドを追加 #35
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
Changes from all commits
c7eaa87
f2c2144
75e8544
5d1a4e5
9b884bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重要: この書き方だとmemberの数だけAPIを叩いてしまうので、ちょっと迷惑かなと |
||
# 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 != "": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits: 以下でよい。空文字列でなければ true になる
|
||
basename = display_name | ||
|
||
if subcommand == 'all': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重要: 対象外だったらcontinueして、そうじゃなかったら最後に append するとコードが読みやすい
|
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits: メッセージとしては先に人数があった方が見やすいのでは。 https://api.slack.com/docs/message-attachments
|
||
desc, '\n'.join(nameall), len(nameall))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nits: 変数名がちょっとアレかなと... member_list とかでいいのでは