:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(slack-app): welcome the bot to a new channel exactly once (#62953)
Vojta Bartošf284859f75a5
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.pyindex bdcbe2e63ea3..079c471ff549 100644--- a/products/slack_app/backend/api.py+++ b/products/slack_app/backend/api.py@@ -65,7 +65,7 @@ from products.slack_app.backend.slack_link_unfurl import handle_posthog_link_unflogger = structlog.get_logger(__name__)-HANDLED_EVENT_TYPES = ["app_mention", "link_shared", "message"]+HANDLED_EVENT_TYPES = ["app_mention", "link_shared", "message", "member_joined_channel"]# The notifications Slack app (`slack`) install carries every scope the coding-agent flow# needs, so both surfaces share one kind.@@ -74,6 +74,12 @@ SLACK_INTEGRATION_KIND = "slack"# Scopes the coding-agent flow exercises end-to-end. Slack stores the granted scope set# per install, so tenants who connected the Slack integration before the full scope set# was requested in prod (2026-05-04, #57177) must reconnect before mentions can work.+#+# ``member_joined_channel`` (used by the channel-onboarding flow) additionally requires+# ``channels:read`` and ``groups:read``. Those are in the Slack app manifest but **not**+# in the required set on purpose: workspaces that connected before the scopes were added+# keep working — they just don't deliver the join event, so they silently skip the+# welcome message instead of seeing a "missing scopes" warning.POSTHOG_CODE_REQUIRED_SLACK_SCOPES: frozenset[str] = frozenset({"app_mentions:read",@@ -86,6 +92,15 @@ POSTHOG_CODE_REQUIRED_SLACK_SCOPES: frozenset[str] = frozenset(})+# Onboarding-on-join dedupe TTL: just long enough to absorb Slack retries and+# a near-simultaneous cross-region race during cutover. A real re-add after+# this window should re-onboard — most likely the person forgot how it works.+CHANNEL_ONBOARDING_DEDUPE_TTL_SECONDS = 60 * 10+CHANNEL_ONBOARDING_DOCS_URL = "https://posthog.com/docs/slack-app"+# Slack assigns a stable bot user id per install; a few hours is enough to pick+# up a reinstall while keeping ``auth.test`` traffic negligible.+SLACK_BOT_USER_ID_CACHE_TTL_SECONDS = 60 * 60 * 6+ROUTE_HANDLED_LOCALLY = "handled_locally"ROUTE_PROXIED = "proxied"ROUTE_PROXY_FAILED = "proxy_failed"@@ -1853,6 +1868,18 @@ def route_posthog_code_event_to_relevant_region(untagged_followup=untagged_followup_mapping is not None,)+ if event_type == "member_joined_channel":+ return _route_member_joined_channel(+ request,+ event,+ slack_team_id,+ proxied=proxied,+ other_domain=other_domain,+ can_defer_to_other_region=can_defer_to_other_region,+ incoming_host=incoming_host,+ is_ext_shared_channel=is_ext_shared_channel,+ )+# link_shared (unfurl) works with either integration kind.link_result = load_integrations(slack_team_id=slack_team_id, kinds=list(SLACK_INTEGRATION_KINDS))local_match = link_result.candidates[0] if link_result.candidates else None@@ -1956,6 +1983,185 @@ def _count_session_thread_messages(integration: Integration, channel: str | Nonereturn None+def _route_member_joined_channel(+ request: HttpRequest,+ event: dict[str, Any],+ slack_team_id: str,+ *,+ proxied: bool,+ other_domain: str,+ can_defer_to_other_region: bool,+ incoming_host: str,+ is_ext_shared_channel: bool,+) -> str:+ """Welcome the @PostHog bot to a new channel exactly once.++ Slack fires ``member_joined_channel`` for every user (including bot users)+ added to a channel. We only act when the joining user is our own bot, and+ we cache the first post per (workspace, channel) so retries and re-adds+ don't double-post. Externally-shared channels are dropped before any DB+ work — anyone outside the home workspace would see the welcome, and the+ first @PostHog mention there will run the existing approval flow.+ """+ joined_user = event.get("user") if isinstance(event.get("user"), str) else None+ channel_id = event.get("channel") if isinstance(event.get("channel"), str) else None+ if not joined_user or not channel_id:+ return ROUTE_HANDLED_LOCALLY++ # Drop ext-shared channels before touching the DB. Anyone outside the home+ # workspace would see the welcome message, and the ``is_ext_shared_channel``+ # flag lives on the event envelope so this costs nothing.+ if is_ext_shared_channel:+ return ROUTE_HANDLED_LOCALLY++ workspace_result = load_integrations(slack_team_id=slack_team_id, kinds=[SLACK_INTEGRATION_KIND])+ if not workspace_result.candidates:+ return _route_to_other_region_or_drop(request, slack_team_id, proxied=proxied, other_domain=other_domain)++ if _us_should_handle_instead(slack_team_id, [SLACK_INTEGRATION_KIND], can_defer_to_other_region, incoming_host):+ return _proxy_event_and_return_route(request, other_domain)++ integration = workspace_result.candidates[0]+ slack = SlackIntegration(integration)++ bot_user_id = _get_cached_bot_user_id(slack, integration)+ if bot_user_id is None or joined_user != bot_user_id:+ # We only care about our own bot joining a channel. Every other join+ # (humans, third-party bots) is ignored silently — Slack fires this+ # event for every channel-membership change, so the volume is high.+ return ROUTE_HANDLED_LOCALLY++ if not _claim_channel_onboarding(slack_team_id, channel_id):+ logger.info(+ "slack_app_channel_onboarding_skipped_duplicate",+ slack_team_id=slack_team_id,+ channel_id=channel_id,+ )+ return ROUTE_HANDLED_LOCALLY++ posted = _post_channel_onboarding_message(slack, integration, channel_id)+ if not posted:+ # Release the dedupe slot so the next delivery (retry or future re-add)+ # gets another shot rather than being silently swallowed.+ _release_channel_onboarding_claim(slack_team_id, channel_id)++ return ROUTE_HANDLED_LOCALLY+++def _bot_user_id_cache_key(integration_id: int) -> str:+ return f"slack_app:bot_user_id:v1:{integration_id}"+++def _get_cached_bot_user_id(slack: SlackIntegration, integration: Integration) -> str | None:+ cache_key = _bot_user_id_cache_key(integration.id)+ cached = cache.get(cache_key)+ if isinstance(cached, str) and cached:+ return cached+ try:+ response = slack.client.auth_test()+ bot_user_id = response.get("user_id")+ except Exception:+ logger.warning(+ "slack_app_bot_user_id_lookup_failed",+ integration_id=integration.id,+ exc_info=True,+ )+ return None+ if not isinstance(bot_user_id, str) or not bot_user_id:+ return None+ cache.set(cache_key, bot_user_id, timeout=SLACK_BOT_USER_ID_CACHE_TTL_SECONDS)+ return bot_user_id+++def _channel_onboarding_cache_key(slack_team_id: str, channel_id: str) -> str:+ return f"slack_app:channel_onboarded:v1:{slack_team_id}:{channel_id}"+++def _claim_channel_onboarding(slack_team_id: str, channel_id: str) -> bool:+ """Atomically claim the right to send the onboarding message.++ ``cache.add`` is the Django-blessed idempotency primitive: it returns True+ only if the key didn't already exist, so concurrent webhook deliveries+ (Slack retries, two-region race during cutover) can't double-post.+ """+ return bool(+ cache.add(+ _channel_onboarding_cache_key(slack_team_id, channel_id),+ True,+ timeout=CHANNEL_ONBOARDING_DEDUPE_TTL_SECONDS,+ )+ )+++def _release_channel_onboarding_claim(slack_team_id: str, channel_id: str) -> None:+ cache.delete(_channel_onboarding_cache_key(slack_team_id, channel_id))+++def _post_channel_onboarding_message(slack: SlackIntegration, integration: Integration, channel_id: str) -> bool:+ """Post the welcome message. Returns True on success."""+ blocks: list[dict[str, Any]] = [+ {+ "type": "section",+ "text": {+ "type": "mrkdwn",+ "text": (+ ":wave: Thanks for adding the PostHog app to this channel! "+ "Mention me with `@PostHog` to get started – I can answer "+ "questions about your PostHog data, research your codebase, "+ "and kick off coding tasks backed by real usage data. "+ "I'll also unfurl PostHog links you share here."+ ),+ },+ },+ {+ "type": "section",+ "text": {+ "type": "mrkdwn",+ "text": (+ "*Try one of these:*\n"+ "• `@PostHog what's our weekly active user count this month?`\n"+ "• `@PostHog open a PR that adds a unit test for src/utils.py`"+ ),+ },+ },+ {+ "type": "actions",+ "elements": [+ {+ "type": "button",+ "text": {"type": "plain_text", "text": "Read the docs"},+ "url": CHANNEL_ONBOARDING_DOCS_URL,+ }+ ],+ },+ ]++ try:+ slack.client.chat_postMessage(+ channel=channel_id,+ text="Thanks for adding the PostHog app – mention me with @PostHog to get started.",+ blocks=blocks,+ unfurl_links=False,+ unfurl_media=False,+ )+ logger.info(+ "slack_app_channel_onboarding_posted",+ integration_id=integration.id,+ slack_workspace_id=integration.integration_id,+ channel_id=channel_id,+ )+ return True+ except Exception:+ logger.warning(+ "slack_app_channel_onboarding_post_failed",+ integration_id=integration.id,+ slack_workspace_id=integration.integration_id,+ channel_id=channel_id,+ exc_info=True,+ )+ return False++def _channel_is_approved(slack_workspace_id: str, channel_id: str) -> bool:"""True iff a user in this channel has already granted PostHog approval.diff --git a/products/slack_app/backend/tests/test_channel_onboarding_routing.py b/products/slack_app/backend/tests/test_channel_onboarding_routing.pynew file mode 100644index 000000000000..a7590004f80f--- /dev/null+++ b/products/slack_app/backend/tests/test_channel_onboarding_routing.py@@ -0,0 +1,168 @@+from unittest.mock import MagicMock, patch++from django.core.cache import cache+from django.test import TestCase, override_settings+from django.test.client import RequestFactory++from posthog.models.integration import Integration+from posthog.models.organization import Organization, OrganizationMembership+from posthog.models.team.team import Team+from posthog.models.user import User++from products.slack_app.backend.api import (+ POSTHOG_CODE_REQUIRED_SLACK_SCOPES,+ ROUTE_HANDLED_LOCALLY,+ ROUTE_NO_INTEGRATION,+ _bot_user_id_cache_key,+ _channel_onboarding_cache_key,+ route_posthog_code_event_to_relevant_region,+)+++@override_settings(DEBUG=False)+class TestMemberJoinedChannelRouting(TestCase):+ """Routing-level coverage for the channel onboarding flow.++ Each test patches ``SlackIntegration`` so we can assert against the Slack+ client without standing up a real WebClient. The Slack workspace row is+ real Postgres state — only the side-effecting Slack API is mocked.+ """++ BOT_USER_ID = "U_BOT"+ SLACK_TEAM_ID = "T12345"+ CHANNEL_ID = "C_NEW_CHANNEL"++ def setUp(self):+ cache.clear()+ self.factory = RequestFactory()+ self.organization = Organization.objects.create(name="Test Org")+ self.team = Team.objects.create(organization=self.organization, name="Test Team")+ self.user = User.objects.create(email="[email protected]", distinct_id="user-1")+ OrganizationMembership.objects.create(organization=self.organization, user=self.user)+ self.integration = Integration.objects.create(+ team=self.team,+ kind="slack",+ integration_id=self.SLACK_TEAM_ID,+ config={"scope": ",".join(sorted(POSTHOG_CODE_REQUIRED_SLACK_SCOPES))},+ sensitive_config={"access_token": "xoxb-test"},+ )++ def _request(self):+ return self.factory.post("/slack/event-callback/", HTTP_HOST="us.posthog.com")++ def _event(self, *, user: str | None = None, channel: str | None = None) -> dict:+ return {+ "type": "member_joined_channel",+ "user": user if user is not None else self.BOT_USER_ID,+ "channel": channel if channel is not None else self.CHANNEL_ID,+ "channel_type": "C",+ }++ def _mock_slack(self, slack_cls_mock, *, bot_user_id: str | None = BOT_USER_ID, post_ok: bool = True):+ instance = MagicMock()+ instance.client.auth_test.return_value = {"user_id": bot_user_id}+ if post_ok:+ instance.client.chat_postMessage.return_value = {"ok": True}+ else:+ instance.client.chat_postMessage.side_effect = RuntimeError("slack down")+ slack_cls_mock.return_value = instance+ return instance++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_bot_joined_posts_onboarding_and_claims_dedupe(self, slack_cls):+ instance = self._mock_slack(slack_cls)++ result = route_posthog_code_event_to_relevant_region(self._request(), self._event(), self.SLACK_TEAM_ID)++ assert result == ROUTE_HANDLED_LOCALLY+ instance.client.chat_postMessage.assert_called_once()+ call_kwargs = instance.client.chat_postMessage.call_args.kwargs+ assert call_kwargs["channel"] == self.CHANNEL_ID+ assert any(block.get("type") == "actions" for block in call_kwargs["blocks"])+ # Dedupe slot is held so a Slack retry won't double-post.+ assert cache.get(_channel_onboarding_cache_key(self.SLACK_TEAM_ID, self.CHANNEL_ID)) is True++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_human_joined_does_not_post(self, slack_cls):+ instance = self._mock_slack(slack_cls)++ result = route_posthog_code_event_to_relevant_region(+ self._request(), self._event(user="U_HUMAN"), self.SLACK_TEAM_ID+ )++ assert result == ROUTE_HANDLED_LOCALLY+ instance.client.chat_postMessage.assert_not_called()+ assert cache.get(_channel_onboarding_cache_key(self.SLACK_TEAM_ID, self.CHANNEL_ID)) is None++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_duplicate_delivery_is_idempotent(self, slack_cls):+ instance = self._mock_slack(slack_cls)++ route_posthog_code_event_to_relevant_region(self._request(), self._event(), self.SLACK_TEAM_ID)+ route_posthog_code_event_to_relevant_region(self._request(), self._event(), self.SLACK_TEAM_ID)++ instance.client.chat_postMessage.assert_called_once()++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_ext_shared_channel_drops_silently_without_db_touch(self, slack_cls):+ """Onboarding never posts in externally-shared channels.++ The drop happens before workspace lookup, so ``SlackIntegration`` is+ never instantiated — asserting that catches future regressions that+ reorder the gate behind the DB query.+ """+ result = route_posthog_code_event_to_relevant_region(+ self._request(), self._event(), self.SLACK_TEAM_ID, is_ext_shared_channel=True+ )++ assert result == ROUTE_HANDLED_LOCALLY+ slack_cls.assert_not_called()++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_auth_test_failure_drops_silently(self, slack_cls):+ instance = MagicMock()+ instance.client.auth_test.side_effect = RuntimeError("auth down")+ slack_cls.return_value = instance++ result = route_posthog_code_event_to_relevant_region(self._request(), self._event(), self.SLACK_TEAM_ID)++ assert result == ROUTE_HANDLED_LOCALLY+ instance.client.chat_postMessage.assert_not_called()++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_post_failure_releases_dedupe_slot(self, slack_cls):+ self._mock_slack(slack_cls, post_ok=False)++ result = route_posthog_code_event_to_relevant_region(self._request(), self._event(), self.SLACK_TEAM_ID)++ assert result == ROUTE_HANDLED_LOCALLY+ # Slot released so a future retry can try again.+ assert cache.get(_channel_onboarding_cache_key(self.SLACK_TEAM_ID, self.CHANNEL_ID)) is None++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_no_workspace_integration_routes_no_integration(self, slack_cls):+ self.integration.delete()+ # Mark the request as already-proxied so the dispatcher drops instead+ # of trying to forward to the other region (which would hit the network).+ request = self.factory.post(+ "/slack/event-callback/",+ HTTP_HOST="us.posthog.com",+ headers={"X-PostHog-Region-Proxied": "1"},+ )++ result = route_posthog_code_event_to_relevant_region(request, self._event(), self.SLACK_TEAM_ID)++ assert result == ROUTE_NO_INTEGRATION+ slack_cls.assert_not_called()++ @patch("products.slack_app.backend.api.SlackIntegration")+ def test_bot_user_id_is_cached(self, slack_cls):+ instance = self._mock_slack(slack_cls)++ route_posthog_code_event_to_relevant_region(self._request(), self._event(), self.SLACK_TEAM_ID)+ # Second event in a different channel; auth.test should only have been+ # called once because the bot user id is cached per integration.+ route_posthog_code_event_to_relevant_region(self._request(), self._event(channel="C_OTHER"), self.SLACK_TEAM_ID)++ assert instance.client.auth_test.call_count == 1+ assert cache.get(_bot_user_id_cache_key(self.integration.id)) == self.BOT_USER_IDdiff --git a/products/slack_app/backend/tests/test_posthog_code_event_handler.py b/products/slack_app/backend/tests/test_posthog_code_event_handler.pyindex 86894a596260..4926c349dc07 100644--- a/products/slack_app/backend/tests/test_posthog_code_event_handler.py+++ b/products/slack_app/backend/tests/test_posthog_code_event_handler.py@@ -71,6 +71,7 @@ class TestPostHogCodeEventHandler(TestCase):("app_mention_handled_locally", "app_mention", "handled_locally", 202, True),("app_mention_proxy_failed", "app_mention", "proxy_failed", 502, True),("app_mention_no_integration", "app_mention", "no_integration", 202, True),+ ("member_joined_channel_routes", "member_joined_channel", "handled_locally", 202, True),("non_handled_event_type_skips_routing", "reaction_added", "handled_locally", 202, False),])