question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for "sub apps"

See original GitHub issue

When creating a large slack application it would be nice to be able to reuse components in a more pythonic way than what is possible right now. A workaround is described in issue #236, but it requires passing the app object around and manually wiring up things by registering and calling decorators as functions directly.

I suggest adding a feature much like include_router in FastAPI or add_typer in Typer.

This feature will let us create sub-apps in separate files or even separate packages, like plugins that can be published on pypi.org separately.

Here is a small example illustrating what I mean:

main.py

Here is our main app, where we add a plugin from a separate package

import os
from slack_bolt import App

from my_plugin import my_plugin


# Initializes your app with your bot token and signing secret
app = App(
    token=os.environ.get("SLACK_BOT_TOKEN"),
    signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
app.add_app(my_plugin)

my_plugin.py

A simple plugin that just creates a slack_bolt app, but without any config

from slack_bolt import App


my_plugin = App()

@my_plugin.message("hello")
def message_hello(message, say):
    say("Hello world")

Category

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
davidolrikcommented, Apr 30, 2021

My main goal was to facilitate better code reuse, but most importantly better organization of large complex slack apps while still being able to use the @app.* decorators.

Maybe a kind of proxy sub app object would make sense then, and app.add_sub_app(my_plugin) could then transfer what ever was defined on the sub app to the main app.

from slack_bolt import SubApp


my_plugin = SubApp()

@my_plugin.message("hello")
def message_hello(message, say):
    say("Hello world")

The important part is that the SubApp should be able to use all the decorators from App.

1reaction
jianyuancommented, Jun 3, 2022

Here’s what I came up with:

class SubApp:
    def __init__(self) -> None:
        self.actions = []
        self.events = []
        self.views = []

    def action(self, *args, **kwargs):
        def __call__(func):
            self.actions.append((args, kwargs, func))
            return func

        return __call__

    def event(self, *args, **kwargs):
        def __call__(func):
            self.events.append((args, kwargs, func))
            return func

        return __call__

    def view(self, *args, **kwargs):
        def __call__(func):
            self.views.append((args, kwargs, func))
            return func

        return __call__

    def register_to(self, app: App):
        for args, kwargs, func in self.actions:
            app.action(*args, **kwargs)(func)
        for args, kwargs, func in self.events:
            app.event(*args, **kwargs)(func)
        for args, kwargs, func in self.views:
            app.view(*args, **kwargs)(func)

Usage:

sub_app = SubApp()

@sub_app.event("app_mention")
def handle_app_mentions(logger, event, say):
    logger.info(event)
    say(f"Hi there, <@{event['user']}>")

# Then...

app = App(...)
sub_app.register_to(app)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sub Apps - Bubble Docs
The feature can be found in Settings > Sub apps. There, you can create a sub app using the current app as the...
Read more >
Add an app to Microsoft Teams
Add from Apps​​ On the left side of Teams select Apps and search for your favorite app or browse the app categories to...
Read more >
Add Sub K-1 4+ - App Store
Updated to support iOS12 - thank you for your patience. • A few other minor tweaks. • As time allows, I'd enjoy hearing...
Read more >
'Desktop PWA Sub Apps' could help users install multiple ...
A new potential Chrome flag discovered by Chrome Story called 'Desktop PWA Sub Apps' may enable installed web applications to create ...
Read more >
Add developer account users and manage permissions
Choose what permissions to apply to specific apps using the App permissions tab, or what permissions to apply to all apps in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found