From afe1b0e35e12d4befdd19107ab587c19b0e49eff Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 4 Dec 2016 00:42:01 -0500 Subject: [PATCH] Add reactions plugin --- plugins/reactions.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/reactions.py diff --git a/plugins/reactions.py b/plugins/reactions.py new file mode 100644 index 0000000..0f17c13 --- /dev/null +++ b/plugins/reactions.py @@ -0,0 +1,51 @@ +# react.py +# Necessary plugin for productive Slack orgs. +# Eryn Wells + +import logging +import re +import requests + +LOGGER = logging.getLogger('react') + +EXTENSIONS = ('', '.gif', '.png', '.jpg') +MESSAGE_RE = re.compile(r'(!react)\s+(\w+)') + +# +# rtmbot interface +# + +config = None +outputs = [] + +def process_message(data): + LOGGER.debug(config) + if 'reactions_root' not in config: + LOGGER.error('Please define the root URL where reactions live in config.reactions_root') + return + + try: + text = data['text'] + except KeyError: + LOGGER.debug('Missing "text" key in data.') + return + + # find the message + match = MESSAGE_RE.match(text) + if not match: + return + reaction = match.group(2) + if not reaction: + LOGGER.info('Reaction command but no reaction.') + + for ext in EXTENSIONS: + url = '{}/{}{}'.format(config['reactions_root'], reaction, ext) + LOGGER.debug('Checking url: %s', url) + r = requests.head(url) + if r.status_code != 200: + continue + outputs.append([data['channel'], url]) + break + else: + outputs.append([data['channel'], 'Sorry, no reaction by that name']) +