Skip to content

Channels

tac.channels

Communication channels for the Twilio Agent Connect.

BaseChannel

BaseChannel(
    tac: TAC,
    memory_mode: MemoryMode = "never",
    dedup_capacity: int = 10000,
)

Bases: ABC

Abstract base class for TAC channels.

Channels handle protocol-specific webhook processing and response delivery for different communication channels (SMS, Voice, etc.).

Provides only what every channel family shares: webhook deduplication, participant-matching helpers, and memory retrieval.

Session storage deliberately isn't here. VoiceChannel owns its sessions (a call is pinned to one process); messaging channels derive a session per request and hold nothing between them.

Initialize base channel.

Parameters:

Name Type Description Default
tac TAC

TAC instance for memory/context operations

required
memory_mode MemoryMode

Memory retrieval mode. Default is "never". - "always": Retrieve memory for every message with the query string - "once" (Voice only): Retrieve once with an empty query and cache it on the session. Invalidated when the conversation becomes INACTIVE. - "never": Skip memory retrieval

'never'
dedup_capacity int

Maximum number of idempotency tokens to track for webhook deduplication. Default 10000. Must be positive.

10000

process_webhook abstractmethod async

process_webhook(
    webhook_data: dict[str, Any],
    idempotency_token: str | None = None,
) -> None

Process incoming webhook event from Twilio.

This method should: 1. Parse and validate webhook data 2. Handle conversation lifecycle (start, message, end) 3. Trigger memory retrieval via TAC 4. Invoke registered callbacks

Parameters:

Name Type Description Default
webhook_data dict[str, Any]

Raw webhook event data from Twilio

required
idempotency_token str | None

Optional Twilio idempotency token from request headers

None

send_response abstractmethod async

send_response(
    conversation_id: str,
    response: str
    | AsyncGenerator[str | dict[str, Any], None],
    role: str | None = None,
) -> None

Send response back through the channel.

Supports both simple string responses and streaming via async generators.

Parameters:

Name Type Description Default
conversation_id str

Conversation ID to send response to

required
response str | AsyncGenerator[str | dict[str, Any], None]

Message content (string) or async generator for streaming

required
role str | None

Optional message role (e.g., 'assistant', 'user', 'system')

None

get_channel_name abstractmethod

get_channel_name() -> str

Get the channel name identifier.

Returns:

Type Description
str

Channel name (e.g., 'SMS', 'VOICE')

ChatChannel

ChatChannel(
    tac: TAC,
    config: ChatChannelConfig
    | dict[str, Any]
    | None = None,
)

Bases: MessagingChannel

Chat Channel for handling web chat conversations.

Uses identity-based addressing instead of phone numbers. Automatically creates AI_AGENT participant if needed (lazy creation) and manages conversation lifecycle through Conversation Orchestrator webhooks.

initiate_outbound_conversation async

initiate_outbound_conversation(
    options: InitiateChatConversationOptions,
) -> InitiateConversationResult

Initiate an outbound Chat conversation.

Creates a conversation via Conversation Orchestrator with inline participants, then sends the initial message via the Actions API. If an active conversation with the same addresses already exists (group-by dedup), CO returns 409 and the existing conversation is reused.

ChatChannelConfig pydantic-model

Bases: MessagingChannelConfig

Configuration for Chat channel.

Attributes:

Name Type Description
agent_address str

Chat agent identity string used to identify the bot's messages.

Fields:

agent_address pydantic-field

agent_address: str = 'ai-assistant'

Chat agent identity string for bot message filtering

MessagingChannel

MessagingChannel(
    tac: TAC,
    dedup_capacity: int = 10000,
    memory_mode: MemoryMode = "never",
)

Bases: BaseChannel

Abstract base class for messaging channels (SMS, RCS, WhatsApp, Chat).

Provides shared webhook processing logic for channels that use Conversation Orchestrator webhooks with COMMUNICATION_CREATED and CONVERSATION_UPDATED event types.

