OperationCode / operationcode_backend

This is the backend repo for the Operation Code website
https://operationcode.org
MIT License
62 stars 110 forks source link

Invite slack user Job #440

Closed apex-omontgomery closed 5 years ago

apex-omontgomery commented 5 years ago

SideKiq job service class

Why is this feature being added?

We want to invite users automatically without a delay. As this is causing issues with our other integrations.

What should your feature do?

Create a Job service class that will create a background action to invite users.

A python version of this is:

from creds import LEGACY_TOKEN

invite_api_url = "https://{team_id}.slack.com/api/users.admin.invite"

from exceptions import (AlreadyInTeam, InvalidInviteeEmail,
                         InvalidAuthToken, AlreadyInvited,
                         APIRequestError)

def invite(team_id, api_token, invitee_email, channels):
    url = invite_api_url.format(team_id=team_id)
    payload = {'email': invitee_email, 'token': api_token, 'extra_message': "Hi folks insert random message you want"}
    r = requests.post(url, data=payload)

    _process_response(response=r)

    return True

def _process_response(response):
    print(response)
    response_data = response.json()
    print(response_data)
    if not response.status_code == requests.codes.ok:
        raise APIRequestError('api_error')

    if not response_data.get('ok'):
        try:
            _check_error(response_data['error'])
        except Exception as e:
            print(e)

    return response_data

def _check_error(error):
    if error == 'invalid_auth':
        raise InvalidAuthToken(error)
    if error == 'already_in_team':
        raise AlreadyInTeam(error)
    if error == 'invalid_email':
        raise InvalidInviteeEmail(error)
    if error == 'already_invited':
        raise AlreadyInvited(error)

if __name__ =='__main__':
    user_emails = ['testemail@gmali.com']

    OC_TEAM = 'T03GSNF5H'
    OC_CHANNELS = ','.join(['C03GSNF77'])

    for email in user_emails:
        invite(OC_TEAM, LEGACY_TOKEN, email, OC_CHANNELS)
apex-omontgomery commented 5 years ago

Assigning myself, if anybody would like to help with this feel free too.