Esempio n. 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[: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 = {"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
Esempio n. 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[: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
Esempio n. 3
0
def meme():
    memegen = Memegen()
    slack = Slack()

    if not request.args:
        return memegen.help()

    token = request.args["token"]
    text = request.args["text"].strip()
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

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

    if text.lower() in ("help", ""):
        return memegen.help()

    if text.lower() == "templates":
        return memegen.list_templates()

    try:
        template, top, bottom = parse_text_into_params(text)
    except ValueError:
        return "Bad command: `%s`\n" % text + memegen.help()

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

    if template in valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif image_exists(template):
        meme_url = memegen.build_url("custom", top, bottom, template)
    else:
        return memegen.bad_template(template)

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

    attachments = [{"image_url": meme_url, "fallback": "; ".join([top, bottom])}]
    payload.update({"attachments": attachments})

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

    return "Success!", 200
Esempio n. 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.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
Esempio n. 5
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
Esempio n. 6
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
            }
        ]
    }
Esempio n. 7
0
def meme():
    meme = Memegen()
    slack = Slack()

    token = request.args["token"]

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

    text = request.args["text"]

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

    try:
        template, top, bottom = meme.parse_text_into_params(text)
    except:
        return "Your syntax should be in the form `/meme [template]; [top]; [bottom];. Type `/meme templates` to see valid templates."

    valid_templates = [x["name"] for x in meme.get_templates()]

    if template not in valid_templates:
        return "That template doesn't exist. Type `/meme templates` to see valid templates."

    meme_url = meme.build_url(template, top, bottom)
    channel_id = request.args["channel_id"]

    payload = {"text": meme_url, "channel": channel_id}

    user_id = request.args["user_id"]
    user = slack.find_user_info(user_id)

    payload.update(user)
    slack.post_meme_to_webhook(payload)

    return "Success!", 200
Esempio n. 8
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()
    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()

    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 image_exists(template):
        meme_url = memegen.build_url("custom", top, bottom, template)
    else:
        return memegen.error()

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

    attachments = [{"image_url": meme_url, "fallback": "; ".join([top, bottom])}]
    payload.update({"attachments": attachments})

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

    return "Success!", 200
Esempio n. 9
0
def meme():
    memegen = Memegen()
    slack = Slack()

    if not request.args:
        return memegen.help()

    token = request.args["token"]
    text = request.args["text"].strip()
    channel_id = request.args["channel_id"]
    user_id = request.args["user_id"]

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

    if text.lower() in ("help", ""):
        return memegen.help()

    if text.lower() == "templates":
        return memegen.list_templates()

    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 image_exists(template):
        meme_url = memegen.build_url("custom", top, bottom, template)
    else:
        return memegen.bad_template(template)

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

    attachments = [{"image_url": meme_url, "fallback": "; ".join([top, bottom])}]
    payload.update({"attachments": attachments})

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

    return "Success!", 200
Esempio n. 10
0
from flask import Flask, request
from models import Memegen, Slack, parse_text_into_params, image_exists

app = Flask(__name__)
memegen = Memegen()
slack = Slack()

@app.route("/", methods=["GET", "POST"])
def meme():
    data = request.form if request.method == 'POST' else request.args
    token, text, channel_id, user_id = [data[key] for key in ("token", "text", "channel_id", "user_id")]
    text = text.strip()

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

    if text.lower() in ("help", ""):
        return memegen.help()

    if text.lower() == "templates":
        return memegen.template_list

    template, top, bottom = parse_text_into_params(text)

    if template in memegen.valid_templates:
        meme_url = memegen.build_url(template, top, bottom)
    elif image_exists(template):
        meme_url = memegen.build_url("custom", top, bottom, template)
    else:
        return memegen.bad_template(template)
Esempio n. 11
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()
    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()

    if text[:6] == "search":
        command, search, ignore = parse_text_into_params(text)
        return memegen.search_templates(search)

    if text[:9] == "shortcuts":
        return memegen.list_shortcuts()
    
    if text[:6] == "create":
        name, url, description = parse_text_into_params(text)
        if name and url:
            set_shortcut(name, url, description)
            return "Success! Now you can use it with: `/meme %s;<top>;<bottom>`" % (name)
        else:
            return "You need to pass name and url at least to create a shortcut: `/meme create;<name>;<url>;<description>`"

    template, top, bottom = parse_text_into_params(text)
    templates_not_shortcuts = [t[0] for t in memegen.get_templates() if not t[3]]
    
    if template in templates_not_shortcuts:
        meme_url = memegen.build_url(template, top, bottom)
    elif get_shortcut(template):
        meme_url = memegen.build_url("custom", top, bottom, get_shortcut(template))
        print meme_url
    elif image_exists(template):
        print template
        meme_url = memegen.build_url("custom", top, bottom, template)
        print meme_url
    else:
        return memegen.error()

    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