Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include a Dockerfile and change configuration strategy to use a JSON file #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:2.7.10-onbuild
MAINTAINER Robert Ross <robert@creativequeries.com>

CMD ["python", "./slackbotExercise.py"]

RUN mkdir -p /slackbot

# A small protection against building a container with credentials inside of it
# Just remove the config if it gets added to the container.
RUN rm ./config.json || true
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ A fun hack that gets Slackbot to force your teammates to work out!

<img src="https://ctrlla-blog.s3.amazonaws.com/2015/Jun/Screen_Shot_2015_06_03_at_8_44_00_AM-1433557565175.png" width = 500>

6. Save your SLACK_USER_TOKEN_STRING and SLACK_URL_TOKEN_STRING as environmental variables in your terminal.
6. Copy the `default.json` file to `config.json` ( `cp default.json config.json` )

`$ export SLACK_USER_TOKEN_STRING=YOURUSERTOKEN`
6. Save your user token and url token in the `config.json` file you just copied.

`$ export SLACK_URL_TOKEN_STRING=YOURURLTOKEN`

Open `default.json` and set `teamDomain` (ex: ctrlla) `channelName` (ex: general) and `channelId` (ex: B22D35YMS). Save the file as `config.json` in the same directory. Set any other configurations as you like.
Open `config.json` and set `teamDomain` (ex: ctrlla) `channelName` (ex: general) and `channelId` (ex: B22D35YMS).
Set any other configurations as you like.

If you don't know the channel Id, fetch it using

Expand All @@ -48,3 +48,31 @@ A fun hack that gets Slackbot to force your teammates to work out!
`$ python slackbotExercise.py`

Run the script to start the workouts and hit ctrl+c to stop the script. Hope you have fun with it!

## Docker

This project includes a Dockerfile that will run the python script within a container.

To run the Docker container, follow these steps:

#### Pull the container

```sh
$ docker pull slackbots/exercise
```

#### Run the container

This container expects a config file somewhere in its file system. The easiest
way to do this, is to mount a directory on the host, inside of the container. We
do this using the `-v` flag when running the container.

The example we give is `-v "$PWD:/slackbot"`, Where `$PWD` is the current direcotry
you're mounting. Just make sure you have a JSON config file in it.

After that, you'll need to set an environment variable when running the
container to point to the configuration file.

```sh
$ docker run -t -v "$PWD:/slackbot" -e "CONFIG_FILE=/slackbot/config.json" slackbots/exercise
```
11 changes: 10 additions & 1 deletion User.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
import datetime

# Environment variables must be set with your tokens
USER_TOKEN_STRING = os.environ['SLACK_USER_TOKEN_STRING']
CONFIG_FILE_STRING = os.environ['CONFIG_FILE']
USER_TOKEN_STRING = ''
URL_TOKEN_STRING = ''

HASH = "%23"

with open(CONFIG_FILE_STRING) as f:
settings = json.load(f)
USER_TOKEN_STRING = settings['userToken']
URL_TOKEN_STRING = settings['urlToken']

class User:

Expand Down
6 changes: 4 additions & 2 deletions default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"teamDomain": "yourDomainHere",
"channelName": "general",
"channelId": "channelIdHere",
"channelName": "exercise",
"channelId": "",
"userToken": "",
"urlToken": "",

"officeHours": {
"on": false,
Expand Down
9 changes: 0 additions & 9 deletions results.csv

This file was deleted.

13 changes: 9 additions & 4 deletions slackbotExercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
from User import User

# Environment variables must be set with your tokens
USER_TOKEN_STRING = os.environ['SLACK_USER_TOKEN_STRING']
URL_TOKEN_STRING = os.environ['SLACK_URL_TOKEN_STRING']
CONFIG_FILE_STRING = os.environ['CONFIG_FILE']
USER_TOKEN_STRING = ''
URL_TOKEN_STRING = ''

HASH = "%23"

with open(CONFIG_FILE_STRING) as f:
settings = json.load(f)
USER_TOKEN_STRING = settings['userToken']
URL_TOKEN_STRING = settings['urlToken']

# Configuration values to be set in setConfiguration
class Bot:
Expand Down Expand Up @@ -50,7 +55,7 @@ def loadUserCache(self):
'''
def setConfiguration(self):
# Read variables fromt the configuration file
with open('config.json') as f:
with open(CONFIG_FILE_STRING) as f:
settings = json.load(f)

self.team_domain = settings["teamDomain"]
Expand Down Expand Up @@ -221,7 +226,7 @@ def assignExercise(bot, exercise):

# Announce the user
if not bot.debug:
requests.post(bot.post_URL, data=winner_announcement)
response = requests.post(bot.post_URL, data=winner_announcement)
print winner_announcement


Expand Down