Compare commits

...
This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.

1 commit
master ... dev

Author SHA1 Message Date
Alex Huddleston
0b014390cc Modified runner script to work with dev/prod environments. 2019-07-09 16:10:01 +00:00
3 changed files with 41 additions and 2 deletions

View file

@ -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.

View file

@ -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 &

37
slack-bot-dev.py Normal file
View file

@ -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()