
    ړi                         U d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZ  G d d      Zdaee   ed	<   d
efdZdedefdZy)zI
Twilio Client Module

Provides Twilio API wrapper and TwiML generation.
    N)Optional)Client   )TWILIO_ACCOUNT_SIDTWILIO_AUTH_TOKENWEBSOCKET_URLSTREAM_REDIRECT_URLvalidate_twilio_credentialslog_callc            	           e Zd ZdZddeefdedededefdZede	fd	       Z
dd
edefdZdedefdZdefdZdefdZdedefdZdefdZy)TwilioHelperzc
    Helper class for Twilio operations.

    Provides TwiML generation and call manipulation.
    Naccount_sid
auth_tokenwebsocket_urlstream_redirect_urlc                 j    |xs t         | _        |xs t        | _        || _        || _        d| _        y)aK  
        Initialize Twilio helper.

        Args:
            account_sid: Twilio account SID (uses env var if not provided)
            auth_token: Twilio auth token (uses env var if not provided)
            websocket_url: URL for media stream WebSocket
            stream_redirect_url: URL for stream redirect after TTS
        N)r   r   r   r   r   r   _client)selfr   r   r   r   s        ;/home/sas/my/fyp/ringai/ringai/twilio_integration/client.py__init__zTwilioHelper.__init__   s6      '<*<$9(9*#6 )-    returnc                     | j                   H| j                  r| j                  st        d      t	        | j                  | j                        | _         | j                   S )z)Get Twilio client, initializing if neededzfTwilio credentials not set. Please set TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables.)r   r   r   RuntimeErrorr   r   s    r   clientzTwilioHelper.client3   sS     <<##4??"?  "$"2"2DOODDL||r   custom_greetingc                 4    |xs d}d| d| j                    dS )z
        Build TwiML for incoming call greeting.

        Args:
            custom_greeting: Optional custom greeting text

        Returns:
            TwiML XML string
        z<Say voice="alice">Hello.</Say>
  <Pause length="1"/>
  <Say voice="alice">This is Ring A I.</Say>
  <Pause length="1"/>
  <Say voice="alice">Please speak clearly after the tone.</Say>z4<?xml version="1.0" encoding="UTF-8"?>
<Response>
  z
  <Connect>
    <Stream url=""/>
  </Connect>
</Response>r   )r   r   greetings      r   build_greeting_twimlz!TwilioHelper.build_greeting_twiml?   sA     # 
N 	* $$% &	 	r   textc                 (    d| d| j                    dS )z
        Build TwiML for AI response.

        Args:
            text: Text to speak

        Returns:
            TwiML XML string
        zG<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say voice="alice">z7</Say>
  <Pause length="1"/>
  <Redirect method="POST">z</Redirect>
</Response>)r   )r   r#   s     r   build_say_twimlzTwilioHelper.build_say_twimlY   s,    V 334 5	 	r   c                 "    d| j                    dS )zk
        Build TwiML for resuming stream after TTS.

        Returns:
            TwiML XML string
        zO<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Connect>
    <Stream url="r   r    r   s    r   build_stream_twimlzTwilioHelper.build_stream_twimlj   s$     $$% & 	r   c                      y)z\
        Build TwiML for hanging up.

        Returns:
            TwiML XML string
        zI<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Hangup/>
</Response> r   s    r   build_hangup_twimlzTwilioHelper.build_hangup_twimlx   s    r   call_idc                   K   t        j                  d||       | j                  |      }	 t        j                  | j
                  j                  |      j                  |       d{    y7 # t        $ rJ}dt        |      v sdt        |      j                         v rt        j                  d|       n Y d}~yd}~ww xY ww)z~
        Send TTS to active call.

        Args:
            call_id: Twilio call SID
            text: Text to speak
        zcall=%s TTS -> %r)twimlN21220znot in-progressz&call=%s already ended, cannot send TTS)r   infor%   asyncio	to_threadr   callsupdate	Exceptionstrlower)r   r+   r#   r-   es        r   sayzTwilioHelper.say   s      	)7D9$$T*
	##!!'*11    	#a& $5Q$GFP Q	sA   )C=A1 )A/*A1 .C/A1 1	C:A B?:C?CCc                 T    | j                   j                  |      j                         S )zGet call resource by SID)r   r2   fetch)r   r+   s     r   get_callzTwilioHelper.get_call   s     {{  )//11r   )N)__name__
__module____qualname____doc__r   r	   r5   r   propertyr   r   r"   r%   r'   r*   r8   r;   r)   r   r   r   r      s      *#6.. . 	.
 !., 	 	 	C 3 4C C "C 
C 
 C 02 2r   r   _twilio_helperr   c                  .    t         
t               a t         S )z!Get global Twilio helper instance)rA   r   r)   r   r   get_twilio_clientrC      s     %r   r+   r#   c                 X   K   t               }|j                  | |       d{    y7 w)z\
    Convenience function to send TTS to call.

    Uses global Twilio helper instance.
    N)rC   r8   )r+   r#   helpers      r   trigger_sayrF      s$       F
**Wd
###s    *(*)r?   r0   typingr   twilio.restr   configr   r   r   r	   r
   r   r   rA   __annotations__rC   r5   rF   r)   r   r   <module>rK      s\       H2 H2X *.& -< $s $# $r   