Exemple #1
0
def send_slack_raw(slug, token, channel_id, data={}):
    logger.debug(f"Sending slack message to {str(channel_id)}")

    try:
        if "slack_payload" in data:
            payload = data["slack_payload"]
            print(payload)
        else:
            template = get_template_content(slug, data, ["slack"])
            payload = json.loads(template['slack'])
            if "blocks" in payload:
                payload = payload["blocks"]

        # for modals mainly
        meta = ""
        if "private_metadata" in payload:
            meta = payload["private_metadata"]

        api = client.Slack(token)
        data = api.post(
            "chat.postMessage", {
                "channel": channel_id,
                "private_metadata": meta,
                "blocks": payload,
                "parse": "full"
            })
        logger.debug(f"Notification to {str(channel_id)} sent")
        return True
    except Exception:
        logger.exception(f"Error sending notification to {str(channel_id)}")
        return False
Exemple #2
0
def sync_slack_team_channel(team_id):

    logger.debug(f"Sync slack team {team_id}: looking for channels")

    team = SlackTeam.objects.filter(id=team_id).first()
    if team is None:
        raise Exception("Invalid team id: " + str(team_id))

    credentials = CredentialsSlack.objects.filter(
        team_id=team.slack_id).first()
    if credentials is None:
        raise Exception(f"No credentials found for this team {team_id}")

    # Starting to sync, I need to reset the status
    team.sync_status = 'INCOMPLETED'
    team.synqued_at = timezone.now()
    team.save()

    api = client.Slack(credentials.token)
    data = api.get("conversations.list", {
        "types": "public_channel,private_channel",
        "limit": 300,
    })

    channels = data['channels']
    while 'response_metadata' in data and 'next_cursor' in data[
            'response_metadata'] and data['response_metadata'][
                'next_cursor'] != "":
        print("Next cursor: ", data['response_metadata']['next_cursor'])
        data = api.get(
            "conversations.list", {
                "limit": 300,
                "cursor": data['response_metadata']['next_cursor'],
                "types": "public_channel,private_channel",
            })
        channels = channels + data['channels']

    logger.debug(f"Found {str(len(channels))} channels, starting to sync")
    for channel in channels:

        # only sync channels
        if channel["is_channel"] == False and channel[
                'is_group'] == False and channel['is_general'] == False:
            continue

        # will raise exception if it fails
        sync_slack_channel(channel, team)

    # finished sync, status back to normal
    team.sync_status = 'COMPLETED'
    team.save()

    return True
Exemple #3
0
def sync_slack_team_users(team_id):

    logger.debug(f"Sync slack team {team_id}: looking for users")

    team = SlackTeam.objects.filter(id=team_id).first()
    if team is None:
        raise Exception("Invalid team id: " + str(team_id))

    credentials = CredentialsSlack.objects.filter(
        team_id=team.slack_id).first()
    if credentials is None:
        raise Exception(f"No credentials found for this team {team_id}")

    # Starting to sync, I need to reset the status
    team.sync_status = 'INCOMPLETED'
    team.synqued_at = timezone.now()
    team.save()

    api = client.Slack(credentials.token)
    data = api.get("users.list", {"limit": 300})

    members = data['members']
    while 'response_metadata' in data and 'next_cursor' in data[
            'response_metadata'] and data['response_metadata'][
                'next_cursor'] != "":
        print("Next cursor: ", data['response_metadata']['next_cursor'])
        data = api.get("users.list", {
            "limit": 300,
            "cursor": data['response_metadata']['next_cursor']
        })
        members = members + data['members']

    logger.debug(f"Found {str(len(members))} members, starting to sync")
    for member in members:

        # ignore bots
        if member["is_bot"] or member["name"] == "slackbot":
            continue

        # will raise exception if it fails
        sync_slack_user(member, team)

    # finished sync, status back to normal
    team.sync_status = 'COMPLETED'
    team.save()

    return True
from breathecode.services.slack import client

api = client.Slack(token)
data = api.get("users.list", {"limit": 300})

members = data['members']
while 'response_metadata' in data and 'next_cursor' in data[
        'response_metadata'] and data['response_metadata']['next_cursor'] != "":
    print("Next cursor: ", data['response_metadata']['next_cursor'])
    data = api.get("users.list", {
        "limit": 300,
        "cursor": data['response_metadata']['next_cursor']
    })
    members = members + data['members']

print(len(members))

from breathecode.services.slack import client
api = client.Slack(token)
data = api.get("users.list")
print(len(data['members']))