: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
fix(attentive): say where to grant the missing Webhooks permission (#100258)
Tom Owers8b81ef0720e0
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/api_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/api_client.pyindex 7fdf0dc123ea..cdb216a5cbcc 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/api_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/api_client.py@@ -28,6 +28,21 @@ LOGGER = structlog.get_logger(__name__)WEBHOOKS_URL = f"{ATTENTIVE_V1_BASE_URL}/webhooks"REQUEST_TIMEOUT_SECONDS = 30+API_KEY_REJECTED_ERROR = (+ "Attentive rejected your API key. Create a private app under Marketplace > Create app in "+ "Attentive and reconnect with its API key."+)+WEBHOOKS_PERMISSION_ERROR = (+ "Your Attentive private app doesn't have the Webhooks permission. Grant it to the app under "+ "Marketplace in Attentive, then reconnect."+)+RATE_LIMITED_ERROR = "Attentive rate-limited the request. Wait a few seconds, then try again."+REQUEST_REJECTED_ERROR = (+ "Attentive rejected the request. Check the API key and its permissions under Marketplace in "+ "Attentive, then reconnect."+)+UNREACHABLE_ERROR = "Could not reach Attentive. Check your network, then try again."+def _session(api_key: str) -> requests.Session:return make_tracked_session(@@ -51,16 +66,16 @@ def _events_for_resources(resource_names: list[str]) -> list[str]:def _format_http_error(error: requests.HTTPError) -> str:status_code = error.response.status_code+ # The customer-facing strings below omit the status, so log it here for every rejection rather+ # than only the unrecognized ones.+ LOGGER.warning("Attentive rejected the request", status_code=status_code)if status_code == 401:- return (- "Attentive rejected the API key (401). Create a private app under Marketplace > "- "Create app in Attentive and use its API key."- )+ return API_KEY_REJECTED_ERRORif status_code == 403:- return "Attentive denied the request (403). Make sure the private app has the Webhooks permission."+ return WEBHOOKS_PERMISSION_ERRORif status_code == 429:- return "Attentive rate-limited the request (429). Try again in a few seconds."- return f"Attentive API error ({status_code})."+ return RATE_LIMITED_ERROR+ return REQUEST_REJECTED_ERRORdef validate_credentials(api_key: str) -> tuple[bool, str | None]:@@ -73,7 +88,8 @@ def validate_credentials(api_key: str) -> tuple[bool, str | None]:except requests.HTTPError as e:return False, _format_http_error(e)except requests.RequestException as e:- return False, f"Could not reach Attentive: {e}"+ LOGGER.warning("Could not reach Attentive to validate the API key", error=str(e))+ return False, UNREACHABLE_ERRORreturn True, None@@ -144,7 +160,7 @@ def create_webhook(api_key: str, webhook_url: str, resource_names: list[str]) ->return WebhookCreationResult(success=False, error=_format_http_error(e))except requests.RequestException as e:LOGGER.warning("Could not reach Attentive to create webhook", error=str(e))- return WebhookCreationResult(success=False, error=f"Could not reach Attentive: {e}")+ return WebhookCreationResult(success=False, error=UNREACHABLE_ERROR)# Attentive does NOT return the signing key in the create response — the# user has to copy it from the webhook's settings in the Attentive UI.@@ -172,7 +188,7 @@ def enable_webhook(api_key: str, webhook_url: str) -> tuple[bool, str | None]:return False, _format_http_error(e)except requests.RequestException as e:LOGGER.warning("Could not reach Attentive to enable webhook", error=str(e))- return False, f"Could not reach Attentive: {e}"+ return False, UNREACHABLE_ERRORreturn True, None@@ -208,7 +224,7 @@ def sync_webhook_events(api_key: str, webhook_url: str, resource_names: list[strreturn WebhookSyncResult(success=False, error=_format_http_error(e))except requests.RequestException as e:LOGGER.warning("Could not reach Attentive to sync webhook events", error=str(e))- return WebhookSyncResult(success=False, error=f"Could not reach Attentive: {e}")+ return WebhookSyncResult(success=False, error=UNREACHABLE_ERROR)return WebhookSyncResult(success=True)@@ -234,7 +250,7 @@ def delete_webhook(api_key: str, webhook_url: str) -> WebhookDeletionResult:return WebhookDeletionResult(success=False, error=_format_http_error(e))except requests.RequestException as e:LOGGER.warning("Could not reach Attentive to delete webhook", error=str(e))- return WebhookDeletionResult(success=False, error=f"Could not reach Attentive: {e}")+ return WebhookDeletionResult(success=False, error=UNREACHABLE_ERROR)return WebhookDeletionResult(success=True)@@ -245,7 +261,8 @@ def get_external_webhook_info(api_key: str, webhook_url: str) -> ExternalWebhookexcept requests.HTTPError as e:return ExternalWebhookInfo(exists=False, error=_format_http_error(e))except requests.RequestException as e:- return ExternalWebhookInfo(exists=False, error=f"Could not reach Attentive: {e}")+ LOGGER.warning("Could not reach Attentive to read the webhook", error=str(e))+ return ExternalWebhookInfo(exists=False, error=UNREACHABLE_ERROR)existing = _find_webhook_by_url(webhooks, webhook_url)if existing is None:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/source.pyindex 2e482b8843bd..c71ce71637fd 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/source.py@@ -161,14 +161,8 @@ class AttentiveSource(def get_non_retryable_errors(self) -> dict[str, str | None]:return {- "401 Client Error: Unauthorized": (- "Attentive rejected the API key. Create a private app under Marketplace > Create app "- "in Attentive and reconnect with its API key."- ),- "403 Client Error: Forbidden": (- "The API key doesn't have permission for this endpoint. Make sure the private app has "- "the Webhooks permission."- ),+ "401 Client Error: Unauthorized": api_client.API_KEY_REJECTED_ERROR,+ "403 Client Error: Forbidden": api_client.WEBHOOKS_PERMISSION_ERROR,}def get_schemas(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/tests/test_attentive_api_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/tests/test_attentive_api_client.pyindex 3eb3efc9d0bd..4ae8dad17ac5 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/tests/test_attentive_api_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/attentive/tests/test_attentive_api_client.py@@ -51,7 +51,7 @@ class TestValidateCredentials:ok, error = api_client.validate_credentials("key")assert ok is False- assert "rejected the API key" in (error or "")+ assert "rejected your API key" in (error or "")@mock.patch(f"{_MODULE}.make_tracked_session")def test_falls_back_to_v1_me_on_404(self, mock_session):@@ -137,7 +137,7 @@ class TestCreateWebhook:result = api_client.create_webhook("key", "https://ph.example/webhook", ["sms_sent"])assert result.success is False- assert "denied the request" in (result.error or "")+ assert "Webhooks permission" in (result.error or "")class TestEnableWebhook: