-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapslack_flask.py
117 lines (97 loc) · 4.94 KB
/
snapslack_flask.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
104
105
106
107
108
109
110
111
112
113
114
115
116
from flask import Flask, request, render_template, make_response
import threading
import sys
import redis
import json
import requests
import requests.packages.urllib3
import time
requests.packages.urllib3.disable_warnings()
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('SNAPSLACK_SETTINGS')
db = redis.StrictRedis(host='localhost', port=6379, db=0)
@app.route('/')
def landingPage():
return render_template('landingpage.html')
@app.route('/oauth', )
def oauth():
code = request.args.get('code', '')
state = request.args.get('state', '')
if not code:
return "Couldn't get a code from Slack. Something must have gone wrong"
r = requests.post(app.config['SLACK_API_BASE']+'oauth.access', data = {"client_id":app.config['OAUTH_CLIENT_ID'], "client_secret":app.config['OATUH_CLIENT_SECRET'], "code":code})
if r.status_code == requests.codes.ok:
r = r.json()
team_id = r['team_id']
access_token = r['access_token']
# Get User ID
r = requests.post(app.config['SLACK_API_BASE']+'auth.test', data = {"token":access_token})
r = r.json()
db.set(r['user_id']+'-'+team_id, access_token)
else:
r.raise_for_status()
return render_template('error.html')
return render_template('authsuccess.html')
@app.route('/slash', methods=['POST'])
def slashCommand():
if request.form['token'] != app.config['SLACK_VERYFICATION_TOKEN']:
return ''
token = db.get(request.form['user_id']+'-'+request.form['team_id'])
if not token:
return "Looks like you don't have Snapslack enabled for your account yet. Please visit https://snapslack.conradfoucher.ca to enable it."
if request.form['text']:
# Must response in 3000 ms so spawn thread to do work and responde immediately
thread = threading.Thread(target=slashResponse(request.form, token))
thread.start()
response = make_response("", 200)
return response
else:
return "You need to type something in order to Snapslack!"
def slashResponse(form, token):
message = form['text']
tag = message
R = False
if message.find("/giphy") != -1:
if message.find("/giphyr") != -1:
tag = message.replace("/giphyr", "")
tag = tag.strip()
if tag:
r = requests.get(app.config['GIHPY_API_BASE']+'random', params={"api_key":app.config['GIHPY_API_KEY'], "tag":tag, "rating":"r"})
else:
response = "You must type something for Giphy to match against"
r = requests.post(form['response_url'], data = json.JSONEncoder().encode({"text":response}), headers = {'Content-Type': 'application/json'})
return
else:
tag = message.replace("/giphy", "")
tag = tag.strip()
if tag:
r = requests.get(app.config['GIHPY_API_BASE']+'random', params={"api_key":app.config['GIHPY_API_KEY'], "tag":tag})
else:
response = "You must type something for Giphy to match against"
r = requests.post(form['response_url'], data = json.JSONEncoder().encode({"text":response}), headers = {'Content-Type': 'application/json'})
return
r = r.json()
if r['meta']['msg'] == 'OK':
if r['data']:
message = message + "\n"+r['data']['url']
else:
response = "Giphy could not match "+tag
r = requests.post(form['response_url'], data = json.JSONEncoder().encode({"text":response}), headers = {'Content-Type': 'application/json'})
return
r = requests.post(app.config['SLACK_API_BASE']+'chat.postMessage', data = {"token":token, "channel":form['channel_id'], "text":"*Snapslack-`10`*: " + message, "username":'SS - '+form['user_name'], "as_user":'true'})
r = r.json()
if r['ok']:
snapData = {"channel_id": form['channel_id'], "ts":r['ts'], "team_id":form['team_id'], "text":form['text'], "user_id":form['user_id']}
db.zadd('snaps', str(time.time()), json.JSONEncoder().encode(snapData))
return
else:
if r['error'] == 'invalid_auth' or r['error'] == 'not_authed' or r['error'] == 'account_inactive':
response = "Looks like you don't have Snapslack enabled for your account yet. Please visit https://snapslack.conradfoucher.ca to enable it."
r = requests.post(form['response_url'], data = json.JSONEncoder().encode({"text":response}), headers = {'Content-Type': 'application/json'})
else:
response = "Something went wrong. Please try again later. If the issue persists please try reauthenticating at https://snapslack.conradfoucher.ca"
r = requests.post(form['response_url'], data = json.JSONEncoder().encode({"text":response}), headers = {'Content-Type': 'application/json'})
if __name__ == '__main__':
app.debug = app.config['DEBUG']
app.run(host='0.0.0.0')