import requests


def cat(argv, params):
    """Returns a slack attachment with a picture of a cat from thecatapi"""
    # Print prints logs to cloudwatch
    # Send response to response url
    caturl = 'http://thecatapi.com/api/images/get?format=xml&size=med&type=jpg,png&results_per_page=1'
    start = '<url>'
    end = '</url>'
    catr = requests.get(caturl)
    cattext = catr.text
    url = (cattext.split(start))[1].split(end)[0]

    payload = {
        'statusCode': '200',
        "attachments": [
            {
                "author_name": '@{} /catops cat'.format(
                    params.get('user_name', ['CatOps'])[0]),
                "fallback": "Meow meow.",
                "title": "Meow!",
                "text": "Here is a cat.",
                "image_url": url,
                "color": "#764FA5"
            }
        ],
        'response_type': 'in_channel',
        'headers': {'Content-Type': 'application/json'}
    }
    return payload