|
| 1 | +from typing import Any, Callable, Dict, Iterable, List, Tuple |
| 2 | + |
| 3 | +from slack_bolt import App |
| 4 | +from slack_bolt.adapter.wsgi.http_request import WsgiHttpRequest |
| 5 | +from slack_bolt.adapter.wsgi.http_response import WsgiHttpResponse |
| 6 | +from slack_bolt.oauth.oauth_flow import OAuthFlow |
| 7 | +from slack_bolt.request import BoltRequest |
| 8 | +from slack_bolt.response import BoltResponse |
| 9 | + |
| 10 | + |
| 11 | +class SlackRequestHandler: |
| 12 | + def __init__(self, app: App, path: str = "/slack/events"): |
| 13 | + """Setup Bolt as a WSGI web framework, this will make your application compatible with WSGI web servers. |
| 14 | + This can be used for production deployments. |
| 15 | +
|
| 16 | + With the default settings, `http://localhost:3000/slack/events` |
| 17 | + Run Bolt with [gunicorn](https://gunicorn.org/) |
| 18 | +
|
| 19 | + # Python |
| 20 | + app = App() |
| 21 | +
|
| 22 | + api = SlackRequestHandler(app) |
| 23 | +
|
| 24 | + # bash |
| 25 | + export SLACK_SIGNING_SECRET=*** |
| 26 | +
|
| 27 | + export SLACK_BOT_TOKEN=xoxb-*** |
| 28 | +
|
| 29 | + gunicorn app:api -b 0.0.0.0:3000 --log-level debug |
| 30 | +
|
| 31 | + Args: |
| 32 | + app: Your bolt application |
| 33 | + path: The path to handle request from Slack (Default: `/slack/events`) |
| 34 | + """ |
| 35 | + self.path = path |
| 36 | + self.app = app |
| 37 | + |
| 38 | + def dispatch(self, request: WsgiHttpRequest) -> BoltResponse: |
| 39 | + return self.app.dispatch( |
| 40 | + BoltRequest(body=request.get_body(), query=request.query_string, headers=request.get_headers()) |
| 41 | + ) |
| 42 | + |
| 43 | + def handle_installation(self, request: WsgiHttpRequest) -> BoltResponse: |
| 44 | + oauth_flow: OAuthFlow = self.app.oauth_flow |
| 45 | + return oauth_flow.handle_installation( |
| 46 | + BoltRequest(body=request.get_body(), query=request.query_string, headers=request.get_headers()) |
| 47 | + ) |
| 48 | + |
| 49 | + def handle_callback(self, request: WsgiHttpRequest) -> BoltResponse: |
| 50 | + oauth_flow: OAuthFlow = self.app.oauth_flow |
| 51 | + return oauth_flow.handle_callback( |
| 52 | + BoltRequest(body=request.get_body(), query=request.query_string, headers=request.get_headers()) |
| 53 | + ) |
| 54 | + |
| 55 | + def _get_http_response(self, request: WsgiHttpRequest) -> WsgiHttpResponse: |
| 56 | + if request.method == "GET": |
| 57 | + if self.app.oauth_flow is not None: |
| 58 | + if request.path == self.app.oauth_flow.install_path: |
| 59 | + bolt_response: BoltResponse = self.handle_installation(request) |
| 60 | + return WsgiHttpResponse( |
| 61 | + status=bolt_response.status, headers=bolt_response.headers, body=bolt_response.body |
| 62 | + ) |
| 63 | + if request.path == self.app.oauth_flow.redirect_uri_path: |
| 64 | + bolt_response: BoltResponse = self.handle_callback(request) |
| 65 | + return WsgiHttpResponse( |
| 66 | + status=bolt_response.status, headers=bolt_response.headers, body=bolt_response.body |
| 67 | + ) |
| 68 | + if request.method == "POST" and request.path == self.path: |
| 69 | + bolt_response: BoltResponse = self.dispatch(request) |
| 70 | + return WsgiHttpResponse(status=bolt_response.status, headers=bolt_response.headers, body=bolt_response.body) |
| 71 | + return WsgiHttpResponse(status=404, headers={"content-type": ["text/plain;charset=utf-8"]}, body="Not Found") |
| 72 | + |
| 73 | + def __call__( |
| 74 | + self, |
| 75 | + environ: Dict[str, Any], |
| 76 | + start_response: Callable[[str, List[Tuple[str, str]]], None], |
| 77 | + ) -> Iterable[bytes]: |
| 78 | + request = WsgiHttpRequest(environ) |
| 79 | + if "HTTP" in request.protocol: |
| 80 | + response: WsgiHttpResponse = self._get_http_response( |
| 81 | + request=request, |
| 82 | + ) |
| 83 | + start_response(response.status, response.get_headers()) |
| 84 | + return response.get_body() |
| 85 | + raise TypeError(f"Unsupported SERVER_PROTOCOL: {request.protocol}") |
0 commit comments