|
| 1 | +from typing import Dict, Any, Optional |
| 2 | + |
1 | 3 | from starlette.requests import Request |
2 | 4 | from starlette.responses import Response |
3 | 5 |
|
|
6 | 8 | from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow |
7 | 9 |
|
8 | 10 |
|
9 | | -def to_async_bolt_request(req: Request, body: bytes) -> AsyncBoltRequest: |
10 | | - return AsyncBoltRequest( |
| 11 | +def to_async_bolt_request( |
| 12 | + req: Request, |
| 13 | + body: bytes, |
| 14 | + addition_context_properties: Optional[Dict[str, Any]] = None, |
| 15 | +) -> AsyncBoltRequest: |
| 16 | + request = AsyncBoltRequest( |
11 | 17 | body=body.decode("utf-8"), |
12 | 18 | query=req.query_params, |
13 | 19 | headers=req.headers, |
14 | 20 | ) |
| 21 | + if addition_context_properties is not None: |
| 22 | + for k, v in addition_context_properties.items(): |
| 23 | + request.context[k] = v |
| 24 | + return request |
15 | 25 |
|
16 | 26 |
|
17 | 27 | def to_starlette_response(bolt_resp: BoltResponse) -> Response: |
@@ -39,23 +49,27 @@ class AsyncSlackRequestHandler: |
39 | 49 | def __init__(self, app: AsyncApp): # type: ignore |
40 | 50 | self.app = app |
41 | 51 |
|
42 | | - async def handle(self, req: Request) -> Response: |
| 52 | + async def handle( |
| 53 | + self, req: Request, addition_context_properties: Optional[Dict[str, Any]] = None |
| 54 | + ) -> Response: |
43 | 55 | body = await req.body() |
44 | 56 | if req.method == "GET": |
45 | 57 | if self.app.oauth_flow is not None: |
46 | 58 | oauth_flow: AsyncOAuthFlow = self.app.oauth_flow |
47 | 59 | if req.url.path == oauth_flow.install_path: |
48 | 60 | bolt_resp = await oauth_flow.handle_installation( |
49 | | - to_async_bolt_request(req, body) |
| 61 | + to_async_bolt_request(req, body, addition_context_properties) |
50 | 62 | ) |
51 | 63 | return to_starlette_response(bolt_resp) |
52 | 64 | elif req.url.path == oauth_flow.redirect_uri_path: |
53 | 65 | bolt_resp = await oauth_flow.handle_callback( |
54 | | - to_async_bolt_request(req, body) |
| 66 | + to_async_bolt_request(req, body, addition_context_properties) |
55 | 67 | ) |
56 | 68 | return to_starlette_response(bolt_resp) |
57 | 69 | elif req.method == "POST": |
58 | | - bolt_resp = await self.app.async_dispatch(to_async_bolt_request(req, body)) |
| 70 | + bolt_resp = await self.app.async_dispatch( |
| 71 | + to_async_bolt_request(req, body, addition_context_properties) |
| 72 | + ) |
59 | 73 | return to_starlette_response(bolt_resp) |
60 | 74 |
|
61 | 75 | return Response( |
|
0 commit comments