|
| 1 | +from datetime import datetime # type: ignore |
| 2 | +from http import HTTPStatus |
| 3 | + |
| 4 | +from falcon import version as falcon_version |
| 5 | +from falcon.asgi import Request, Response |
| 6 | +from slack_bolt import BoltResponse |
| 7 | +from slack_bolt.async_app import AsyncApp |
| 8 | +from slack_bolt.error import BoltError |
| 9 | +from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow |
| 10 | +from slack_bolt.request.async_request import AsyncBoltRequest |
| 11 | + |
| 12 | + |
| 13 | +class AsyncSlackAppResource: |
| 14 | + """ |
| 15 | + For use with ASGI Falcon Apps. |
| 16 | +
|
| 17 | + from slack_bolt.async_app import AsyncApp |
| 18 | + app = AsyncApp() |
| 19 | +
|
| 20 | + import falcon |
| 21 | + app = falcon.asgi.App() |
| 22 | + app.add_route("/slack/events", AsyncSlackAppResource(app)) |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, app: AsyncApp): # type: ignore |
| 26 | + if falcon_version.__version__.startswith("2."): |
| 27 | + raise BoltError("This ASGI compatible adapter requires Falcon version >= 3.0") |
| 28 | + |
| 29 | + self.app = app |
| 30 | + |
| 31 | + async def on_get(self, req: Request, resp: Response): |
| 32 | + if self.app.oauth_flow is not None: |
| 33 | + oauth_flow: AsyncOAuthFlow = self.app.oauth_flow |
| 34 | + if req.path == oauth_flow.install_path: |
| 35 | + bolt_resp = await oauth_flow.handle_installation( |
| 36 | + await self._to_bolt_request(req) |
| 37 | + ) |
| 38 | + await self._write_response(bolt_resp, resp) |
| 39 | + return |
| 40 | + elif req.path == oauth_flow.redirect_uri_path: |
| 41 | + bolt_resp = await oauth_flow.handle_callback( |
| 42 | + await self._to_bolt_request(req) |
| 43 | + ) |
| 44 | + await self._write_response(bolt_resp, resp) |
| 45 | + return |
| 46 | + |
| 47 | + resp.status = "404" |
| 48 | + resp.body = "The page is not found..." |
| 49 | + |
| 50 | + async def on_post(self, req: Request, resp: Response): |
| 51 | + bolt_req = await self._to_bolt_request(req) |
| 52 | + bolt_resp = await self.app.async_dispatch(bolt_req) |
| 53 | + await self._write_response(bolt_resp, resp) |
| 54 | + |
| 55 | + async def _to_bolt_request(self, req: Request) -> AsyncBoltRequest: |
| 56 | + return AsyncBoltRequest( |
| 57 | + body=(await req.stream.read(req.content_length or 0)).decode("utf-8"), |
| 58 | + query=req.query_string, |
| 59 | + headers={k.lower(): v for k, v in req.headers.items()}, |
| 60 | + ) |
| 61 | + |
| 62 | + async def _write_response(self, bolt_resp: BoltResponse, resp: Response): |
| 63 | + resp.text = bolt_resp.body |
| 64 | + status = HTTPStatus(bolt_resp.status) |
| 65 | + resp.status = str(f"{status.value} {status.phrase}") |
| 66 | + resp.set_headers(bolt_resp.first_headers_without_set_cookie()) |
| 67 | + for cookie in bolt_resp.cookies(): |
| 68 | + for name, c in cookie.items(): |
| 69 | + expire_value = c.get("expires") |
| 70 | + expire = ( |
| 71 | + datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z") |
| 72 | + if expire_value |
| 73 | + else None |
| 74 | + ) |
| 75 | + resp.set_cookie( |
| 76 | + name=name, |
| 77 | + value=c.value, |
| 78 | + expires=expire, |
| 79 | + max_age=c.get("max-age"), |
| 80 | + domain=c.get("domain"), |
| 81 | + path=c.get("path"), |
| 82 | + secure=True, |
| 83 | + http_only=True, |
| 84 | + ) |
0 commit comments