Created
August 12, 2021 13:09
-
-
Save konifar/8d5138b1544521d552c4577a4d23f9e6 to your computer and use it in GitHub Desktop.
特定のSlackチャネルに全メンバーを一括招待するCode by Zapier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib.request | |
import urllib.parse | |
import urllib.error | |
API_BASE_URL = 'https://slack.com/api' | |
ALL_MEMBER_CHANNEL_ID = 'xxxxxxxx' # #generalなど、全メンバーが入っている元となるチャネルのID | |
def load_channel_info(channel_id, token): | |
"""指定したチャネルの情報を取得する | |
:param channel_id: (str) SlackのChannel ID | |
:param token: (str) SlackのAPI Token | |
:return: (str) レスポンスのjsonオブジェクト | |
""" | |
# curlコマンド例 | |
# curl -X POST -d 'channel={channel_id}' https://slack.com/api/conversations.members -H 'Authorization: Bearer {token}' | |
url = API_BASE_URL + '/conversations.members' | |
data = { | |
'channel': channel_id, | |
'limit': 300, | |
} | |
headers = { | |
'Authorization': 'Bearer ' + token, | |
} | |
req = urllib.request.Request(url, urllib.parse.urlencode(data).encode("utf-8"), headers) | |
try: | |
with urllib.request.urlopen(req) as res: | |
body = res.read() | |
body_json = json.loads(body.decode('utf-8')) | |
return body_json | |
except urllib.error.HTTPError as err: | |
print('[Error occurred] code:' + str(err.code) + ', reason: ' + err.reason) | |
except urllib.error.URLError as err: | |
print('[Error occurred] reason:' + err.reason) | |
def invite_members(channel_id, token, member_ids): | |
"""メンバーをチャネルに招待する | |
:param channel_id: (str) SlackのChannel ID | |
:param token: (str) SlackのAPI Token | |
:param member_ids: (str) カンマ区切りの招待するメンバーID一覧 | |
""" | |
# curlコマンド例 | |
# curl -X POST -d 'token={token}&channel={channel_id}&users={member_ids}' https://slack.com/api/conversations.invite -H 'Authorization: Bearer {token}' | |
url = API_BASE_URL + '/conversations.invite' | |
data = { | |
'channel': channel_id, | |
'users': member_ids, | |
} | |
headers = { | |
'Authorization': 'Bearer ' + token, | |
} | |
req = urllib.request.Request(url, urllib.parse.urlencode(data).encode("utf-8"), headers) | |
try: | |
with urllib.request.urlopen(req) as res: | |
body = res.read() | |
body_json = json.loads(body.decode('utf-8')) | |
return body_json | |
except urllib.error.HTTPError as err: | |
print('[Error occurred] code:' + str(err.code) + ', reason: ' + err.reason) | |
except urllib.error.URLError as err: | |
print('[Error occurred] reason:' + err.reason) | |
# Zapierから渡されるパラメータの取得 | |
# input_data = { | |
# 'token': 'xoxb-xxxxxxxxxxxxxxxxxxxxxx', | |
# 'channel_id': 'xxxxxxxxx', | |
# } | |
access_token = input_data['token'] | |
channel_id = input_data['channel_id'] | |
# 全メンバーがいるチャネル内のメンバーID一覧を取得 | |
info_json = load_channel_info(ALL_MEMBER_CHANNEL_ID, access_token) | |
print(info_json) | |
all_member_ids = info_json['members'] | |
# メンバー一覧をカンマ区切りに | |
member_ids = '' | |
for uid in all_member_ids: | |
member_ids += uid + ',' | |
print(member_ids) | |
# チャネルに招待 | |
result = invite_members(channel_id, access_token, member_ids) | |
print(result) | |
# Zapierの返り値にセット | |
output = { | |
'count': len(all_member_ids) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment