Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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 - 2021-02-13
--------------------------
- [ISSHA-1329] add members command

Release Notes - 2021-02-13
--------------------------
- [ISSHA-2297] add multiple plusplus 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 @@ -235,6 +235,9 @@ BOT: @takanory おはようございます
- `$cal`: 今月のカレンダーを返す
- `$cal 9`: 今年の指定された月のカレンダーを返す
- `$cal 9 2016`: 指定された年月のカレンダーを返す
- `$members`: チャンネルにいる通常の参加者のメンション名の一覧を返す
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MUST: membersは別プラグインなので、新しい見出し members plugin を追加して、そこに書いてください

- `$members bot`: チャンネルにいるbotの参加者のメンション名の一覧を返す
- `$members all`: チャンネルにいる全ての参加者のメンション名の一覧を返す
- [misc.py](https://github.com/pyconjp/pyconjpbot/blob/master/pyconjpbot/plugins/misc.py)

## How to build
Expand Down
96 changes: 96 additions & 0 deletions pyconjpbot/plugins/members.py
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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

channels.infoはdeprecatedになったので、APIを変更しました

https://api.slack.com/methods/channels.info

- https://api.slack.com/methods/users.list
- https://api.slack.com/methods/users.getPresence
Copy link
Member

Choose a reason for hiding this comment

The 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']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAY: ループの外側で
[x for x in all_user_info.body['members'] if x['id'] in members] で一度にリスト作っちゃうんじゃだめかなーって思いました(動作確認はしてません)

if x['id'] == member_id]
# 全ユーザ情報リストに該当無いケースは無視
if memberkeys == []:
continue

# idが複数ヒットした場合警告を出す
if len(memberkeys) > 1:
Copy link
Member

Choose a reason for hiding this comment

The 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 != "":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHOULD: if display_name だけでよい

basename = display_name

member_list.append(basename)

# 探しやすいように大小文字区別なしアルファベット順
member_list.sort(key=str.lower)

pretext = 'このチャンネルの{0}参加者は以下{1}名です。'.format(desc, len(member_list))
Copy link
Member

Choose a reason for hiding this comment

The 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)
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