: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(data-warehouse): make slack channel tables webhook-only (#60857)
Marcus Hofe77c613613af
diff --git a/posthog/temporal/data_imports/sources/slack/slack.py b/posthog/temporal/data_imports/sources/slack/slack.pyindex a7b06877ca9f..21ad0f66a87d 100644--- a/posthog/temporal/data_imports/sources/slack/slack.py+++ b/posthog/temporal/data_imports/sources/slack/slack.py@@ -421,32 +421,27 @@ def slack_source(resource = rest_api_resource(config, team_id, job_id, None)items = lambda: resourceelse:- # Per-channel message endpoint+ # Per-channel message endpoint.+ #+ # Slack channel tables are webhook-only: we deliberately skip the historical+ # `conversations.history` / `conversations.replies` backfill. That backfill makes one+ # `conversations.replies` request per threaded message, which fans out into tens of+ # thousands of rate-limited requests on workspaces with many channels and threads.+ # Passing `skip_initial_sync_complete_check=True` activates webhook mode from the very+ # first sync (the same pattern the Customer.io webhook tables use). Until the webhook is+ # configured and delivering events, the table stays empty rather than backfilling history.endpoint_config = messages_endpoint_config()sort_mode = "desc"if channel_id is None:raise Exception(f"channel_not_found: {endpoint}")- webhook_enabled = async_to_sync(webhook_source_manager.webhook_enabled)()-- oldest_ts: str | None = None- if should_use_incremental_field and db_incremental_field_last_value is not None:- # Known limitation: incremental polling only fetches thread replies for parent messages- # returned by conversations.history in this window. Replies added to older parent threads- # (parent ts < oldest_ts) are intentionally not captured here and are expected to be- # addressed by webhook sources.- oldest_ts = str(db_incremental_field_last_value.timestamp())-- resolved_id = channel_id- resolved_oldest_ts = oldest_ts+ webhook_enabled = async_to_sync(webhook_source_manager.webhook_enabled)(True)def channel_items() -> Iterable[Any] | AsyncIterable[Any]:if webhook_enabled:return webhook_source_manager.get_items(table_transformer=_webhook_table_transformer)- return _channel_messages_generator(- access_token, resolved_id, resumable_source_manager, oldest_ts=resolved_oldest_ts- )+ return iter([])items = channel_itemsdiff --git a/posthog/temporal/data_imports/sources/slack/source.py b/posthog/temporal/data_imports/sources/slack/source.pyindex 1d55b6533079..a07ebcf2735f 100644--- a/posthog/temporal/data_imports/sources/slack/source.py+++ b/posthog/temporal/data_imports/sources/slack/source.py@@ -24,12 +24,9 @@ from posthog.temporal.data_imports.sources.common.mixins import OAuthMixinfrom posthog.temporal.data_imports.sources.common.registry import SourceRegistryfrom posthog.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom posthog.temporal.data_imports.sources.common.schema import SourceSchema-from posthog.temporal.data_imports.sources.common.webhook_s3 import (- WebhookSourceManager,- is_webhook_feature_flag_enabled,-)+from posthog.temporal.data_imports.sources.common.webhook_s3 import WebhookSourceManagerfrom posthog.temporal.data_imports.sources.generated_configs import SlackSourceConfig-from posthog.temporal.data_imports.sources.slack.settings import ENDPOINTS, messages_endpoint_config+from posthog.temporal.data_imports.sources.slack.settings import ENDPOINTSfrom posthog.temporal.data_imports.sources.slack.slack import (SlackResumeConfig,get_channels,@@ -187,21 +184,23 @@ class SlackSource(ResumableSource[SlackSourceConfig, SlackResumeConfig], Webhookif not access_token:raise ValueError("Slack access token not found")- msg_config = messages_endpoint_config()- webhook_flag_enabled = is_webhook_feature_flag_enabled(team_id)authed_user = self._get_authed_user_id(integration)channels = get_channels(integration.id, access_token, authed_user, force_refresh=force_refresh)for ch in channels:if ch["id"] in ENDPOINTS:continue+ # Channel message tables are webhook-only: messages arrive via the realtime webhook+ # pipeline, not the polling sync, so incremental/append don't apply and full-refresh+ # would only delete data and reload nothing. Webhook is the only sync method we offer+ # (mirrors the Customer.io webhook schemas).schemas.append(SourceSchema(name=ch["id"],label=ch["name"],- supports_incremental=len(msg_config.incremental_fields) > 0,- supports_webhooks=webhook_flag_enabled,- supports_append=len(msg_config.incremental_fields) > 0,- incremental_fields=msg_config.incremental_fields,+ supports_incremental=False,+ supports_append=False,+ supports_webhooks=True,+ incremental_fields=[],))diff --git a/posthog/temporal/data_imports/sources/slack/test_slack.py b/posthog/temporal/data_imports/sources/slack/test_slack.pyindex d5f04041df74..10d1f0796fbc 100644--- a/posthog/temporal/data_imports/sources/slack/test_slack.py+++ b/posthog/temporal/data_imports/sources/slack/test_slack.py@@ -1,6 +1,6 @@from typing import Any-from unittest.mock import MagicMock, patch+from unittest.mock import AsyncMock, MagicMock, patchfrom django.core.cache import cache as django_cache@@ -373,10 +373,6 @@ class TestSlackSourceGetSchemasForceRefresh:"posthog.temporal.data_imports.sources.slack.slack._fetch_all_channels",side_effect=[[{"id": "C1", "name": "general"}], [{"id": "C2", "name": "renamed"}]],) as mock_fetch,- patch(- "posthog.temporal.data_imports.sources.slack.source.is_webhook_feature_flag_enabled",- return_value=False,- ),):first = source.get_schemas(config, team_id=1)cached = source.get_schemas(config, team_id=1)@@ -393,6 +389,29 @@ class TestSlackSourceGetSchemasForceRefresh:assert channel_names(cached) == {"C1"}assert channel_names(forced) == {"C2"}+ def test_channel_schemas_are_webhook_only(self) -> None:+ from posthog.temporal.data_imports.sources.slack.source import SlackSource++ config, integration = self._build_mocks()+ source = SlackSource()++ with (+ patch.object(source, "get_oauth_integration", return_value=integration),+ patch(+ "posthog.temporal.data_imports.sources.slack.slack._fetch_all_channels",+ return_value=[{"id": "C1", "name": "general"}],+ ),+ ):+ schemas = source.get_schemas(config, team_id=1)++ channel = next(s for s in schemas if s.name == "C1")+ # Webhook is the only valid sync method: full-refresh would wipe the table and+ # reload nothing, incremental/append have no polling endpoint to read from.+ assert channel.supports_webhooks is True+ assert channel.supports_incremental is False+ assert channel.supports_append is False+ assert channel.incremental_fields == []+class TestSlackSourceChannelsEndpoint:def setup_method(self) -> None:@@ -442,3 +461,45 @@ class TestSlackSourceChannelsEndpoint:assert items == samplemock_fetch.assert_called_once_with("token", authed_user)+++class TestSlackSourceChannelMessagesWebhookOnly:+ def _build_channel_source(self, webhook_manager: Any) -> Any:+ return slack_source(+ access_token="token",+ integration_id=42,+ endpoint="C123",+ team_id=1,+ job_id="job-1",+ webhook_source_manager=webhook_manager,+ resumable_source_manager=MagicMock(spec=ResumableSourceManager),+ channel_id="C123",+ )++ @parameterized.expand(+ [+ ("webhook_enabled", True, ["webhook-item"], True),+ ("webhook_disabled", False, [], False),+ ]+ )+ def test_channel_messages_are_webhook_only_and_never_backfill(+ self,+ _name: str,+ webhook_on: bool,+ expected_items: list[Any],+ expect_get_items_called: bool,+ ) -> None:+ manager = MagicMock(spec=WebhookSourceManager)+ manager.webhook_enabled = AsyncMock(return_value=webhook_on)+ manager.get_items.return_value = ["webhook-item"]++ with patch("posthog.temporal.data_imports.sources.slack.slack._channel_messages_generator") as mock_backfill:+ response = self._build_channel_source(manager)+ items = list(response.items())++ # Webhook mode is activated from the first sync (skip the initial-sync-complete gate).+ # When disabled the table stays empty until webhook events arrive — never a historical backfill.+ manager.webhook_enabled.assert_awaited_once_with(True)+ assert items == expected_items+ assert manager.get_items.called == expect_get_items_called+ mock_backfill.assert_not_called()