2016-05-18 00:15:22 -04:00
|
|
|
# cowsay.py
|
|
|
|
# Necessary plugin for productive Slack orgs.
|
|
|
|
# Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger('cowsay')
|
|
|
|
|
|
|
|
MESSAGE_RE = re.compile(r'(!cowsay)(.*)')
|
|
|
|
|
2016-12-04 00:46:39 -05:00
|
|
|
#
|
|
|
|
# rtmbot interface
|
|
|
|
#
|
|
|
|
|
|
|
|
config = None
|
2016-05-18 00:15:22 -04:00
|
|
|
outputs = []
|
|
|
|
|
|
|
|
def process_message(data):
|
2016-12-04 00:46:39 -05:00
|
|
|
if 'path' not in config:
|
|
|
|
LOGGER.error('Please define config.path pointing to the cowsay binary.')
|
|
|
|
return
|
|
|
|
|
2016-05-18 00:15:22 -04:00
|
|
|
try:
|
|
|
|
text = data['text']
|
|
|
|
except KeyError:
|
2016-12-04 00:46:39 -05:00
|
|
|
LOGGER.debug('Missing "text" key in data.')
|
2016-05-18 00:15:22 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# find the message
|
|
|
|
match = MESSAGE_RE.match(text)
|
|
|
|
if not match:
|
|
|
|
return
|
|
|
|
cmd = match.group(1)
|
|
|
|
if not cmd:
|
|
|
|
return
|
|
|
|
msg = match.group(2)
|
|
|
|
if not msg:
|
|
|
|
LOGGER.info('Cowsay command but no message.')
|
|
|
|
|
|
|
|
# cowsay it up
|
|
|
|
try:
|
2016-12-04 00:46:39 -05:00
|
|
|
out = subprocess.check_output([config['path'], msg]).decode()
|
2016-05-18 00:15:22 -04:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
LOGGER.error('Error running cowsay. (%d)', e.returncode)
|
|
|
|
return
|
|
|
|
out = '```\n{}```'.format(out)
|
|
|
|
outputs.append([data['channel'], out])
|
|
|
|
|