Stateless by construction. No conversation session is kept between webhooks: each request derives its own ConversationSession from the payload, config, and one list_participants call it needs anyway. So any replica can serve any webhook — no sticky sessions, no shared datastore. A session lasts one turn, so session.metadata doesn't carry over to the next message; keep state across turns in your own store, keyed by conversation_id. ConversationSession.conversation_metadata() reads the metadata stored on Conversation Orchestrator from any instance.

Subclasses must implement:

  • is_default_agent_address(): Fast-path check for the channel's default agent address
  • get_agent_address(conversation_id): Return the agent's ParticipantAddress
  • get_channel_name(): Return channel name ("SMS", "RCS", "WHATSAPP", "CHAT")

send_response() is provided here as a shared implementation. Subclasses may override _build_channel_settings() to customize how ActionChannelSettings is built for the outbound send (e.g. chat requires channel_id).

Subclass class attributes:

  • reconcile_customer_type: If True, reconciliation will also promote a channel-matching UNKNOWN participant (not owning the agent address) to CUSTOMER. Set False for channels where the customer is identified author-driven (e.g. chat).
  • derive_inbound_agent_from_recipients: If True, the inbound agent address is derived from the webhook's recipient (which of TAC's configured numbers was messaged), validated against the channel's allowlist. True for phone-like channels (SMS, RCS, WhatsApp). Set False for channels where the agent is a single identity rather than a number set (e.g. chat), which keeps the default-address path instead.

is_default_agent_address abstractmethod

is_default_agent_address(author_address: str) -> bool

Fast-path check: is the author address one of this channel's configured agent addresses?

Checks the channel's full sender allowlist, not just the default sender: config.phone_numbers for SMS, config.rcs_sender_ids for RCS, config.whatsapp_numbers for WhatsApp, and agent_address for Chat.

Parameters:

Name Type Description Default
author_address str

The address of the message author

required

Returns:

Type Description
bool

True if the address matches one of the channel's configured agent addresses

get_agent_address abstractmethod

get_agent_address(
    conversation_id: str,
) -> ParticipantAddress

Return the agent-side ParticipantAddress for this conversation.

Identifies which participant represents the agent, and supplies the from address for outbound sends.

send_response async

send_response(
    conversation_id: str | ConversationSession,
    response: str
    | AsyncGenerator[str | dict[str, Any], None],
    role: str | None = None,
) -> None

Send a text response using the Conversation Orchestrator Send API.

Pass the session, not the id. Hand back the one on_message_ready gave you (or that initiate_outbound_conversation returned) and the send costs no extra API call — it already carries both participants. A bare id still works, but the channel keeps no session to look up, so it spends a list_participants call rebuilding one.

Parameters:

Name Type Description Default
conversation_id str | ConversationSession

The ConversationSession to reply within (preferred), or the conversation id. Named for the id it took before sessions were accepted, so existing keyword calls work.

required
response str | AsyncGenerator[str | dict[str, Any], None]

Message content. Must be str — messaging channels send a single complete message via the Conversation Orchestrator Send API and do not support streaming (unlike the Voice channel).

required
role str | None

Optional message role (unused by messaging channels)

None

Raises:

Type Description
TypeError

