-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.py
More file actions
69 lines (57 loc) · 1.93 KB
/
Copy pathapi.py
File metadata and controls
69 lines (57 loc) · 1.93 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
import config
from modules import database
from slack_bolt.app.async_app import AsyncWebClient
from sanic import Sanic, HTTPResponse
from sanic.views import HTTPMethodView
from sanic.response import redirect
db = database.get_connection()
app = Sanic("TLDR-Bot-api")
class SlackOauth(HTTPMethodView):
async def get(self, request):
code = request.args.get('code')
if not code:
return HTTPResponse("Invalid Code")
client = AsyncWebClient()
try:
response = await client.oauth_v2_access(
client_id=config.SLACK_CLIENT_ID,
client_secret=config.SLACK_CLIENT_SECRET,
code=code
)
except:
return HTTPResponse("Authentication Failed.")
team_id = response['team']['id']
access_token = response['access_token']
bot_id = response['bot_user_id']
# update slack team in the database
data = db.slack_bridge.find_one({'team_id': team_id})
if data and data.get('token', '') != access_token:
db.slack_bridge.update_one(
{'team_id': team_id},
{'$set': {'token': access_token}}
)
if not data:
db.slack_bridge.insert_one({
'team_id': team_id,
'token': access_token,
'bot_id': bot_id,
'aliases': [],
'bridges': []
})
# create task
db.tasks.insert_one({
'function': 'update_slack_team',
'kwargs': {
'team_id': team_id
}
})
return redirect(f'https://app.slack.com/client/{team_id}')
app.add_route(SlackOauth.as_view(), '/slack/oauth')
app.run(
host='0.0.0.0',
port=443,
ssl=dict(
cert=f"/etc/letsencrypt/live/{config.SLACK_REDIRECT_DOMAIN}/fullchain.pem",
key=f"/etc/letsencrypt/live/{config.SLACK_REDIRECT_DOMAIN}/privkey.pem",
)
)