This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
134 lines (104 loc) · 4.4 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os, sys, json, logging, jsonpickle, urllib.request
from flask import Flask, request, Response, jsonify #import main Flask class and request object
from logging.handlers import RotatingFileHandler
from slackclient import SlackClient
from Models.SlackRequest import SlackRequest
from Models.SlackQueryResponse import SlackQueryResponse
DEBUG = os.getenv('DEGUG', default=True)
TEST_AUTH_KEY = os.getenv('TEST_AUTH_KEY')
SLACK_STANLEE_API_TOKEN = os.getenv('SLACK_STANLEE_API_TOKEN')
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
MARVEL_GOOGLE_CSE_ID = os.getenv('MARVEL_GOOGLE_CSE_ID')
DC_COMICS_GOOGLE_CSE_ID = os.getenv('DC_COMICS_GOOGLE_CSE_ID')
slack_client = SlackClient(SLACK_STANLEE_API_TOKEN)
app = Flask(__name__) #create the Flask app
@app.route('/', methods=['GET'])
def home():
return "<h1>StanLeeBot</h1>"
@app.route('/slack-marvel-response', methods=['POST'])
def SlackMarvelResponse():
sr = GetSlackResponse(request.form)
resp = Response(response=jsonpickle.encode(GetGoogleSearchSlackResponseJson(sr, MARVEL_GOOGLE_CSE_ID)),
status=200,
mimetype="application/json")
return resp
@app.route('/slack-marvel', methods=['POST'])
def SlackMarvel():
sr = GetSlackResponse(request.form)
slack_client.api_call(
"chat.postMessage",
channel=sr.ChannelId,
attachments=GetGoogleSearchSlackResponseJson(sr, MARVEL_GOOGLE_CSE_ID),
unfurl_links=True,
unfurl_media=True,
as_user=True
)
return Response(), 200
@app.route('/slack-dc', methods=['POST'])
def SlackDCComics():
sr = GetSlackResponse(request.form)
slack_client.api_call(
"chat.postMessage",
channel=sr.ChannelId,
attachments=GetGoogleSearchSlackResponseJson(sr, DC_COMICS_GOOGLE_CSE_ID),
unfurl_links=True,
unfurl_media=True,
as_user=True
)
return Response(), 200
@app.route('/test-marvel', methods=['GET'])
def TestMarvel():
authKey = request.args.get('auth_key')
if TEST_AUTH_KEY == authKey:
sr = GetSlackResponse(request.args)
resp = Response(response=jsonpickle.encode(GetGoogleSearchSlackResponseJson(sr, MARVEL_GOOGLE_CSE_ID)),
status=200,
mimetype="application/json")
return resp
else:
return Response(), 403
@app.route('/test-dc', methods=['GET'])
def TestDCComics():
authKey = request.args.get('auth_key')
if TEST_AUTH_KEY == authKey:
sr = GetSlackResponse(request.args)
resp = Response(response=jsonpickle.encode(GetGoogleSearchSlackResponseJson(sr, DC_COMICS_GOOGLE_CSE_ID)),
status=200,
mimetype="application/json")
return resp
else:
return Response(), 403
def GetSlackResponse(req):
sr = SlackRequest()
sr.Command = req.get('command')
sr.ChannelName = req.get('channel_name')
sr.ChannelId = req.get('channel_id')
sr.UserName = req.get('user_name')
sr.UserId = req.get('user_id')
sr.Text = urllib.parse.quote(req.get('text'))
sr.ResponseUrl = req.get('response_url')
sr.TriggerId = req.get('trigger_id')
return sr
def GetGoogleSearchSlackResponseJson(sr, cse):
url = "https://www.googleapis.com/customsearch/v1?cx={}&key={}&q={}".format(cse, GOOGLE_API_KEY, sr.Text)
app.logger.debug(jsonpickle.encode(sr))
app.logger.debug("Url Requested: {}".format(url))
res = urllib.request.urlopen(url)
res_body = res.read()
j = json.loads(res_body.decode("utf-8"))
sqr = SlackQueryResponse()
sqr.FromJson(j)
return sqr.ToJson()
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
if __name__ == '__main__':
formatter = logging.Formatter( "%(asctime)s | %(pathname)s:%(lineno)d | %(funcName)s | %(levelname)s | %(message)s ")
handler = RotatingFileHandler('logs/StanLeeBotApp.log', maxBytes=10000, backupCount=5)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
#app.logger.addHandler(handler)
app.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
app.logger.setLevel(logging.DEBUG)
app.run(debug=DEBUG) #run app in debug mode on port 5000