If response is not a string (e.g. an async generator is passed, since messaging channels don't support streaming)

RuntimeError

If no recipient can be resolved for the conversation.

process_webhook async

process_webhook(
    webhook_data: dict[str, Any],
    idempotency_token: str | None = None,
) -> None

Process messaging channel webhook event and manage conversation lifecycle.

Handles:

  • COMMUNICATION_CREATED: Process incoming messages from customers
  • CONVERSATION_UPDATED: Fire on_conversation_ended and report "Conversation Ended" when the conversation closes
  • PARTICIPANT_ADDED: Report "Conversation Started" when the customer joins on this channel

Any replica can handle any webhook. The idempotency cache is per-process, though, so a Twilio retry landing on a different replica is processed again, and on_message_ready can run twice for the same message. Retries are rare when the webhook is acknowledged before processing, as TACFastAPIServer does.

Parameters:

Name Type Description Default
webhook_data dict[str, Any]

Raw webhook event data from Twilio

required
idempotency_token str | None

Optional Twilio idempotency token from request headers

None

MessagingChannelConfig pydantic-model

Bases: BaseModel

Base configuration for messaging channels (SMS, RCS, WhatsApp, Chat).

Attributes:

Name Type Description
dedup_capacity int

Maximum number of idempotency tokens to track. Default 10000 is suitable for most applications. Uses Twilio's i-twilio-idempotency-token header for deduplication.

memory_mode MemoryMode

Memory retrieval mode. Default is "never".

  • "always": Retrieve memory for every message with the query string
  • "never": Skip memory retrieval
  • "once": Deprecated on messaging channels and removed in 3.0. It caches a recall on a session that outlives one request, which messaging channels don't keep, so it logs a warning and runs as "always". It's still supported on the Voice channel.

Fields:

dedup_capacity pydantic-field

dedup_capacity: int = 10000

Maximum number of idempotency tokens to track for deduplication

memory_mode pydantic-field

memory_mode: MemoryMode = 'never'

Memory retrieval mode for this channel

RCSChannel

RCSChannel(
    tac: TAC,
    config: RCSChannelConfig | dict[str, Any] | None = None,
)

Bases: MessagingChannel

RCS Channel for handling RCS-based conversations.

Inherits shared messaging channel webhook processing from MessagingChannel and provides RCS-specific message sending and filtering.

RCS uses RCS Sender IDs configured in TACConfig (via TWILIO_RCS_SENDER_ID).

is_default_agent_address

is_default_agent_address(author_address: str) -> bool

Check if the author address is one of the configured RCS senders.

get_agent_address

get_agent_address(
    conversation_id: str,
) -> ParticipantAddress

Get the agent's default participant address for this conversation.

initiate_outbound_conversation async

initiate_outbound_conversation(
    options: InitiateMessagingConversationOptions,
) -> InitiateConversationResult

Initiate an outbound RCS conversation.

Creates a conversation via Conversation Orchestrator with inline participants, then sends the initial message via the Actions API. Uses options.from_ when provided (must be one of the configured RCS senders), otherwise falls back to the default rcs_sender_id from TACConfig. If an active conversation with the same addresses already exists (group-by dedup), CO returns 409 and the existing conversation is reused.

Parameters:

Name Type Description Default
options InitiateMessagingConversationOptions

Conversation initiation options (to address and message)

required

Returns:

Type Description
InitiateConversationResult

InitiateConversationResult with conversation_id and session

Raises:

Type Description
ValueError

If options.from_ is set but is not one of the configured RCS senders.

RCSChannelConfig pydantic-model

Bases: MessagingChannelConfig

Configuration for RCS channel.

Inherits dedup_capacity and memory_mode from MessagingChannelConfig.

Fields:

dedup_capacity pydantic-field

dedup_capacity: int = 10000

Maximum number of idempotency tokens to track for deduplication

SMSChannel

SMSChannel(
    tac: TAC,
    config: SMSChannelConfig | dict[str, Any] | None = None,
)

Bases: MessagingChannel

SMS Channel for handling SMS-based conversations.

Inherits shared messaging channel webhook processing from MessagingChannel and provides SMS-specific message sending and filtering.

initiate_outbound_conversation async

initiate_outbound_conversation(
    options: InitiateMessagingConversationOptions,
) -> InitiateConversationResult

Initiate an outbound SMS conversation.

Creates a conversation via Conversation Orchestrator with inline participants, then sends the initial message via the Actions API. If an active conversation with the same addresses already exists (group-by dedup), CO returns 409 and the existing conversation is reused.

SMSChannelConfig pydantic-model

Bases: MessagingChannelConfig

Configuration for SMS channel.

Inherits dedup_capacity and memory_mode from MessagingChannelConfig.

Fields:

dedup_capacity pydantic-field

dedup_capacity: int = 10000

Maximum number of idempotency tokens to track for deduplication

VoiceChannel

VoiceChannel(
    tac: TAC,
    config: VoiceProviderConfig
    | dict[str, Any]
    | None = None,
)

Bases: BaseChannel

Voice Channel for handling voice-based conversations via WebSocket.

Owns the Twilio Calls API lifecycle and conversation bookkeeping (inherited from BaseChannel). The real-time media transport itself — TwiML generation, WebSocket protocol handling, outbound call initiation — is delegated to a pluggable VoiceProvider.

This channel is framework-agnostic and accepts any WebSocket implementation satisfying WebSocketProtocol. For a batteries-included FastAPI server, use tac.server.TACFastAPIServer.

Initialize Voice channel for websocket protocol handling.

Parameters:

Name Type Description Default
tac TAC

TAC instance for memory/context operations

required
config VoiceProviderConfig | dict[str, Any] | None

Voice channel configuration — a VoiceProviderConfig instance for any provider, or a dict. The dict form is shorthand for ConversationRelayProviderConfig (the default ConversationRelay provider) specifically, not a generic constructor — it's hydrated as ConversationRelayProviderConfig(**config) and fails if it has fields that config doesn't have. To configure a different provider, construct that provider's config and pass it directly instead of a dict. If None, uses ConversationRelayProviderConfig().

None

Examples:

>>> channel = VoiceChannel(tac, config={"memory_mode": "always"})
>>> channel = VoiceChannel(
...     tac, config=ConversationRelayProviderConfig(session_manager=sm)
... )
>>> channel = VoiceChannel(tac)  # Use defaults

on_inbound_call_twiml

on_inbound_call_twiml(
    callback: InboundCallTwiMLHandler,
) -> None

Register a callback that produces per-call overrides for the active provider's inbound-call TwiML.

The callback receives a framework-neutral TwiMLRequest (parsed from the Twilio webhook form) and returns a VoiceTwiMLOptions — the concrete subclass the active provider expects (e.g. VoiceTwiMLOptionsConversationRelay for the default ConversationRelay provider; see that provider's handle_incoming_call for its merge/precedence rules).

Outbound calls don't use this — pass per-call TwiML via InitiateVoiceConversationOptions.twiml_options directly.

on_call_status

on_call_status(callback: CallStatusHandler) -> None

Register a handler for Twilio status_callback webhooks.

This is the Calls-API status callback (call disposition), not the active provider's own out-of-band lifecycle webhook — see :meth:handle_twilio_provider_callback.

Registering does two things: it stores the handler, and it makes later outbound calls pass status_callback to calls.create. With no handler registered TAC omits that parameter, so Twilio has nowhere to post and the event never arrives.

Twilio reports only the terminal event by default, which covers every disposition; set CallOptions.status_callback_event for ringing/answered.

Example
async def on_call_status(event: CallStatusEvent) -> None:
    if event.is_unreached:
        ...  # queue a retry


voice_channel.on_call_status(on_call_status)

on_amd

on_amd(callback: AmdHandler) -> None

Register a handler for Twilio async_amd_status_callback webhooks.

Registering makes later outbound calls pass async_amd_status_callback to calls.create; without a handler TAC omits it and Twilio has nowhere to post the result. It does not enable detection — that's per-call, via CallOptions.machine_detection and async_amd, both of which are required for this to fire (at most once per call).

Example
async def on_amd(event: AmdEvent) -> None:
    if event.is_machine:
        await voice_channel.end_call(event.call_sid)  # voicemail → hang up


voice_channel.on_amd(on_amd)

on_recording

on_recording(callback: RecordingHandler) -> None

Register a handler for Twilio recording_status_callback webhooks.

Registering makes later outbound calls pass recording_status_callback to calls.create; without a handler TAC omits it and Twilio has nowhere to post. It does not start recording — that's CallOptions.record, which is required for this to fire.

Example
async def on_recording(event: RecordingEvent) -> None:
    if event.recording_status == "completed":
        ...  # store event.recording_url


voice_channel.on_recording(on_recording)

on_call_ended

on_call_ended(callback: CallEndedHandler) -> None

Register a handler for WebSocket teardown.

Fires once per call, always, on the instance that held the call. It receives the live session just before it is discarded, so it is the only place late-call in-memory state — the transcript, call_sid, anything you put on metadata — is still reachable.

Not the same as on_conversation_ended (fires when the conversation closes, on any instance; on an orchestrated call, from a session rebuilt from Conversation Orchestrator, identity only) or on_call_status (a Twilio Calls-API webhook, no session). Handler exceptions are logged and swallowed.

Example
async def on_call_ended(session: ConversationSession) -> None:
    await archive(session.call_sid, session.metadata.get("transcript", []))


voice_channel.on_call_ended(on_call_ended)

handle_incoming_call async

handle_incoming_call(
    twiml_request: TwiMLRequest | None = None,
    *,
    host_twiml_options: VoiceTwiMLOptions | None = None,
) -> str

Generate TwiML response for incoming voice calls. Delegates to the active provider — see ConversationRelayProvider.handle_incoming_call for the full merge/precedence rules (only meaningful for that provider; a non-TwiML provider ignores host_twiml_options).

host_twiml_options is typed against the VoiceTwiMLOptions base — the active provider defines the concrete shape it expects (e.g. VoiceTwiMLOptionsConversationRelay) and validates it at runtime.

handle_twilio_provider_callback async

handle_twilio_provider_callback(
    payload_dict: dict[str, str],
) -> None

Handle the active provider's own out-of-band lifecycle webhook, if it has one — e.g. ConversationRelay's <Connect action=...> callback.

In relay-only mode, this is a secondary mechanism for cleaning up conversation state when a call ends (the primary mechanism is websocket disconnect). In orchestrated mode, conversation lifecycle is managed by CO webhooks, so this is a no-op.

Not every provider has an equivalent webhook — see VoiceProvider.handle_twilio_provider_callback.

Parameters:

Name Type Description Default
payload_dict dict[str, str]

Raw form data dict from the webhook request.

required

handle_call_status_event async

handle_call_status_event(
    payload_dict: dict[str, str],
) -> None

Handle a Twilio status_callback webhook.

The developer routes the request here (TACFastAPIServer does this automatically for its /status call-event route). Parsed into a :class:CallStatusEvent and dispatched to the :meth:on_call_status handler. No-op if no handler is registered.

Parameters:

Name Type Description Default
payload_dict dict[str, str]

Raw form data dict from the webhook request.

required

handle_amd_event async

handle_amd_event(payload_dict: dict[str, str]) -> None

Handle a Twilio async_amd_status_callback webhook.

The developer routes the request here (TACFastAPIServer does this automatically for its /amd call-event route). Parsed into an :class:AmdEvent and dispatched to the :meth:on_amd handler. No-op if no handler is registered.

Parameters:

Name Type Description Default
payload_dict dict[str, str]

Raw form data dict from the webhook request.

required

handle_recording_event async

handle_recording_event(
    payload_dict: dict[str, str],
) -> None

Handle a Twilio recording_status_callback webhook.

The developer routes the request here (TACFastAPIServer does this automatically for its /recording call-event route). Parsed into a :class:RecordingEvent and dispatched to the :meth:on_recording handler. No-op if no handler is registered.

Parameters:

Name Type Description Default
payload_dict dict[str, str]

Raw form data dict from the webhook request.

required

end_call async

end_call(call_sid: str) -> bool

Hang up a call and clean up its session.

Works on call_sid alone, whether or not a session exists yet. No-ops the session cleanup if none is tracked.

Does not raise — hanging up an already-ended call is routine (the callee hangs up while AMD is still resolving), and handlers shouldn't have to guard against it.

The session is released as on any hangup: on_call_ended fires now. On an orchestrated call on_conversation_ended fires later, when Conversation Orchestrator's CLOSED webhook arrives; otherwise it fires now too.

Parameters:

Name Type Description Default
call_sid str

Twilio Call SID (from a call event, the outbound result, or ConversationSession.call_sid).

required

Returns:

Type Description
bool

True if Twilio accepted the hangup, False if it failed (logged).

bool

Session cleanup runs either way.

get_conversation_session_by_call_sid

get_conversation_session_by_call_sid(
    call_sid: str,
) -> ConversationSession | None

Look up the active voice session for a Twilio Call SID.

Out-of-band code holding a CallSid — a dashboard route, an operator action, a call-event handler — can't reach the session-facing methods, which are keyed by conversation id: the Orchestrator conversation id in orchestrator mode, the CallSid only in ConversationRelay-only mode.

Relay-only mode creates the session on the caller's first prompt. Orchestrator mode creates it earlier — as soon as the background CO lookup started at WebSocket setup finishes — so it may already exist before the caller has said anything, including before on_amd fires. Either way, treat this as racy and use :meth:end_call to hang up, which works whether or not a session exists yet.

At the other end the session is released as soon as the call's WebSocket closes, in every mode — so out-of-band events that arrive after the hangup (on_call_status, on_recording) generally find nothing here. Read late-call state from the session on_call_ended hands you instead.

Named for ConversationSession; session_manager deals in SessionState, a different type.

Example
async def nudge(call_sid: str) -> None:
    session = voice_channel.get_conversation_session_by_call_sid(call_sid)
    if session is not None:
        await voice_channel.send_response(session.conversation_id, "Still there?")

Parameters:

Name Type Description Default
call_sid str

Twilio Call SID, e.g. from InitiateVoiceConversationResult.call_sid or a call event.

required

Returns:

Type Description
ConversationSession | None

The session, or None — not created yet (relay-only mode, or

ConversationSession | None

orchestrator mode where the background CO lookup hasn't finished),

ConversationSession | None

the call ended, or it landed on another instance. For the last

ConversationSession | None

case, :meth:resolve_conversation_session_by_call_sid falls back

ConversationSession | None

to Conversation Orchestrator.

resolve_conversation_session_by_call_sid async

resolve_conversation_session_by_call_sid(
    call_sid: str,
) -> ConversationSession | None

Look up a call's session on any instance, falling back to Conversation Orchestrator when this instance doesn't hold the call.

Out-of-band call events (on_call_status, on_amd, on_recording) carry only a CallSid and, without TACConfig.instance_public_domain, can reach any replica. This returns the live session when the call is held here — same as :meth:get_conversation_session_by_call_sid — and otherwise, for a ConversationRelay call in orchestrator mode, a session rebuilt from Conversation Orchestrator.

A rebuilt session carries identity only (conversation id, call_sid, profile, both participants), is not tracked, and can't be used with :meth:send_response: the call's WebSocket is on another instance. If CO holds several conversations for the call (it starts a new one when a conversation closes mid-call), the active one wins, else the newest — this rule applies only to the rebuild path. On the instance holding the call, the live session is returned as-is, so after a mid-call close it still carries the closed conversation's id while other instances return the new one; correlate by call_sid if you need a stable key.

To tell live from rebuilt, check voice_channel.get_websocket(session.conversation_id) is not None — true only for a session live on this instance. A rebuilt session's started_at is the rebuild time, not the call's, and its metadata is always empty.

Cost: on a local miss this makes up to two Conversation Orchestrator requests (list conversations, then participants). Prefer :meth:get_conversation_session_by_call_sid on hot paths, or when TACConfig.instance_public_domain already routes call events to the instance holding the call.

Example
async def on_status(event: CallStatusEvent) -> None:
    session = await voice_channel.resolve_conversation_session_by_call_sid(
        event.call_sid
    )
    if session is not None:
        audit_log(session.conversation_id, session.profile_id, event.call_status)

Parameters:

Name Type Description Default
call_sid str

Twilio Call SID, e.g. from a call event.

required

Returns:

Type Description
ConversationSession | None

The live or rebuilt session, or None — relay-only or Media

ConversationSession | None

Streams mode (no CO conversation behind the call), CO knows no

ConversationSession | None

conversation for it, or the lookup failed (logged).

handle_websocket async

handle_websocket(websocket: WebSocketProtocol) -> None

Handle voice streaming WebSocket connection lifecycle. Delegates to the active provider.

Parameters:

Name Type Description Default
websocket WebSocketProtocol

Any WebSocket implementation satisfying WebSocketProtocol

required

initiate_outbound_conversation async

initiate_outbound_conversation(
    options: InitiateVoiceConversationOptions,
) -> InitiateVoiceConversationResult

Initiate an outbound voice conversation through the active provider.

ConversationRelayProvider, OpenAIRealtimeProvider and GPTLiveProvider all place outbound calls. The Media Streams providers also take a per-call session_config, via InitiateVoiceConversationOptionsOpenAIRealtime or InitiateVoiceConversationOptionsGPTLive. A provider without outbound support raises NotImplementedError.

process_webhook async

process_webhook(
    webhook_data: dict[str, Any],
    idempotency_token: str | None = None,
) -> None

Process conversation webhooks for cleanup and cache invalidation.

Voice channel processes these events:

  • PARTICIPANT_ADDED: report "Conversation Started" when the customer joins on voice (orchestrated ConversationRelay only).
  • CLOSED (CONVERSATION_UPDATED): fire on_conversation_ended. The call's session is normally already released (the WebSocket closed when the caller hung up), so this rebuilds it from Conversation Orchestrator — which is also what makes the hook fire on whichever instance received the webhook rather than only on the one that held the call. If this instance still holds the call's session (the call is live), the hook fires now instead, with a snapshot of that live session, and the session stays until the call's own teardown — which still fires on_call_ended. A single call can then produce a second on_conversation_ended, under a different conversation id, when Conversation Orchestrator later closes the conversation it started for the call's remaining traffic. It also reports "Conversation Ended", with CO's duration.
  • INACTIVE: invalidate cached memory, if this instance holds the session. A call is pinned to one instance for its lifetime, so an INACTIVE landing elsewhere has no cache to clear and is ignored.

Parameters:

Name Type Description Default
webhook_data dict[str, Any]

Raw webhook event data from Twilio

required
idempotency_token str | None

Optional Twilio idempotency token from request headers

None

send_response async

send_response(
    conversation_id: str,
    response: str
    | AsyncGenerator[str | dict[str, Any], None],
    role: str | None = None,
) -> None

Send a response back through this channel's active provider.

Parameters:

Name Type Description Default
conversation_id str

Conversation ID

required
response str | AsyncGenerator[str | dict[str, Any], None]

Response text (string) or async generator for streaming

required
role str | None

Optional message role (not used by ConversationRelayProvider, but kept for API consistency with BaseChannel interface)

None

get_websocket

get_websocket(
    conversation_id: str,
) -> WebSocketProtocol | None

Get the WebSocket connection for a specific conversation.

Parameters:

Name Type Description Default
conversation_id str

Conversation ID

required

Returns:

Type Description
WebSocketProtocol | None

WebSocket connection if exists, None otherwise

WhatsAppChannel

WhatsAppChannel(
    tac: TAC,
    config: WhatsAppChannelConfig
    | dict[str, Any]
    | None = None,
)

Bases: MessagingChannel

WhatsApp Channel for handling WhatsApp-based conversations.

Inherits shared messaging channel webhook processing from MessagingChannel and provides WhatsApp-specific message sending and filtering.

WhatsApp uses WhatsApp sender phone numbers configured in TACConfig (via TWILIO_WHATSAPP_NUMBER). Address format: whatsapp:+1234567890

is_default_agent_address

is_default_agent_address(author_address: str) -> bool

Check if the author address is one of the configured WhatsApp numbers.

get_agent_address

get_agent_address(
    conversation_id: str,
) -> ParticipantAddress

Get the agent's default participant address for this conversation.

initiate_outbound_conversation async

initiate_outbound_conversation(
    options: InitiateMessagingConversationOptions,
) -> InitiateConversationResult

Initiate an outbound WhatsApp conversation.

Creates a conversation via Conversation Orchestrator with inline participants, then sends the initial message via the Actions API. Uses options.from_ when provided (must be one of the configured WhatsApp numbers), otherwise falls back to the default whatsapp_number from TACConfig. If an active conversation with the same addresses already exists (group-by dedup), CO returns 409 and the existing conversation is reused.

Parameters:

Name Type Description Default
options InitiateMessagingConversationOptions

Conversation initiation options (to address and message)

required

Returns:

Type Description
InitiateConversationResult

InitiateConversationResult with conversation_id and session

Raises:

Type Description
ValueError

If options.from_ is set but is not one of the configured WhatsApp numbers.

WhatsAppChannelConfig pydantic-model

Bases: MessagingChannelConfig

Configuration for WhatsApp channel.

Inherits dedup_capacity and memory_mode from MessagingChannelConfig.

Fields:

dedup_capacity pydantic-field

dedup_capacity: int = 10000

Maximum number of idempotency tokens to track for deduplication