-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_run.py
56 lines (41 loc) · 1.72 KB
/
local_run.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
from __future__ import annotations
from dotenv import load_dotenv
load_dotenv()
from flask import Flask
from flask import request
from slack_bolt import BoltRequest
from slack_bolt.app import App
from slack_bolt.oauth import OAuthFlow
from lambda_handler import bolt_app
def run(app: App):
@app.use
def print_request(request: BoltRequest, next, logger):
logger.info(f'Request body: {request.body}')
next()
from flask import Request, Response, make_response
from slack_bolt.adapter.flask.handler import to_flask_response, to_bolt_request
class LocalFlaskAppHandler:
def __init__(self, app: App): # type: ignore
self.app = app
def handle(self, req: Request) -> Response:
if req.method == 'GET':
if self.app.oauth_flow is not None:
oauth_flow: OAuthFlow = self.app.oauth_flow
if 'code' in req.args or 'error' in req.args:
bolt_resp = oauth_flow.handle_callback(to_bolt_request(req))
return to_flask_response(bolt_resp)
else:
bolt_resp = oauth_flow.handle_installation(to_bolt_request(req))
return to_flask_response(bolt_resp)
elif req.method == 'POST':
bolt_resp = self.app.dispatch(to_bolt_request(req))
return to_flask_response(bolt_resp)
return make_response('Not Found', 404)
flask_app = Flask(__name__)
handler = LocalFlaskAppHandler(app)
# Run server locally: http://localhost:5000
@flask_app.route('/', methods=['GET', 'POST'])
def slack_events():
return handler.handle(request)
flask_app.run()
run(bolt_app)