# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. from __future__ import annotations from typing import Any, Dict, Union, cast from datetime import datetime import httpx from ...._types import Body, Omit, Query, Headers, NoneType, NotGiven, omit, not_given from ...._utils import path_template, maybe_transform, strip_not_given, async_maybe_transform from ...._compat import cached_property from ...._resource import SyncAPIResource, AsyncAPIResource from ...._response import ( to_raw_response_wrapper, to_streamed_response_wrapper, async_to_raw_response_wrapper, async_to_streamed_response_wrapper, ) from ....pagination import SyncDefaultFlatPagination, AsyncDefaultFlatPagination from ...._base_client import AsyncPaginator, make_request_options from ....types.ai.assistants import ( ConversationChannelType, scheduled_event_list_params, scheduled_event_create_params, ) from ....types.ai.assistants.scheduled_event_response import ScheduledEventResponse from ....types.ai.assistants.conversation_channel_type import ConversationChannelType from ....types.ai.assistants.scheduled_call_settings_param import ScheduledCallSettingsParam from ....types.ai.assistants.scheduled_event_list_response import ScheduledEventListResponse __all__ = ["ScheduledEventsResource", "AsyncScheduledEventsResource"] class ScheduledEventsResource(SyncAPIResource): """Configure AI assistant specifications""" @cached_property def with_raw_response(self) -> ScheduledEventsResourceWithRawResponse: """ This property can be used as a prefix for any HTTP method call to return the raw response object instead of the parsed content. For more information, see https://www.github.com/team-telnyx/telnyx-python#accessing-raw-response-data-eg-headers """ return ScheduledEventsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> ScheduledEventsResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/team-telnyx/telnyx-python#with_streaming_response """ return ScheduledEventsResourceWithStreamingResponse(self) def create( self, assistant_id: str, *, scheduled_at_fixed_datetime: Union[str, datetime], telnyx_agent_target: str, telnyx_conversation_channel: ConversationChannelType, telnyx_end_user_target: str, call_settings: ScheduledCallSettingsParam | Omit = omit, conversation_metadata: Dict[str, Union[str, int, bool]] | Omit = omit, dynamic_variables: Dict[str, str] | Omit = omit, max_retries_client_errors: int | Omit = omit, retry_interval_secs: int | Omit = omit, text: str | Omit = omit, idempotency_key: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> ScheduledEventResponse: """ Create a scheduled event for an assistant Args: scheduled_at_fixed_datetime: The datetime at which the event should be scheduled. Formatted as ISO 8601. telnyx_agent_target: The phone number, SIP URI, to schedule the call or text from. telnyx_end_user_target: The phone number, SIP URI, to schedule the call or text to. call_settings: Per-call telephony overrides applied when a scheduled phone-call event dispatches. Phone-call events only. New per-call dispatch options should be added here rather than as top-level event fields. conversation_metadata: Metadata associated with the conversation. Telnyx provides several pieces of metadata, but customers can also add their own. dynamic_variables: A map of dynamic variable names to values. These variables can be referenced in the assistant's instructions and messages using {{variable_name}} syntax. max_retries_client_errors: Configure number of retries on client errors: busy, no-answer, failed, canceled (caller hung up before the callee answered) text: Required for sms scheduled events. The text to be sent to the end user. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") extra_headers = {**strip_not_given({"Idempotency-Key": idempotency_key}), **(extra_headers or {})} return cast( ScheduledEventResponse, self._post( path_template("/ai/assistants/{assistant_id}/scheduled_events", assistant_id=assistant_id), body=maybe_transform( { "scheduled_at_fixed_datetime": scheduled_at_fixed_datetime, "telnyx_agent_target": telnyx_agent_target, "telnyx_conversation_channel": telnyx_conversation_channel, "telnyx_end_user_target": telnyx_end_user_target, "call_settings": call_settings, "conversation_metadata": conversation_metadata, "dynamic_variables": dynamic_variables, "max_retries_client_errors": max_retries_client_errors, "retry_interval_secs": retry_interval_secs, "text": text, }, scheduled_event_create_params.ScheduledEventCreateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=cast( Any, ScheduledEventResponse ), # Union types cannot be passed in as arguments in the type system ), ) def retrieve( self, event_id: str, *, assistant_id: str, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> ScheduledEventResponse: """ Returns the details of a single scheduled event configured for the specified assistant. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") if not event_id: raise ValueError(f"Expected a non-empty value for `event_id` but received {event_id!r}") return cast( ScheduledEventResponse, self._get( path_template( "/ai/assistants/{assistant_id}/scheduled_events/{event_id}", assistant_id=assistant_id, event_id=event_id, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=cast( Any, ScheduledEventResponse ), # Union types cannot be passed in as arguments in the type system ), ) def list( self, assistant_id: str, *, conversation_channel: ConversationChannelType | Omit = omit, from_date: Union[str, datetime] | Omit = omit, page_number: int | Omit = omit, page_size: int | Omit = omit, to_date: Union[str, datetime] | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> SyncDefaultFlatPagination[ScheduledEventListResponse]: """ Get scheduled events for an assistant with pagination and filtering Args: conversation_channel: Filter results by conversation channel. from_date: Start of the date range filter (inclusive, ISO 8601). to_date: End of the date range filter (inclusive, ISO 8601). extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") return self._get_api_list( path_template("/ai/assistants/{assistant_id}/scheduled_events", assistant_id=assistant_id), page=SyncDefaultFlatPagination[ScheduledEventListResponse], options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform( { "conversation_channel": conversation_channel, "from_date": from_date, "page_number": page_number, "page_size": page_size, "to_date": to_date, }, scheduled_event_list_params.ScheduledEventListParams, ), ), model=cast( Any, ScheduledEventListResponse ), # Union types cannot be passed in as arguments in the type system ) def delete( self, event_id: str, *, assistant_id: str, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> None: """If the event is pending, this will cancel the event. Otherwise, this will simply remove the record of the event. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") if not event_id: raise ValueError(f"Expected a non-empty value for `event_id` but received {event_id!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return self._delete( path_template( "/ai/assistants/{assistant_id}/scheduled_events/{event_id}", assistant_id=assistant_id, event_id=event_id, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=NoneType, ) class AsyncScheduledEventsResource(AsyncAPIResource): """Configure AI assistant specifications""" @cached_property def with_raw_response(self) -> AsyncScheduledEventsResourceWithRawResponse: """ This property can be used as a prefix for any HTTP method call to return the raw response object instead of the parsed content. For more information, see https://www.github.com/team-telnyx/telnyx-python#accessing-raw-response-data-eg-headers """ return AsyncScheduledEventsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncScheduledEventsResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/team-telnyx/telnyx-python#with_streaming_response """ return AsyncScheduledEventsResourceWithStreamingResponse(self) async def create( self, assistant_id: str, *, scheduled_at_fixed_datetime: Union[str, datetime], telnyx_agent_target: str, telnyx_conversation_channel: ConversationChannelType, telnyx_end_user_target: str, call_settings: ScheduledCallSettingsParam | Omit = omit, conversation_metadata: Dict[str, Union[str, int, bool]] | Omit = omit, dynamic_variables: Dict[str, str] | Omit = omit, max_retries_client_errors: int | Omit = omit, retry_interval_secs: int | Omit = omit, text: str | Omit = omit, idempotency_key: str | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> ScheduledEventResponse: """ Create a scheduled event for an assistant Args: scheduled_at_fixed_datetime: The datetime at which the event should be scheduled. Formatted as ISO 8601. telnyx_agent_target: The phone number, SIP URI, to schedule the call or text from. telnyx_end_user_target: The phone number, SIP URI, to schedule the call or text to. call_settings: Per-call telephony overrides applied when a scheduled phone-call event dispatches. Phone-call events only. New per-call dispatch options should be added here rather than as top-level event fields. conversation_metadata: Metadata associated with the conversation. Telnyx provides several pieces of metadata, but customers can also add their own. dynamic_variables: A map of dynamic variable names to values. These variables can be referenced in the assistant's instructions and messages using {{variable_name}} syntax. max_retries_client_errors: Configure number of retries on client errors: busy, no-answer, failed, canceled (caller hung up before the callee answered) text: Required for sms scheduled events. The text to be sent to the end user. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") extra_headers = {**strip_not_given({"Idempotency-Key": idempotency_key}), **(extra_headers or {})} return cast( ScheduledEventResponse, await self._post( path_template("/ai/assistants/{assistant_id}/scheduled_events", assistant_id=assistant_id), body=await async_maybe_transform( { "scheduled_at_fixed_datetime": scheduled_at_fixed_datetime, "telnyx_agent_target": telnyx_agent_target, "telnyx_conversation_channel": telnyx_conversation_channel, "telnyx_end_user_target": telnyx_end_user_target, "call_settings": call_settings, "conversation_metadata": conversation_metadata, "dynamic_variables": dynamic_variables, "max_retries_client_errors": max_retries_client_errors, "retry_interval_secs": retry_interval_secs, "text": text, }, scheduled_event_create_params.ScheduledEventCreateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=cast( Any, ScheduledEventResponse ), # Union types cannot be passed in as arguments in the type system ), ) async def retrieve( self, event_id: str, *, assistant_id: str, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> ScheduledEventResponse: """ Returns the details of a single scheduled event configured for the specified assistant. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") if not event_id: raise ValueError(f"Expected a non-empty value for `event_id` but received {event_id!r}") return cast( ScheduledEventResponse, await self._get( path_template( "/ai/assistants/{assistant_id}/scheduled_events/{event_id}", assistant_id=assistant_id, event_id=event_id, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=cast( Any, ScheduledEventResponse ), # Union types cannot be passed in as arguments in the type system ), ) def list( self, assistant_id: str, *, conversation_channel: ConversationChannelType | Omit = omit, from_date: Union[str, datetime] | Omit = omit, page_number: int | Omit = omit, page_size: int | Omit = omit, to_date: Union[str, datetime] | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> AsyncPaginator[ScheduledEventListResponse, AsyncDefaultFlatPagination[ScheduledEventListResponse]]: """ Get scheduled events for an assistant with pagination and filtering Args: conversation_channel: Filter results by conversation channel. from_date: Start of the date range filter (inclusive, ISO 8601). to_date: End of the date range filter (inclusive, ISO 8601). extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") return self._get_api_list( path_template("/ai/assistants/{assistant_id}/scheduled_events", assistant_id=assistant_id), page=AsyncDefaultFlatPagination[ScheduledEventListResponse], options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform( { "conversation_channel": conversation_channel, "from_date": from_date, "page_number": page_number, "page_size": page_size, "to_date": to_date, }, scheduled_event_list_params.ScheduledEventListParams, ), ), model=cast( Any, ScheduledEventListResponse ), # Union types cannot be passed in as arguments in the type system ) async def delete( self, event_id: str, *, assistant_id: str, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> None: """If the event is pending, this will cancel the event. Otherwise, this will simply remove the record of the event. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ if not assistant_id: raise ValueError(f"Expected a non-empty value for `assistant_id` but received {assistant_id!r}") if not event_id: raise ValueError(f"Expected a non-empty value for `event_id` but received {event_id!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return await self._delete( path_template( "/ai/assistants/{assistant_id}/scheduled_events/{event_id}", assistant_id=assistant_id, event_id=event_id, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=NoneType, ) class ScheduledEventsResourceWithRawResponse: def __init__(self, scheduled_events: ScheduledEventsResource) -> None: self._scheduled_events = scheduled_events self.create = to_raw_response_wrapper( scheduled_events.create, ) self.retrieve = to_raw_response_wrapper( scheduled_events.retrieve, ) self.list = to_raw_response_wrapper( scheduled_events.list, ) self.delete = to_raw_response_wrapper( scheduled_events.delete, ) class AsyncScheduledEventsResourceWithRawResponse: def __init__(self, scheduled_events: AsyncScheduledEventsResource) -> None: self._scheduled_events = scheduled_events self.create = async_to_raw_response_wrapper( scheduled_events.create, ) self.retrieve = async_to_raw_response_wrapper( scheduled_events.retrieve, ) self.list = async_to_raw_response_wrapper( scheduled_events.list, ) self.delete = async_to_raw_response_wrapper( scheduled_events.delete, ) class ScheduledEventsResourceWithStreamingResponse: def __init__(self, scheduled_events: ScheduledEventsResource) -> None: self._scheduled_events = scheduled_events self.create = to_streamed_response_wrapper( scheduled_events.create, ) self.retrieve = to_streamed_response_wrapper( scheduled_events.retrieve, ) self.list = to_streamed_response_wrapper( scheduled_events.list, ) self.delete = to_streamed_response_wrapper( scheduled_events.delete, ) class AsyncScheduledEventsResourceWithStreamingResponse: def __init__(self, scheduled_events: AsyncScheduledEventsResource) -> None: self._scheduled_events = scheduled_events self.create = async_to_streamed_response_wrapper( scheduled_events.create, ) self.retrieve = async_to_streamed_response_wrapper( scheduled_events.retrieve, ) self.list = async_to_streamed_response_wrapper( scheduled_events.list, ) self.delete = async_to_streamed_response_wrapper( scheduled_events.delete, )