async def slack_request(action, **params): slack_client = AsyncHTTPClient() params["token"] = config.slack_apitoken encoded_params = urlencode(params) url = "https://slack.com/api/{0}?{1}".format(action, encoded_params) request = HTTPRequest(url) try: response = await slack_client.fetch(request) except tornado.httpclient.HTTPError as e: # XXX Wait for throttling if e.code == 429: retry_after = e.response.headers.get("Retry-After", None) if retry_after is not None: svc_log.warn("slack throttled for {}s: {}".format( retry_after, url)) await tornado.gen.sleep(int(retry_after) + 1) svc_log.warn("slack done throttling: {}".format(url)) # Redo our call response = await slack_client.fetch(request) else: raise return json.loads(response.body.decode("utf-8"))
async def group_create(group_slug): """Create a group. Initially we check if the group already exists if it does we unarchive the group.""" channel_id = await group_slug_to_id(group_slug) if channel_id: svc_log.warn( "creation of group {} is causing unarchival".format(group_slug)) return await group_unarchive(group_slug) else: svc_log.warn("created group {}".format(group_slug)) response = await slack_request("groups.create", name=group_slug) return response["group"]["id"]
async def group_upkeep(group): """See if any members in the group are not allowed to be in this group, this function gets called with all allowed members.""" slack_channel_id = await group_slug_to_id(group.slug) slack_channel_current_members = await group_members(group.slug) slack_channel_current_members = set( member for member in slack_channel_current_members if member != 'U1YTLNYDC') slack_channel_wanted_members = set() # Get all members in this group, look up their slack ID (if any) for member in group.members: for identity in member.slack_identities: slack_member = await user_email_to_id(identity.email) if slack_member: slack_channel_wanted_members.add(slack_member) slack_to_invite = slack_channel_wanted_members - slack_channel_current_members slack_to_kick = slack_channel_current_members - slack_channel_wanted_members svc_log.warn( "running slack group upkeep for {} ({}), inviting {}, kicking {}". format(group.slug, len(slack_channel_current_members), len(slack_to_invite), len(slack_to_kick))) if group.slug not in ('eft-warriors', 'atxv', 'tech', 'specops', 'intel-hk', 'intel-citadels', 'intel-capitals', 'intel-supers', 'roamers'): return for member in slack_to_invite: await group_invite(group.slug, member) for member in slack_to_kick: await group_kick(group.slug, member) return None
async def group_unarchive(group_slug): """Restore a channel from the archive so it can be re-used.""" channel_id = await group_slug_to_id(group_slug) await slack_request("groups.unarchive", channel=channel_id) svc_log.warn("unarchived channel {}".format(group_slug))
async def group_archive(group_slug): """Archive a channel making it unavailable to users.""" channel_id = await group_slug_to_id(group_slug) await slack_request("groups.archive", channel=channel_id) svc_log.warn("archived channel {}".format(group_slug))