예제 #1
0
def meme():
    if not request.args:
        message = """
        Welcome to Slack Meme!
        Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>.
        """

        return message

    memegen = Memegen()
    memeifier = Memeifier()
    slack = Slack()

    token = request.args["token"]
    text = request.args["text"]
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

    if token != slack.SLASH_COMMAND_TOKEN:
        return "Unauthorized."

    if text.strip() == "":
        return memegen.error()

    if text[:9] == "templates":
        return memegen.list_templates()

    preview = True if text[:7] == "preview" else False
    text = text.replace("preview", "", 1) if preview else text

    template, top, bottom = parse_text_into_params(text)

    valid_templates = [x[0] for x in memegen.get_templates()]

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif memeifier.image_exists(template):
        meme_url = memeifier.build_url(template, top, bottom)
    else:
        return memegen.error()

    if preview:
        return meme_url

    payload = {"channel": channel_id}
    user = slack.find_user_info(user_id)
    payload.update(user)

    attachments = [{
        "image_url": meme_url,
        "fallback": "Oops. Something went wrong."
    }]
    payload.update({"attachments": attachments})

    try:
        slack.post_meme_to_webhook(payload)
    except Exception as e:
        return e

    return "Success!", 200
예제 #2
0
def meme():
    if not request.args:
        message = """
        Welcome to Slack Meme!
        Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>.
        """

        return message

    memegen = Memegen()
    memeifier = Memeifier()
    slack = Slack()

    token = request.args["token"]
    text = request.args["text"]
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

    if token != slack.SLASH_COMMAND_TOKEN:
        return "Unauthorized."

    if text.strip() == "":
        return memegen.error()

    if text[:9] == "templates":
        return memegen.list_templates()

    preview = True if text[:7] == "preview" else False
    text = text.replace("preview", "", 1) if preview else text

    template, top, bottom = parse_text_into_params(text)

    valid_templates = [x[0] for x in memegen.get_templates()]

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif memeifier.image_exists(template):
        meme_url = memeifier.build_url(template, top, bottom)
    else:
        return memegen.error()

    print(meme_url)
    
    if preview:
        return meme_url

    payload = {"channel": channel_id}
    user = slack.find_user_info(user_id)
    payload.update(user)

    attachments = [{"image_url": meme_url, "fallback": "Oops. Something went wrong."}]
    payload.update({"attachments": attachments})

    try:
        slack.post_meme_to_webhook(payload)
    except Exception as e:
        return e

    return "Success!", 200
예제 #3
0
def lambda_handler(event, context):
    req_body = event['body']
    params = parse_qs(req_body)
    logger.info(params)
    token = params['token'][0]
    if token != expected_token:
        logger.error("Request token (%s) does not match exptected", token)
        raise Exception("Invalid request token")

    memegen = Memegen()
    memeifier = Memeifier()

    try:
        text = params['text'][0]
    except KeyError:
        return {'text': memegen.error()}

    if text.strip() == "":
        return {'text': memegen.error()}

    if text[:9] == "templates":
        return {'text': memegen.list_templates()}

    preview = True if text[:7] == "preview" else False
    text = text.replace("preview", "", 1) if preview else text

    template, top, bottom = parse_text_into_params(text)

    valid_templates = [x[0] for x in memegen.get_templates()]

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif memeifier.image_exists(template):
        meme_url = memeifier.build_url(template, top, bottom)
    else:
        return { 'text': memegen.error()}

    if preview:
        response_type = 'ephemeral'
    else:
        response_type = 'in_channel'

    return {
        "response_type": response_type,
        "text": "",
        "attachments": [
            {
                "image_url": meme_url,
                "fallback": meme_url
            }
        ]
    }
예제 #4
0
def meme():
    if not request.args:
        message = """
        Welcome to Slack Meme!
        Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>.
        """

        return message

    memegen = Memegen()
    memeifier = Memeifier()
    slack = Slack()

    token = request.args["token"]
    text = request.args["text"]
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

    if token != slack.SLASH_COMMAND_TOKEN:
        return "Unauthorized."

    if text[:9] == "templates":
        return memegen.get_help()

    preview = True if text[:7] == "preview" else False
    text = text.replace("preview", "", 1) if preview else text

    template, top, bottom = parse_text_into_params(text)

    valid_templates = [x[0] for x in memegen.get_templates()]

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif memeifier.image_exists(template):
        meme_url = memeifier.build_url(template, top, bottom)
    else:
        return "That template doesn't exist. Type `/meme templates` to see valid templates or provide your own as a URL."

    if preview:
        return meme_url

    payload = {"text": meme_url, "channel": channel_id}
    user = slack.find_user_info(user_id)
    payload.update(user)

    try:
        slack.post_meme_to_webhook(payload)
    except Exception as e:
        return e

    return "Success!", 200
예제 #5
0
def meme():
    if not request.args:
        message = """
        Welcome to Slack Meme!
        Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>.
        """

        return message

    memegen = Memegen()
    memeifier = Memeifier()
    slack = Slack()

    token = request.args["token"]
    text = request.args["text"]
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

    if token != slack.SLASH_COMMAND_TOKEN:
        return "Unauthorized."

    if text[:9] == "templates":
        return memegen.get_help()

    preview = True if text[:7] == "preview" else False
    text = text.replace("preview", "", 1) if preview else text

    template, top, bottom = parse_text_into_params(text)

    valid_templates = [x[0] for x in memegen.get_templates()]

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif memeifier.image_exists(template):
        meme_url = memeifier.build_url(template, top, bottom)
    else:
        return "That template doesn't exist. Type `/meme templates` to see valid templates or provide your own as a URL."

    if preview:
        return meme_url

    payload = {"text": meme_url, "channel": channel_id}
    user = slack.find_user_info(user_id)
    payload.update(user)

    try:
        slack.post_meme_to_webhook(payload)
    except Exception as e:
        return e

    return "Success!", 200
예제 #6
0
def meme():
    memegen = Memegen()
    memeifier = Memeifier()
    slack = Slack()

    token = request.args["token"]
    text = request.args["text"]
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

    if token != slack.SLASH_COMMAND_TOKEN:
        return "Unauthorized."

    if text[:9] == "templates":
        return memegen.get_help()

    if text[:9] == "list":
        return memegen.get_help()

    template, top, bottom = parse_text_into_params(text)

    valid_templates = [x[0] for x in memegen.get_templates()]

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif memeifier.image_exists(template):
        meme_url = memeifier.build_url(template, top, bottom)
    else:
        return "That template doesn't exist. Type `/meme list` to see valid templates or provide your own as a URL."

    payload = {
        "channel": channel_id,
        "attachments": [{
            "text": "",
            "fields": [{ "text": "" }],
            "image_url": meme_url
        }]
    }
    user = slack.find_user_info(user_id)
    payload.update(user)

    slack.post_meme_to_webhook(payload)

    return "Success!", 200