Skip to content

How do I get the correct workspace team ID in message and mention handlers? #1107

@paulkuhle

Description

@paulkuhle

We have a Slack bot that is installed in several customer workspaces. Our bot can respond either to mentions using @app.event("app_mention") or to regular messages using @app.event("message"), depending on whether the customer has a separate channel for our bot or not.

In order to properly keep track of conversations and to verify customer's subscription details and so on, we need to reliably access the team ID of the workspace that the bot is running in. The main issues we've been facing are:

  • One of our clients makes the bot available through a channel that appears as an external connection in their community Slack channel. We've seen that the context_team_id field gives us the team ID we're looking for (https://api.slack.com/enterprise/org-wide-apps#context-team-id). However, this field isn't available when using the @app.event("app_mention") handler.
  • body["event"]["team"] seems to always be defined, but can return team IDs that we're not interested in (from what we've seen, when the user is a Slack Connect user from a different workspace, we'll get the user's team ID, which is different from the workspace team ID).

We currently don't have a way to reliably get the correct team ID, at least for the @app.event("app_mention") handler. Is there a different method or workaround?

Reproducible in:

@app.event("message")
async def handle_message(body, say, context, logger):
  logger.info(body)

@app.event("app_mention")
async def handle_app_mention_events(body, say, logger):
  logger.info(body)

The slack_bolt version

slack_bolt==1.19.0
slack_sdk==3.29.0
slackclient==2.9.4

Python runtime version

3.11.9

OS info

ProductName:		macOS
ProductVersion:		14.5
BuildVersion:		23F79
Darwin Kernel Version 23.5.0: Wed May  1 20:16:51 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8103

Steps to reproduce:

Register a basic Slack bot with the message/mention handlers in a workspace setup such as the one described above (external users/external connections) and try to get the team ID of the workspace that the bot is running in, not the user's team ID.

Expected result:

Get the workspace's team ID.

Actual result:

Sometimes we can only get the team ID that the user belongs to, but not the team ID of the workspace that the bot belongs to.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions