diff --git a/README.md b/README.md index 7c8ed03..8238b4e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Firstly, this bot was written using BASH so I'm unsure how well this will run on Clone this repo, then make sure you have the `slackclient` module installed in your python environment. You will need an API token from Slack, you can learn that and more on how to set up a bot from this documentation: https://github.com/slackapi/python-slackclient/blob/master/tutorial/01-creating-the-slack-app.md Once you have set up and installed your app/bot, you can run it by typing: -`./runner.sh` +`./runner.sh prod` The bot should automatically put itself in the background, currently the only way to kill it is either to execute a `kill` command or use something like `htop` to kill the process. diff --git a/runner.sh b/runner.sh index 2b98d8f..f6e298d 100755 --- a/runner.sh +++ b/runner.sh @@ -1,3 +1,5 @@ #!/bin/bash -SLACK_API_TOKEN=$(cat .api_token) ~/miniconda3/bin/python slack-bot-testing.py & +RUNNER_ENV=$1 + +SLACK_API_TOKEN=$(cat .api_token) ~/miniconda3/bin/python slack-bot-$RUNNER_ENV.py & diff --git a/slack-bot-dev.py b/slack-bot-dev.py new file mode 100644 index 0000000..1a922c9 --- /dev/null +++ b/slack-bot-dev.py @@ -0,0 +1,37 @@ +from import_db import import_cf_words +from random import randrange as rr +from bot_logging import log_output +from datetime import datetime +import slack +import sys +import os +import re + +output = '{}\tI am running\n'.format(datetime.now()) + +words = import_cf_words() + +@slack.RTMClient.run_on(event='message') +def say_hello(**payload): + output = '{0}\t{1}\n'.format(datetime.now(), payload['data']) + data = payload['data'] + web_client = payload['web_client'] + rtm_client = payload['rtm_client'] + try: + if 'text' in data and 'user' in data: + match = re.search('(\\bc[a-zA-Z]+ \\bf[a-zA-Z]+|cf)', data['text'].lower()) + if match: + match_formatted = ' '.join([ m.capitalize() for m in match.groups()[0].split() ]) + web_client.chat_postMessage( + channel = data['channel'], + text = '{0}, also known as: {1} {2}'.format(match_formatted, (words['c'][rr(len(words['c']))]).capitalize(), (words['f'][rr(len(words['f']))]).capitalize()) + ) + + except Exception as e: + output += '{0}\tAn error occurred: {1}\n'.format(datetime.now(), e) + log_output(output) + +slack_token = os.environ["SLACK_API_TOKEN"] +rtm_client = slack.RTMClient(token=slack_token) +rtm_client.start() +