-
Notifications
You must be signed in to change notification settings - Fork 4
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
ISSHA-1329 add member rewrite #48
base: master
Are you sure you want to change the base?
Changes from all commits
c7eaa87
f2c2144
75e8544
5d1a4e5
6ce3684
86a6ecf
8f2a9f4
314eb8e
88ac45b
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from slackbot.bot import respond_to | ||
from slackbot import settings | ||
import slacker | ||
import json | ||
|
||
from ..botmessage import botsend, botreply, botwebapi | ||
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. SHOULD 使っていない import があるので削除してください |
||
|
||
|
||
@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/conversations.members | ||
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. channels.infoはdeprecatedになったので、APIを変更しました |
||
- https://api.slack.com/methods/users.list | ||
- https://api.slack.com/methods/users.getPresence | ||
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. SHOULD: このあたりのAPIも使ってなさそうなので削除してほしい |
||
- 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.conversations.members(channel) | ||
members = cinfo.body['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'] | ||
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. MAY: ループの外側で |
||
if x['id'] == member_id] | ||
# 全ユーザ情報リストに該当無いケースは無視 | ||
if memberkeys == []: | ||
continue | ||
|
||
# idが複数ヒットした場合警告を出す | ||
if len(memberkeys) > 1: | ||
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. 質問: これ、ありえるんですかね? |
||
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 != "": | ||
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. SHOULD: |
||
basename = display_name | ||
|
||
member_list.append(basename) | ||
|
||
# 探しやすいように大小文字区別なしアルファベット順 | ||
member_list.sort(key=str.lower) | ||
|
||
pretext = 'このチャンネルの{0}参加者は以下{1}名です。'.format(desc, len(member_list)) | ||
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. MAY: f-stringに書き換えたい |
||
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) |
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.
MUST: membersは別プラグインなので、新しい見出し
members plugin
を追加して、そこに書いてください