Skip to content

Commit

Permalink
Add support for lazy listeners when running with chalice local
Browse files Browse the repository at this point in the history
  • Loading branch information
jlujan-invitae authored and seratch committed Mar 30, 2021
1 parent 57fdb45 commit 6894239
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion slack_bolt/adapter/aws_lambda/chalice_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import logging
import json
from os import getenv

from chalice.app import Request, Response, Chalice
from chalice.config import Config
from chalice.test import (
BaseClient, LambdaContext, InvokeResponse
)

from slack_bolt.adapter.aws_lambda.chalice_lazy_listener_runner import (
ChaliceLazyListenerRunner,
Expand All @@ -13,14 +19,41 @@
from slack_bolt.response import BoltResponse


class LocalLambdaClient(BaseClient):
"""Lambda client implementing `invoke` for use when running with Chalice CLI"""
def __init__(self, app, config):
# type: (Chalice, Config) -> None
self._app = app
self._config = config

def invoke(self, FunctionName: str = None, InvocationType: str = "Event", Payload: str = None):
# type: (str, Any) -> InvokeResponse
if Payload is None:
Payload = '{}'
scoped = self._config.scope(self._config.chalice_stage, FunctionName)
lambda_context = LambdaContext(
FunctionName, memory_size=scoped.lambda_memory_size)

with self._patched_env_vars(scoped.environment_variables):
response = self._app(json.loads(Payload), lambda_context)
return InvokeResponse(payload=response)


class ChaliceSlackRequestHandler:
def __init__(self, app: App, chalice: Chalice): # type: ignore
self.app = app
self.chalice = chalice
self.logger = get_bolt_app_logger(app.name, ChaliceSlackRequestHandler)

lambda_client = None
if getenv('AWS_CHALICE_CLI_MODE') == 'true':
lambda_client = LocalLambdaClient(self.chalice, Config())

self.app.listener_runner.lazy_listener_runner = ChaliceLazyListenerRunner(
logger=self.logger
logger=self.logger,
lambda_client=lambda_client
)

if self.app.oauth_flow is not None:
self.app.oauth_flow.settings.redirect_uri_page_renderer.install_path = "?"

Expand Down

0 comments on commit 6894239

Please sign in to comment.