Skip to content

Commit

Permalink
Added envvar support for running on Heroku.
Browse files Browse the repository at this point in the history
- Use TEAM_1_WEBHOOK_OUT_TOKEN=xxxxxxxxxxxx style config for heroku.
- Also some python3 fixes.

(Squashed commits up to Fri Jun 30 11:13:00 2017 -0400, by wdoekes.)
  • Loading branch information
patcon authored and wdoekes committed Aug 25, 2017
1 parent 8b95f1c commit 86e157a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
*.pyc
__pycache__
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn slackbridge --log-file -
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Configuration in Slack:

Configuration of this application:

(Heroku configuration can be found below.)

* Set the `BASE_PATH` to `"/"`. If this script does not run in the root of
your HTTP server, you need to alter that.
* There is a `CONFIG` dictionary below. You need to configure it as
Expand Down Expand Up @@ -96,6 +98,34 @@ the response there:
who you can @mention.


Heroku
------

These instructions require [Heroku Command
Line](https://devcenter.heroku.com/articles/heroku-command-line).

```
heroku create
cp sample.env .env
# Properly set all environment variables in file
vim .env
# Test running the bridge locally
heroku local
# Push environment variables to Heroku
heroku config:push --overwrite
# Deploy to Heroku
git push heroku <my-branch>
```

Things to note:

* Free Heroku dynos can only run 18 hours per day. After that, the
slack bridge will simply not work. This can be very confusing. You
may wish to consider paying $7/month for a 24h dyno.
* Please see `sample.env` for example of how to set environment
variables.


TODO
----

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gunicorn==19.6.0
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.5.2
17 changes: 17 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ADMIN_EMAIL=[email protected]
WEB_CONCURRENCY=3

# CivicTechTO Slack
PORTAL_1_SIDE_A_WEBHOOK_OUT_TOKEN=xxxxxxxxxxxx
PORTAL_1_SIDE_A_WEBHOOK_IN_URL=https://hooks.slack.com/services/xxxxxxxxxxxx
PORTAL_1_SIDE_A_CHANNEL_NAME=portal-yowcivictech
PORTAL_1_SIDE_A_GROUP_NAME=yowcivictech
PORTAL_1_SIDE_A_WEB_API_TOKEN=xxxxxxxxxxxx
# YOWCivicTech Slack
PORTAL_1_SIDE_B_WEBHOOK_OUT_TOKEN=xxxxxxxxxxxx
PORTAL_1_SIDE_B_WEBHOOK_IN_URL=https://hooks.slack.com/services/xxxxxxxxxxxx
PORTAL_1_SIDE_B_CHANNEL_NAME=portal-civictechto
PORTAL_1_SIDE_B_GROUP_NAME=civictechto
PORTAL_1_SIDE_B_WEB_API_TOKEN=xxxxxxxxxxxx

# Can increment portal number and add another set...
33 changes: 32 additions & 1 deletion slackbridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
from email.header import Header
from email.mime.text import MIMEText
from multiprocessing import Process, Pipe
from os import environ
from pprint import pformat


Expand All @@ -142,7 +143,37 @@
# The subdictionaries contain 'iwh_url' for "Incoming WebHooks" post and
# a dictionary with payload updates ({'channel': '#new_chan'}).
# TODO: should we index it by "service_id" instead of "(owh)token"?
CONFIG = {}
try:
CONFIG = {}
i = 1

while True:
portal_config = {
environ['PORTAL_{}_SIDE_A_WEBHOOK_OUT_TOKEN'.format(i)]: {
'iwh_url': environ['PORTAL_{}_SIDE_B_WEBHOOK_IN_URL'.format(i)],
'iwh_update': {
'channel': environ['PORTAL_{}_SIDE_B_CHANNEL_NAME'.format(i)],
'_atchannel': environ['PORTAL_{}_SIDE_A_GROUP_NAME'.format(i)],
},
'owh_linked': environ['PORTAL_{}_SIDE_B_WEBHOOK_OUT_TOKEN'.format(i)],
'wa_token': environ['PORTAL_{}_SIDE_A_WEB_API_TOKEN'.format(i)],
},
environ['PORTAL_{}_SIDE_B_WEBHOOK_OUT_TOKEN'.format(i)]: {
'iwh_url': environ['PORTAL_{}_SIDE_A_WEBHOOK_IN_URL'.format(i)],
'iwh_update': {
'channel': environ['PORTAL_{}_SIDE_A_CHANNEL_NAME'.format(i)],
'_atchannel': environ['PORTAL_{}_SIDE_B_GROUP_NAME'.format(i)],
},
'owh_linked': environ['PORTAL_{}_SIDE_A_WEBHOOK_OUT_TOKEN'.format(i)],
'wa_token': environ['PORTAL_{}_SIDE_B_WEB_API_TOKEN'.format(i)],
},
}

CONFIG.update(portal_config)
i += 1
except KeyError:
pass

# Lazy initialization of workers?
LAZY_INITIALIZATION = True # use, unless you have uwsgi-lazy-apps
# Notification settings (mail_admins) in case of broken connections.
Expand Down

0 comments on commit 86e157a

Please sign in to comment.