diff --git a/slacker.py b/slacker.py index 420307c..bd625a9 100755 --- a/slacker.py +++ b/slacker.py @@ -22,12 +22,13 @@ def __init__(self, slack_name, token, init=True): assert self.token, "Token should not be blank" self.url = self.api_url() self.session = requests.Session() + self.session.headers.update({"Authorization": "Bearer "+token}) if init: self.get_users() self.get_channels() def get_emojis(self): - url = self.url + "emoji.list?token={}".format(self.token) + url = self.url + "emoji.list" return self.get_with_retry_to_json(url) def get_users(self): @@ -72,6 +73,7 @@ def get_with_retry_to_json(self, url): self.logger.debug('Unknown requests error. Sleeping %s. %s/%s retry attempts.', retry_after, retry_attempts, max_retry_attempts) time.sleep(retry_after) continue + print(response.json()) payload = response.json() return payload @@ -82,7 +84,7 @@ def get_messages_in_time_range(self, oldest, cid, latest=None): messages = [] done = False while not done: - murl = self.url + "conversations.history?oldest={}&token={}&channel={}".format(oldest, self.token, cid) + murl = self.url + "conversations.history?oldest={}&channel={}".format(oldest, cid) if latest: murl += "&latest={}".format(latest) else: @@ -183,8 +185,8 @@ def get_channel_members_ids(self, channel_name): if not member_count: return members # should be an empty set - url_template = self.url + "conversations.members?token={}&channel={}" - url = url_template.format(self.token, cid) + url_template = self.url + "conversations.members?channel={}" + url = url_template.format(cid) while True: ret = self.get_with_retry_to_json(url) @@ -198,8 +200,8 @@ def get_channel_members_ids(self, channel_name): # once through the loop once, update the url to call to include the cursor if ret['response_metadata']['next_cursor']: - url_template = self.url + "conversations.members?token={}&channel={}&cursor={}" - url = url_template.format(self.token, cid, ret['response_metadata']['next_cursor']) + url_template = self.url + "conversations.members&channel={}&cursor={}" + url = url_template.format(cid, ret['response_metadata']['next_cursor']) # no more members to iterate over else: break @@ -231,10 +233,10 @@ def get_channel_info(self, channel_name): returns JSON with channel information. Adds 'age' in seconds to JSON """ # ensure include_num_members is available for get_channel_member_count() - url_template = self.url + "conversations.info?token={}&channel={}&include_num_members=true" + url_template = self.url + "conversations.info?channel={}&include_num_members=true" cid = self.get_channelid(channel_name) now = int(time.time()) - url = url_template.format(self.token, cid) + url = url_template.format(cid) ret = self.get_with_retry_to_json(url) if ret['ok'] is not True: m = "Attempted get_channel_info() for {}, but return was {}" @@ -259,8 +261,8 @@ def get_all_channel_objects(self, exclude_archived=True): else: exclude_archived = 0 - url_template = self.url + "conversations.list?exclude_archived={}&token={}" - url = url_template.format(exclude_archived, self.token) + url_template = self.url + "conversations.list?exclude_archived={}" + url = url_template.format(exclude_archived) while True: ret = self.get_with_retry_to_json(url) @@ -274,8 +276,8 @@ def get_all_channel_objects(self, exclude_archived=True): # after going through the loop once, update the url to call to # include the pagination cursor if ret['response_metadata']['next_cursor']: - url_template = self.url + "conversations.list?exclude_archived={}&token={}&cursor={}" - url = url_template.format(exclude_archived, self.token, ret['response_metadata']['next_cursor']) + url_template = self.url + "conversations.list?exclude_archived={}&cursor={}" + url = url_template.format(exclude_archived, ret['response_metadata']['next_cursor']) # no more channels to iterate over else: @@ -284,13 +286,13 @@ def get_all_channel_objects(self, exclude_archived=True): return channels def get_all_user_objects(self): - url = self.url + "users.list?token=" + self.token + url = self.url + "users.list" return self.get_with_retry_to_json(url)['members'] def archive(self, channel_name): - url_template = self.url + "conversations.archive?token={}&channel={}" + url_template = self.url + "conversations.archive?channel={}" cid = self.get_channelid(channel_name) - url = url_template.format(self.token, cid) + url = url_template.format(cid) request = self.session.post(url) payload = request.json() return payload @@ -307,7 +309,6 @@ def post_message(self, channel, message, message_type=None): channel = channel[1:] post_data = { - 'token': self.token, 'channel': channel, 'text': message.encode('utf-8') }