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
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 } ] }
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
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
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
def meme(): 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.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) 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
def orly(): print request.form if not request.form: message = """ Bad Request, Try Again """ return message, 400 try: token = request.form["token"] text = request.form["text"] # print "text is : ", text is_private_channel = request.form["channel_name"] == 'privategroup' channel_id = request.form["channel_id"] team_id = request.form["team_id"] user_name = request.form["user_name"] SLASH_COMMAND_TOKEN = os.environ.get( "SLACK_SLASH_COMMAND_TOKEN").strip() if token != SLASH_COMMAND_TOKEN: return "Unauthorized." title, topText, author, image_code, theme = parse_text_into_params( text) except Exception as e: print "Unexpected error:", e.message return "Failed: Invalid Parameters", 500 print "generating image" try: final_path = generate_image(title, topText, author, image_code, theme) except Exception as e: print "Unexpected error:", e.message return "Failed", 500 print "image generated" try: file_name = '%s.png' % datetime.datetime.now() file_title = "Posted by %s" % user_name slack = Slacker(get_team_token(team_id)) bot_user_id, bot_access_token = get_team_bot(team_id) if bot_access_token is not None: bot = Slacker(bot_access_token) if is_private_channel: response = bot.groups.list() groups = response.body['groups'] print groups invited = False if groups is not None: for group in groups: if channel_id == group['id']: invited = True break if invited == False: slack.groups.invite(channel_id, bot_user_id) response = bot.files.upload(final_path, filename=file_name, title=file_title, channels=[channel_id]) else: response = slack.files.upload(final_path, filename=file_name, title=file_title, channels=[channel_id]) if response.successful: print "Succesfully uploaded file" return "Success", 200 else: print "Error uploading file" print response.error return "Failed: Error uploading file", 500 except Exception as e: print "Unexpected error:", e.message return "Failed", 500 finally: os.remove(final_path)
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
def orly(): print request.form if not request.form: message = """ Bad Request, Try Again """ return message, 400 try: token = request.form["token"] text = request.form["text"] # print "text is : ", text is_private_channel = request.form["channel_name"] == 'privategroup' channel_id = request.form["channel_id"] team_id = request.form["team_id"] user_name = request.form["user_name"] SLASH_COMMAND_TOKEN = os.environ.get("SLACK_SLASH_COMMAND_TOKEN").strip() if token != SLASH_COMMAND_TOKEN: return "Unauthorized." title, topText, author, image_code, theme = parse_text_into_params(text) except Exception as e: print "Unexpected error:", e.message return "Failed: Invalid Parameters", 500 print "generating image" try: final_path = generate_image(title, topText, author, image_code, theme) except Exception as e: print "Unexpected error:", e.message return "Failed", 500 print "image generated" try: file_name = '%s.png'%datetime.datetime.now() file_title = "Posted by %s"%user_name slack = Slacker(get_team_token(team_id)) bot_user_id, bot_access_token = get_team_bot(team_id) if bot_access_token is not None: bot = Slacker(bot_access_token) if is_private_channel: response = bot.groups.list() groups = response.body['groups'] print groups invited = False if groups is not None: for group in groups: if channel_id == group['id']: invited = True break if invited == False: slack.groups.invite(channel_id, bot_user_id) response = bot.files.upload(final_path, filename=file_name, title=file_title, channels=[channel_id]) else: response = slack.files.upload(final_path, filename=file_name, title=file_title, channels=[channel_id]) if response.successful: print "Succesfully uploaded file" return "Success", 200 else: print "Error uploading file" print response.error return "Failed: Error uploading file", 500 except Exception as e: print "Unexpected error:", e.message return "Failed", 500 finally: os.remove(final_path)