Skip to content

: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.

5.9 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)
from products.warehouse_sources.sources.telnyx._config import TelnyxSourceConfig
from products.warehouse_sources.sources.telnyx.settings import ENDPOINTS, INCREMENTAL_FIELDS, TELNYX_ENDPOINTS
from products.warehouse_sources.sources.telnyx.telnyx import (
    TelnyxResumeConfig,
    telnyx_source,
    validate_credentials as validate_telnyx_credentials,
)


@SourceRegistry.register
class TelnyxSource(ResumableSource[TelnyxSourceConfig, TelnyxResumeConfig]):
    codebase_detection = detect(
        env=("^TELNYX_",),
        npm=("telnyx", "@telnyx/webrtc", "@telnyx/react-client"),
        pypi=("telnyx",),
        cocoapods=("TelnyxRTC", "TelnyxVideoSdk", "TelnyxVoiceAIWidget"),
        composer=("telnyx/telnyx-php",),
        go=("github.com/team-telnyx/telnyx-go", "github.com/team-telnyx/telnyx-cli"),
        maven=(
            "com.telnyx.sdk:telnyx",
            "com.telnyx.sdk:telnyx-client-okhttp",
            "com.telnyx.sdk:telnyx-core",
            "com.telnyx.sdk:telnyx-java",
            "com.telnyx.sdk:telnyx-java-client-okhttp",
            "com.telnyx.sdk:telnyx-java-core",
            "com.telnyx.sdk:telnyx-lib",
            "com.telnyx.sdk:telnyx-websocket",
        ),
        pub=("telnyx_webrtc", "flutter_telnyx_voice_ai_widget", "telnyx_common"),
        rubygems=("telnyx",),
        swiftpm=("github.com/team-telnyx/telnyx-webrtc-ios", "github.com/team-telnyx/ios-telnyx-voice-ai-widget"),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://developers.telnyx.com/docs/api/v2/detail-records/detail-record-search"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.TELNYX

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized": "Your Telnyx API key is invalid or has expired. Generate a new key from the Telnyx portal and reconnect.",
            "403 Client Error: Forbidden": "Your Telnyx API key doesn't have permission for this request. Check the key's scopes in the Telnyx portal and reconnect.",
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.telnyx.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: TelnyxSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)

    def validate_credentials(
        self,
        config: TelnyxSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_telnyx_credentials(config.api_key):
            return True, None

        return False, "Invalid Telnyx API key"

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[TelnyxResumeConfig]:
        return ResumableSourceManager[TelnyxResumeConfig](inputs, TelnyxResumeConfig)

    def source_for_pipeline(
        self,
        config: TelnyxSourceConfig,
        resumable_source_manager: ResumableSourceManager[TelnyxResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        endpoint = TELNYX_ENDPOINTS[inputs.schema_name]
        resource = telnyx_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )
        return SourceResponse(
            name=resource.name,
            items=lambda: resource,
            primary_keys=endpoint.primary_key,
            column_hints=resource.column_hints,
            partition_mode="datetime",
            partition_format="month",
            partition_keys=[endpoint.partition_key],
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.TELNYX,
            category=DataWarehouseSourceCategory.COMMUNICATION,
            label="Telnyx",
            keywords=["cpaas", "sms", "voice"],
            releaseStatus=ReleaseStatus.GA,
            caption=(
                "Enter your Telnyx API v2 key to pull messaging, voice, verify, wireless, and media "
                "storage detail records into the PostHog Data warehouse. Generate a key from "
                "**API Keys** in the Telnyx portal."
            ),
            iconPath="/static/services/telnyx.png",
            docsUrl="https://posthog.com/docs/cdp/sources/telnyx",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )