import { CodeBlock } from '../views/docs/CodeBlock';
import { Callout, NextLink, Prose, PropTable } from '../views/docs/prose';

## Twilio Programmable Voice (TwiML)

<Callout><strong>Looking for Twilio Elastic SIP?</strong> Check out our [guide here](./twilio-elastic-sip).</Callout>

If you already use Twilio Programmable Voice / TwiML, you can transfer to a Guava agent at any time during your call.

1. Create a Guava SIP code "guavasip-xxx" [on the Guava dashboard](https://app.goguava.ai/dashboard/sip).
2. Attach an agent to the SIP code.
3. Use the following TwiML template to transfer to your agent.

<CodeBlock
  language="xml"
  code={`<Response>
    <Dial>
      <Sip>sip:guavasip-xxx@sip.goguava.ai</Sip>
    </Dial>
</Response>`}
/>

Below is a more detailed guide.

### Create a Guava SIP code

Every SIP integration in Guava requires a `guavasip` code. `guavasip` codes are used to route inbound calls to agents — agents can listen to codes and peers dial them.

Open the [SIP page in the Guava dashboard](https://app.goguava.ai/dashboard/sip) and click **Create SIP Code**.

<p style={{display: "flex", justifyContent: "center", margin: "1.5rem 0"}}>
    <img style={{maxWidth: "700px", width: "100%", borderRadius: "6px"}} src="/docs/sip-dashboard.png" alt="Guava SIP dashboard" />
</p>

Take note of the SIP code and the termination URI.


### Start an agent

Next, start an agent using `agent.listen_sip("guavasip-xxx")` — Guava forwards every call addressed to that code to your agent.

<CodeBlock
  filename="main.py"
  language="python"
  code={`import os
import guava

agent = guava.Agent(
    name="Nova",
    organization="Acme Corp",
)

# Register handlers — on_call_received, on_call_start, etc.

# Replace with your SIP code from the dashboard.
agent.listen_sip("guavasip-xxx")`}
/>


### Twilio Example: Outbound Call

Initiate an outbound call through Twilio, then use a `Dial` command to transfer the call to your Guava agent using the termination URI.

<CodeBlock
  filename="twilio_outbound.py"
  language="python"
  code={`import os
from twilio.rest import Client

client = Client(
    os.environ["TWILIO_ACCOUNT_SID"],
    os.environ["TWILIO_AUTH_TOKEN"]
)

client.calls.create(
    to="+1...", # The recipient of the call.
    from_="+1...", # Your owned Twilio number.
    twiml="<Response><Dial><Sip>sip:guavasip-xxx@sip.goguava.ai</Sip></Dial></Response>",
)`}
/>
