# Place one outbound call. Python 3.9+ with requests.
import os
import sys

import requests

base = os.environ.get("RELATIONSAI_API_BASE", "https://voice-api.relationsai.com/v1")
# Defaults to the test-mode Sample agent; set AGENT_ID to use your own agent.
agent_id = os.environ.get("AGENT_ID", "agent_1ZltiWzxrXmMprGD6q63")

r = requests.post(
    f"{base}/calls",
    headers={
        "Authorization": f"Bearer {os.environ['RELATIONSAI_API_KEY']}",
    },
    json={
        "agent_id": agent_id,
        "to": os.environ.get("TO", "+12025550100"),
        "variables": {"first_name": "Asha"},
        "metadata": {"crm_record_id": "L-1042"},
    },
    timeout=30,
)
body = r.json()
if not r.ok:
    err = body["error"]
    sys.exit(f"{r.status_code} {err['code']}: {err['message']} (retry={err['retry']}, request {err['request_id']})")
print(f"{body['id']} status={body['status']} livemode={body['livemode']}")
