bolt-python icon indicating copy to clipboard operation
bolt-python copied to clipboard

Can't initialize AsyncApp with custom AsyncInstallationStore and AsyncOAuthStore

Open vstratful opened this issue 3 years ago • 1 comments

Hey,

I'm attempting to follow the example provided in https://github.com/slackapi/bolt-python/blob/main/examples/sqlalchemy/async_oauth_app.py to create my own AsyncInstallationStore and AsyncOAuthStore backed by Prisma Python Client.

I have the following variables in my .env file SLACK_CLIENT_ID, SLACK_CLIENT_SECRET, SLACK_SIGNING_SECRET.

I'm preparing my secrets, Prisma instance, and logger like so:

# Load secrets
client_id, client_secret, signing_secret = (
    os.getenv("SLACK_CLIENT_ID"),
    os.getenv("SLACK_CLIENT_SECRET"),
    os.getenv("SLACK_SIGNING_SECRET"),
)

# Create logger
logging.basicConfig(
    filename="/tmp/workwise-debug.log", encoding="utf-8", level=logging.DEBUG
)
logger = logging.getLogger(__name__)

# Create Prisma instance
prisma = Prisma()
asyncio.run(prisma.connect())

Then I'm preparing my AsyncPrismaInstallationStore, AsyncPrismaOAuthStateStore and AsyncOAuthSettings like so:

# Create AsyncPrismaInstallationStore instance
installation_store = AsyncPrismaInstallationStore(
    client_id=client_id, prisma=prisma, logger=logger
)

# Create AsyncPrismaOAuthStateStore instance
oauth_state_store = AsyncPrismaOAuthStateStore(
    expiration_seconds=120, prisma=prisma, logger=logger
)

# Create OAuth settings
oauth_settings = AsyncOAuthSettings(
    client_id=client_id,
    client_secret=client_secret,
    state_store=oauth_state_store,
)

Finally, I'm creating an instance of AsyncApp:

# Create slack AsyncApp instance
slack = AsyncApp(
    logger=logger,
    signing_secret=signing_secret,
    installation_store=installation_store,
    oauth_settings=oauth_settings,
)

I believe I've followed the example accurately, but when I start the app, I see this logged:

INFO:slack_bolt.AsyncApp:As you've set SLACK_CLIENT_ID and SLACK_CLIENT_SECRET env variables, Bolt has enabled the file-based InstallationStore/OAuthStateStore for you. Note that these file-based stores are for local development. If you'd like to use a different data store, set the oauth_settings argument in the App constructor. Please refer to https://slack.dev/bolt-python/concepts#authenticating-oauth for more details.

If I rename the variables to CLIENT_ID. CLIENT_SECRET and SIGNING_SECRET, the app fails to start with the following message:

slack_bolt.error.BoltError: Either an env variable `SLACK_BOT_TOKEN` or `token` argument in the constructor is required.

Expected behaviour: The AsyncApp instance should use the settings provided, as I've passed in both an installation_store and an oauth_settings object to the AsyncApp constructor.

vstratful avatar Sep 07 '23 13:09 vstratful

I think I've solved the issue.

I added self._framework_logger.info(f"Got here: {oauth_settings}") to AsyncApp.__init__ at line 207, and I see in the logs that this object is being initialized twice.

The problem was my own creation, I was setting up the AsyncApp in a separate file, then importing it into main.py before finally calling slack.start(). My settings are applied correctly on import, but the slack.start() call then re-initializes everything with the defaults.

Apologies for opening unnecessary issues.

vstratful avatar Sep 07 '23 14:09 vstratful