Skip to content

Commit

Permalink
Now it is possible to have giphy support in multiple channels (see: m…
Browse files Browse the repository at this point in the history
  • Loading branch information
devTechi committed Dec 2, 2015
1 parent 360782b commit 0bf28ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ Here's how to start:

3. **Run the server with the correct configuration**
7. Back on SSH or your terminal, add the following lines to your `~/.bash_profile`
- `export MATTERMOST_TOKEN=<your-token-here>` This is the token you copied in the last section
- `export PORT=<your-port-number>` The port number you want the integration to listen on (defaults to 5000)
- `export MATTERMOST_GIPHY_TOKEN=<your-token-here>` This is the token you copied in the last section
- `export MATTERMOST_GIPHY_PORT=<your-port-number>` The port number you want the integration to listen on (defaults to 5000)
8. Source your bash profile
- `source ~/.bash_profile`
9. Run the server
Expand All @@ -102,9 +102,9 @@ If you'd like to use this integration in a production envrionment, it is strongl

##### On Linux/Ubuntu 14.04 Web Server
1. Stop the process currently running the integration
1. Add the following lines to your `~/.bash_profile`
1. Add the following lines to your `~/.bash_profile` or `~/.bashrc`
- `export GIPHY_API_KEY=<your-api-key-here>` With your Giphy API key
2. Source your bash profile
- `source ~/.bash_profile`
- `source ~/.bash_profile` or `source ~/.bashrc`
3. Run the server again
- `python server.py`
25 changes: 13 additions & 12 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
RATING = 'pg' # the maximum parental rating of gifs posted (y, pg, pg-13, r)

GIPHY_API_KEY = 'dc6zaTOxFJmzC' # this is a public beta key, for production use you must go to http://api.giphy.com/submit and request a production key
MATTERMOST_TOKEN = '' # the Mattermost token generated when you created your outgoing webhook
MATTERMOST_GIPHY_TOKEN = '' # the Mattermost token generated when you created your outgoing webhook

@app.route('/')
def root():
Expand All @@ -31,20 +31,20 @@ def new_post():

data = request.form

if data['token'] != MATTERMOST_TOKEN:
print 'Tokens did not match, it is possible that this request came from somewhere other than Mattermost'
if MATTERMOST_GIPHY_TOKEN.find(data['token']) == -1:
print('Tokens did not match, it is possible that this request came from somewhere other than Mattermost')
return 'OK'

translate_text = data['text'][len(data['trigger_word']):]

if len(translate_text) == 0:
print "No translate text provided, not hitting Giphy"
print("No translate text provided, not hitting Giphy")
return 'OK'

gif_url = giphy_translate(translate_text)

if len(gif_url) == 0:
print 'No gif url found, not returning a post to Mattermost'
print('No gif url found, not returning a post to Mattermost')
return 'OK'

resp_data = {}
Expand All @@ -70,7 +70,7 @@ def giphy_translate(text):
resp = requests.get('http://api.giphy.com/v1/gifs/translate', params=params)

if resp.status_code is not requests.codes.ok:
print 'Encountered error using Giphy API, text=%s, status=%d, response_body=%s' % (text, resp.status_code, resp.json())
print('Encountered error using Giphy API, text=%s, status=%d, response_body=%s' % (text, resp.status_code, resp.json()))
return ''

resp_data = resp.json()
Expand All @@ -83,15 +83,16 @@ def giphy_translate(text):
ICON_URL = os.environ.get('ICON_URL', ICON_URL)
RATING = os.environ.get('RATING', RATING)
GIPHY_API_KEY = os.environ.get('GIPHY_API_KEY', GIPHY_API_KEY)
MATTERMOST_TOKEN = os.environ.get('MATTERMOST_TOKEN', MATTERMOST_TOKEN)
MATTERMOST_GIPHY_TOKEN = os.environ.get('MATTERMOST_GIPHY_TOKEN', MATTERMOST_GIPHY_TOKEN)

if len(GIPHY_API_KEY) == 0:
print "GIPHY_API_KEY must be configured. Please see README.md for instructions"
print("GIPHY_API_KEY must be configured. Please see README.md for instructions")
sys.exit()

if len(MATTERMOST_TOKEN) == 0:
print "MATTERMOST_TOKEN must be configured. Please see README.md for instructions"
if len(MATTERMOST_GIPHY_TOKEN) == 0:
print("MATTERMOST_GIPHY_TOKEN must be configured. Please see README.md for instructions")
sys.exit()

port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
port = int(os.environ.get('MATTERMOST_GIPHY_PORT', 5000))
# use 0.0.0.0 if it shall be accessible from outside of host
app.run(host='127.0.0.1', port=port)

0 comments on commit 0bf28ea

Please sign in to comment.