This repository has been archived by the owner on Apr 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
server.py
103 lines (77 loc) · 3.19 KB
/
server.py
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
98
99
100
101
102
103
import os
import sys
import requests
import json
from urlparse import urlsplit,urlunsplit
from flask import Flask
from flask import request
from flask import Response
app = Flask(__name__)
USERNAME = 'giphy' # username the bot posts as
ICON_URL = 'https://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)
SCHEME = 'https' # scheme to be used for the gif url returned to mattermost
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_GIPHY_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 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")
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('https://api.giphy.com/v1/gifs/translate', params=params, verify=True)
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()
url = list(urlsplit(resp_data['data']['images']['original']['url']))
url[0] = SCHEME.lower()
return urlunsplit(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)
SCHEME = os.environ.get('SCHEME', SCHEME)
GIPHY_API_KEY = os.environ.get('GIPHY_API_KEY', GIPHY_API_KEY)
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")
sys.exit()
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('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)