Example #1
0
File: api.py Project: lyft/omnibot
def send_bot_im(team_name, bot_name, email):
    """
    Sends a message as a bot user to an IM (direct message) channel
    between a team member and a bot user,
    for the provided `team_name`, `bot_name`, and `email`.

    .. :quickref: Send an IM message between bot and user

    **Example request**:

    .. sourcecode:: http

       GET /api/v1/slack/send_im/myteam/mybot/[email protected]

    **Example response**:

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

       {
            "channel": "DC1234567",
            "message": {
                "bot_id": "BC1234567",
                "subtype": "bot_message",
                "text": "HI!",
                "ts": "1538593287.000100",
                "type": "message",
                "username": "******"
            },
            "ok": true,
            "ts": "1538593287.000100"
        }

    :param team_name: The team to search for the given bot,
                      as configured in omnibot.
    :type team_name: str
    :param bot_name: The bot sending the IM to the user,
                     as configured in omnibot.
    :type bot_name: str
    :param email: The email address of user to send message to
    :type email: str

    :resheader Content-Type: application/json
    :statuscode 200: success
    :statuscode 400: slack call returned a non-OK status
    :statuscode 404: team, bot, IM unable to be found,
                     or user deleted from slack team
    """
    data = request.json
    try:
        team = Team.get_team_by_name(team_name)
        bot = Bot.get_bot_by_name(team, bot_name)
    except TeamInitializationError:
        return jsonify({
            'error': 'provided team name was not found.',
            'team_name': team_name,
            'bot_name': bot_name,
            'email': email
        }), 404
    except BotInitializationError:
        return jsonify({
            'error': 'provided bot name was not found.',
            'team_name': team_name,
            'bot_name': bot_name,
            'email': email
        }), 404
    user = slack.get_user_by_email(bot, email)
    if not user:
        return jsonify({
            'error': 'unable to find slack user for given email.',
            'team_name': team_name,
            'bot_name': bot_name,
            'email': email
        }), 404
    im_id = slack.get_im_channel_id(bot, user['id'])
    if im_id is None:
        return jsonify({
            'error': 'unable to find IM channel.',
            'team_name': team_name,
            'bot_name': bot_name,
            'email': email
        }), 404
    data['kwargs']['channel'] = im_id
    ret = _perform_action(bot, data)
    if ret['ok']:
        return jsonify(ret), 200
    else:
        return jsonify(ret), 400
Example #2
0
File: api.py Project: lyft/omnibot
def get_user_v2(team_name, bot_name, email):
    """
    Returns basic user information, for the provided `email` in the `team_name`
    using the specified `bot_name`.

    .. :quickref: User; Get a user from a team, via their email

    **Example request**:

    .. sourcecode:: http

       GET /api/v1/slack/get_user/myteam/mybot/[email protected] HTTP/1.1

    **Example response**:

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

       {"email": "*****@*****.**", "name": "Test User", "team_id": "T123456",
        "user_id": "U123ABC"}

    :param team_name: The team to search for this user, as configured in
                      omnibot.
    :type team_name: str
    :param bot_name: The bot to use for the request, as configured in omnibot.
    :type bot_name: str
    :param email: The email address of the user to get.
    :type email: str
    :reqheader x-envoy-internal: Header that indicates whether or not this
                                 request is coming from an internal service
                                 or not. This is auto-set by envoy and doesn't
                                 need to be explicitly set.
    :resheader Content-Type: application/json
    :statuscode 200: success
    :statuscode 404: user with specified email could not be found using the
                     specified bot.
    """
    logger.debug('Getting user team={} bot={} email={}.',
                 extra={
                     'team': team_name,
                     'bot': bot_name,
                     'email': email,
                 })
    try:
        team = Team.get_team_by_name(team_name)
    except TeamInitializationError:
        return jsonify({'error': 'provided team name was not found.'}), 404
    try:
        bot = Bot.get_bot_by_name(team, bot_name)
    except BotInitializationError:
        return jsonify({'error': 'provided bot name was not found.'}), 404
    user = slack.get_user_by_email(bot, email)
    if not user:
        return jsonify({'error': 'user not found'}, ), 404
    name = slack.get_name_from_user(user)
    return jsonify({
        'user': {
            'email': email,
            'name': name,
            'team_id': team.team_id,
            'user_id': user['id']
        }
    })