Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyramid example without side effects #793

Closed
1 of 4 tasks
dz0ny opened this issue Dec 19, 2022 · 2 comments · Fixed by #794
Closed
1 of 4 tasks

Pyramid example without side effects #793

dz0ny opened this issue Dec 19, 2022 · 2 comments · Fixed by #794
Labels
area:adapter docs Improvements or additions to documentation enhancement New feature or request
Milestone

Comments

@dz0ny
Copy link

dz0ny commented Dec 19, 2022

The Pyramid example shown in the docs is mostly for taste. The problem is that Pyramid unlike other frameworks uses thread-locals and using an example as it is, causes numerous side effects and makes it unusable for example if you want to use the database.

The following is an example that will not cause side effects, shows database use and can be used with integration tests.

"""Slack handlers."""

from pyramid.config import Configurator
from pyramid.request import Request
from pyramid.response import Response
from slack_bolt import App
from slack_bolt.adapter.pyramid.handler import to_bolt_request
from slack_bolt.adapter.pyramid.handler import to_pyramid_response
from models import Team


def app_uninstalled(event, client, context: dict) -> None:
    """Process Slack app_uninstalled event."""

    team_id: str = context["team_id"]
    request: Request = context["request"]

    teams = (
        request.db.query(Team)
        .filter(Team.slack_id == team_id)
        .all()
    )

    for team in teams:
        team.delete()
    request.db.flush()


class SlackRequestHandler:
    """Integration layer for Slack Bot SDK."""

    def __init__(self, settings: dict):  # type: ignore
        self.app = App(
            token=settings.get("slack.bot_token"),
            signing_secret=settings.get("slack.bot_signing_secret"),
        )

    def handle(self, request: Request) -> Response:
        bolt_req = to_bolt_request(request)
        bolt_req.context["request"] = request
        bolt_resp = self.app.dispatch(bolt_req)
        return to_pyramid_response(bolt_resp)


def includeme(config: Configurator) -> None:
    """Pyramid knob."""

    slack = SlackRequestHandler(config.registry.settings)

    # Register handlers
    slack.app.event("app_uninstalled")(app_uninstalled)

    # Add API routes for Slack
    config.add_view(slack.handle, route_name="slack.events", request_method="POST")
    config.add_route("slack.events", "/slack/events")

Category (place an x in each of the [ ])

  • slack_bolt.App and/or its core components
  • slack_bolt.async_app.AsyncApp and/or its core components
  • Adapters in slack_bolt.adapter
  • Others

Requirements

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

seratch added a commit to seratch/bolt-python that referenced this issue Dec 19, 2022
@seratch seratch added docs Improvements or additions to documentation enhancement New feature or request area:adapter labels Dec 19, 2022
@seratch seratch added this to the 1.16.1 milestone Dec 19, 2022
@seratch
Copy link
Member

seratch commented Dec 19, 2022

Hi @dz0ny, thanks for sharing this!

Instead of updating the simple example that we have under examples/, I'm thinking of enhancing the adapter side: #794 Does this work for you? If you don't like the context property name "pyramid_request", please go ahead with your own adapter. It's safe enough to use your own one as the adapter layer is simple and stable enough.

@seratch
Copy link
Member

seratch commented Dec 19, 2022

Thanks for checking the PR! We will release a new version soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:adapter docs Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants