-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
75 lines (61 loc) · 2.36 KB
/
api.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
import flask, requests
from base64 import b64encode, b64decode
from utils import BearerAuth
auth_url = 'https://slack.com/oauth/v2/authorize'
scopes = "chat:write"
client_id, client_secret = open("../oauth.txt", "r").read().splitlines()
access_url = "https://slack.com/api/oauth.v2.access"
revoke_url = "https://slack.com/api/auth.revoke"
postMessageURL = "https://slack.com/api/chat.postMessage"
def get_error_json(error):
return {
"text" : "Your latex couldn't be parsed.",
"response_type" : "ephemeral",
"blocks" : [{
"type" : "section",
"text" : {
"type" : "plain_text",
"text" : "Your latex couldn't be parsed. The error we received was '%s'." % error
}
}]
}
def get_authorize_url():
return "{aurl}?client_id={cid}&user_scope={scopes}".format(aurl = auth_url, cid = client_id, scopes = scopes)
def get_psst_json():
return dict(
text = 'Psst! I can do much more than just replying to your message.' + \
' If you give me permission, I can instead reply as you, making the chat much cleaner.' + \
' If this interests you, click ' + \
'<{aurl}|here> to give me permission.'.format(aurl = get_authorize_url(), cid = client_id, scopes = scopes) + \
" On the other hand, if you don't want me to mention this again," + \
" run '/mathconf nopsst' and I will stop nudging you."
,
mrkdwn = "true",
response_type = "ephemeral"
)
def internal_error_json(error):
return {
"text" : "An internal error has occurred. The error we got was '%s'" % error,
"response_type" : "ephemeral"
}
def get_success_json(inp):
return {
"text" : inp,
"response_type" : "in_channel",
"blocks" : [{
"type": "image",
"image_url": "https://slack-mathoid.kgugeler.ca/i/%s" % b64encode(inp.encode("utf-8")).decode("utf-8"),
"alt_text": inp
}]
}
def user_success_message(token, channel, inp):
json = get_success_json(inp)
json['channel'] = channel
json['as_user'] = True
return requests.post(postMessageURL, json = json, auth = BearerAuth(token))
def authorize_user(code):
return requests.post(access_url, auth = (client_id, client_secret), data = dict(code = code))
def revoke_token(token):
return requests.post(revoke_url, auth = BearerAuth(token))
def make_json_resp(json):
return flask.Response(flask.json.dumps(json), mimetype = "application/json")