Skip to content

refactor: register listeners with decorators and load them automatically - #95

Draft
WilliamBergamin wants to merge 9 commits into
mainfrom
feat-decorator-listeners
Draft

WilliamBergamin wants to merge 9 commits into
mainfrom
feat-decorator-listeners

Conversation

@WilliamBergamin

@WilliamBergamin WilliamBergamin commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request lets each listener register itself with a decorator, so adding a listener is just adding a file.

# listeners/events/app_home_opened.py
from listeners import app


@app.event("app_home_opened")
def app_home_opened_callback(client, event, logger): ...
  • app.py creates the app and calls load_listeners(app), which shares it as listeners.app and imports every module under listeners/.
  • The register() functions in each listeners/*/__init__.py are removed.
  • app_oauth.py works the same way. To use OAuth, delete app.py and rename app_oauth.py to app.py.

Testing

  1. Set SLACK_BOT_TOKEN and SLACK_APP_TOKEN, run slack run, and confirm the App Home tab, /sample-command, the "hi" message, the shortcut, and the modal all still work.
  2. Delete app.py, rename app_oauth.py to app.py, set SLACK_CLIENT_ID, SLACK_CLIENT_SECRET, and SLACK_SIGNING_SECRET, then run python3 app.py behind ngrok http 3000. Install through /slack/install and confirm the listeners respond.

🤖 Generated with Claude Code

WilliamBergamin and others added 9 commits October 20, 2026 16:49
Each listener now decorates its own callback with @app.<kind>(...) and
bolt_app.py imports every module under listeners/ with walk_packages, so
adding a listener means adding a file instead of editing register()
functions. bolt_app.py owns the App for both Socket Mode and OAuth: the
OAuth settings turn on when SLACK_CLIENT_ID is set, and app_oauth.py is
now a thin starter you rename to app.py. Tests turn off token
verification through SLACK_TOKEN_VERIFICATION_ENABLED.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Each entry file builds its own App again and passes it to
load_listeners, which sets listeners.app before importing the listener
modules. Listeners decorate with `from listeners import app`, and since
the listeners package is never __main__ there is one App whether the
app starts from app.py, slack run, or app_oauth.py. Switching to OAuth
is now: delete app.py and rename app_oauth.py to app.py. This drops
bolt_app.py, the env-switched OAuth settings, and the token
verification flag; tests build their own App in conftest.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
… a plain walker

The entry file now hands its App over with `listeners.app = app` before
calling `listeners.load_listeners()`, so load_listeners only walks the
package and the happy path reads as two plain lines.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
listeners/__init__.py now builds the default App from SLACK_BOT_TOKEN,
so app.py only imports app and load_listeners. Token verification is
deferred to the first request so importing listeners makes no API
call. app_oauth.py overrides listeners.app with its OAuth app before
loading the listeners, and tests set SLACK_BOT_TOKEN in
pytest_configure.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
This reverts commit bd0bd06. The App belongs in the entry file, so
app.py builds it with token verification on and hands it to the
listeners with `listeners.app = app`.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
…ation

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
…isteners

Listener modules no longer import a shared listeners.app that app.py must set
first. Decorators record onto a registry, and load_listeners(app) applies them,
so any number of App instances can load the same listeners (for example in tests).

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
load_listeners(app) stores the app as listeners.app and then imports every
listener module, so each listener file imports the app and decorates its
callback directly. Drops the registry, which added more indirection than the
starter template needs.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
The listener and OAuth sections on main stay accurate with decorator
listeners, and app_oauth.py still runs on its own.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant