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