forked from mattermost/mattermost-integration-giphy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
97 lines (71 loc) · 2.81 KB
/
server.py
File metadata and controls
97 lines (71 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import sys
import requests
import json
from flask import Flask
from flask import request
from flask import Response
app = Flask(__name__)
USERNAME = 'giphy' # username the bot posts as
ICON_URL = 'http://api.giphy.com/img/api_giphy_logo.png' # display picture the bot posts with
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
@app.route('/')
def root():
"""
Home handler
"""
return "OK"
@app.route('/new_post', methods=['POST'])
def new_post():
"""
Mattermost new post event handler
"""
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'
return 'OK'
translate_text = data['text'][len(data['trigger_word']):]
if len(translate_text) == 0:
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'
return 'OK'
resp_data = {}
resp_data['text'] = gif_url
resp_data['username'] = USERNAME
resp_data['icon_url'] = ICON_URL
resp = Response(content_type='application/json')
resp.set_data(json.dumps(resp_data))
return resp
def giphy_translate(text):
"""
Giphy translate method, uses the Giphy API to find an appropriate gif url
"""
params = {}
params['s'] = text
params['rating'] = RATING
params['api_key'] = GIPHY_API_KEY
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())
return ''
resp_data = resp.json()
return resp_data['data']['images']['original']['url']
if __name__ == "__main__":
USERNAME = os.environ.get('USERNAME', USERNAME)
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)
if len(GIPHY_API_KEY) == 0:
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"
sys.exit()
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)