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

Sleep for slack #160

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions slacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ def add_channel_markup(self, channel_name, fail_silently=True):
if fail_silently:
return "#{}".format(channel_name)

def sleep_for_slack(self):
self.logger.debug('Slack requests can be at most once per second. Sleeping 1.')
time.sleep(1)

def get_with_retry_to_json(self, url):
# TODO: extract class
retry_attempts = 0
max_retry_attempts = 10
payload = None

self.sleep_for_slack()

while not payload:
response = self.session.get(url)

Expand Down Expand Up @@ -221,11 +228,15 @@ def get_all_user_objects(self):
url = self.url + "users.list?token=" + self.token
return self.get_with_retry_to_json(url)['members']

def post(self, *args, **kwargs):
self.sleep_for_slack()
return self.session.post(*args, **kwargs)

def archive(self, channel_name):
url_template = self.url + "channels.archive?token={}&channel={}"
cid = self.get_channelid(channel_name)
url = url_template.format(self.token, cid)
request = self.session.post(url)
request = self.post(url)
payload = request.json()
return payload

Expand Down Expand Up @@ -258,5 +269,5 @@ def post_message(self, channel, message, message_type=None):
if message_type:
post_data['attachments'] = json.dumps([{'fallback': message_type}], encoding='utf-8')

p = self.session.post(self.url + "chat.postMessage", data=post_data)
p = self.post(self.url + "chat.postMessage", data=post_data)
return p.json()