Skip to content

Commit

Permalink
Move token from the query strings to the session
Browse files Browse the repository at this point in the history
Slack stopped allowing tokens in query strings.
See
https://api.slack.com/changelog/2020-11-no-more-tokens-in-querystrings-for-newly-created-apps

This moves the token handling into the request sessions object, which also
cleans up the code a bit.

Fixes #209
  • Loading branch information
christofdamian committed Mar 23, 2021
1 parent d9c2cf8 commit fec703c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions slacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {}"
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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')
}
Expand Down

0 comments on commit fec703c

Please sign in to comment.