Exemplo n.º 1
0
def receive_event(request):
    debug("Hangouts args: " + str(request['args']))
    token = required_parameter(request, 'token')
    type = required_parameter(request, 'type')
    if token != hangouts_verification:
        return {"success": False, "message": "Invalid token"}
    # Google doesn't provide an ID, so we use a hash of the event to check for already processed events
    event_id = sha256(str(request['args']).encode('utf-8'))
    space_id = json.loads(required_parameter(request, 'space'))['name']
    if event_id in _processed_events:
        # We've already processed this event
        return {"success": True}
    _processed_events.append(event_id)
    if type == 'ADDED_TO_SPACE':
        return process_new_space(space_id)
    elif type == 'MESSAGE':
        space = HangoutsSpace.get('space_id', space_id)
        if space is None:
            return process_new_space(space_id)
        user = User.get('user_id', space.user_id)
        message = json.loads(required_parameter(request, 'message'))['text']
        response = process_message(user, space, request, message)
        return response
    else:
        warning("Unsupported hangouts message")
        return {"success": False}
Exemplo n.º 2
0
def associate(request):
    space_id = required_parameter(request, 'space')
    space = HangoutsSpace()
    space.user_id = request['user'].user_id
    space.space_id = space_id
    space.save()
    return redirect('https://thecape.ai/hangouts.html')
Exemplo n.º 3
0
def oauth_callback(request):
    oauth_code = required_parameter(request, "code")
    slack_request = requests.Session()
    slack_response = slack_request.get("https://slack.com/api/oauth.access",
                                       params={
                                           "client_id": slack_client_id,
                                           "client_secret":
                                           slack_client_secret,
                                           "code": oauth_code
                                       })
    slack_request.close()
    try:
        bot = Bot(user_id=request['user'].user_id,
                  bot_id=slack_response.json()['bot']['bot_user_id'],
                  bot_token=slack_response.json()['bot']['bot_access_token'],
                  access_token=slack_response.json()['access_token'])
        bot.save()
    except KeyError:
        raise UserException(ERROR_INVALID_SLACK_RESPONSE)
    except IntegrityError:
        # We already have this bot, so update it with new tokens
        bot = Bot.get('bot_id', slack_response.json()['bot']['bot_user_id'])
        bot.user_id = request['user'].user_id
        bot.bot_token = slack_response.json()['bot']['bot_access_token']
        bot.access_token = slack_response.json()['access_token']
        bot.save()
        return redirect('https://thecape.ai/slack.html#complete')
    return redirect('https://thecape.ai/slack.html#complete')
def _link_account(request):
    redirect_uri = required_parameter(request, 'redirect_uri')
    bot = FacebookBot()
    bot.user_id = request['user'].user_id
    bot.authorization_code = str(uuid4())
    bot.save()
    redirect_uri += '&authorization_code=' + bot.authorization_code
    debug("Redirecting facebook auth to " + redirect_uri)
    return redirect(redirect_uri)
def receive_event(request):
    debug("facebook args: " + str(request['args']))
    mode = optional_parameter(request, 'hub.mode', None)
    if mode is not None:
        if mode == 'subscribe':
            token = required_parameter(request, 'hub.verify_token')
            challenge = required_parameter(request, 'hub.challenge')
            if token == facebook_verification:
                return challenge
            else:
                warning("Invalid token")
        else:
            warning("Unsupported facebook mode")
    else:
        entries = optional_parameter(request, 'entry', None)
        if entries is not None:
            entries = json.loads(entries)
            for entry in entries:
                if 'messaging' in entry:
                    for message_entry in entry['messaging']:
                        sender = message_entry['sender']['id']
                        if 'account_linking' in message_entry:
                            if message_entry['account_linking'][
                                    'status'] == 'linked':
                                authorization_code = message_entry[
                                    'account_linking']['authorization_code']
                                bot = FacebookBot.get('authorization_code',
                                                      authorization_code)
                                bot.facebook_psid = sender
                                bot.save()
                            elif message_entry['account_linking'][
                                    'status'] == 'unlinked':
                                bot = FacebookBot.get('facebook_psid', sender)
                                bot.delete_instance()
                        else:
                            process_message(sender, request,
                                            message_entry['message'])

        else:
            warning("Unsupported facebook event")

    return "OK"
Exemplo n.º 6
0
def receive_event(request):
    challenge = optional_parameter(request, 'challenge', None)
    if challenge is not None:
        # Slack sends us a 'challenge' token when configuring the URL which we have to send back to confirm that we're
        # willing to receive events.
        return challenge
    event = required_parameter(request, 'event')
    event_id = required_parameter(request, 'event_id')
    if event_id in _processed_events:
        # We've already processed this event
        return "200 OK"
    _processed_events.append(event_id)
    bot: Bot = Bot.get('bot_id', required_parameter(request, 'authed_users')[0])
    _process_bot_message_event(bot, event)
    if _process_positive_reaction(bot, request, event):
        return "200 OK"
    if event['type'] == 'message' or event['type'] == 'app_mention' and 'subtype' not in event:
        process_message(bot, event, request)
    elif event['type'] == 'tokens_revoked':
        process_tokens_revoked(event)
    return "200 OK"
Exemplo n.º 7
0
def process_file(event, request):
    authed_users = required_parameter(request, 'authed_users')
    bot_id = authed_users[0]
    channel = event['channel']
    bot = Bot.get('bot_id', bot_id)
    slack_file = event['file']
    if slack_file['filetype'] not in ['text', 'markdown']:
        send_slack_message(bot.bot_token, channel, ERROR_FILE_TYPE_UNSUPPORTED)
    else:
        text = get_slack_file_contents(bot.bot_token, slack_file['url_private'])
        try:
            request['user'] = User.get('user_id', bot.user_id)
            request['args']['text'] = text
            request['args']['title'] = slack_file['title']
            request['args']['origin'] = slack_file['name']
            request['args']['replace'] = 'true'
            responder_upload_document(request)
            send_slack_message(bot.bot_token, channel, BOT_FILE_UPLOADED)
        except UserException as e:
            send_slack_message(bot.bot_token, channel, e.message)