Loading...
Loading...
End-to-end setup for making a Telnyx AI assistant call a phone number. Covers provisioning a phone number, creating a TeXML application, assigning the number, configuring telephony settings, whitelisting destination countries, and triggering outbound calls via scheduled events. Use this skill (not telnyx-ai-assistants-python) when the task involves an AI assistant placing, making, or triggering an outbound phone call to a user.
npx skill4agent add team-telnyx/skills telnyx-ai-outbound-voice-pythonpip install telnyx requestsimport os
from telnyx import Telnyx
client = Telnyx(api_key=os.environ.get("TELNYX_API_KEY"))telephony_settings.default_texml_app_idclient.ai.assistants.create()for a in client.ai.assistants.list().data:
print(a.model)openai/gpt-4oQwen/Qwen3-235B-A22Bimport time
available = client.available_phone_numbers.list()
phone = available.data[0].phone_number
number_order = client.number_orders.create(
phone_numbers=[{"phone_number": phone}],
)
time.sleep(3)
order = client.number_orders.retrieve(number_order.data.id)
assert order.data.status == "success"
print(f"Purchased: {phone}")voice_urlconnection_idtexml_app = client.texml_applications.create(
friendly_name="My AI Assistant App",
voice_url="https://example.com/placeholder",
)
app_id = texml_app.data.id # This is also the connection_id for phone number assignmentimport requests
requests.patch(
f"https://api.telnyx.com/v2/phone_numbers/{phone}",
headers={
"Authorization": f"Bearer {os.environ['TELNYX_API_KEY']}",
"Content-Type": "application/json",
},
json={"connection_id": app_id},
)import requests
headers = {
"Authorization": f"Bearer {os.environ['TELNYX_API_KEY']}",
"Content-Type": "application/json",
}
# Find the outbound voice profile
r = requests.get(
"https://api.telnyx.com/v2/outbound_voice_profiles", headers=headers
)
ovp_id = r.json()["data"][0]["id"]
# Add destination countries (ISO 3166-1 alpha-2 codes)
requests.patch(
f"https://api.telnyx.com/v2/outbound_voice_profiles/{ovp_id}",
headers=headers,
json={"whitelisted_destinations": ["US", "CA", "IE", "GB"]},
)
# Assign the profile to the TeXML app
requests.patch(
f"https://api.telnyx.com/v2/texml_applications/{app_id}",
headers=headers,
json={
"friendly_name": "My AI Assistant App",
"voice_url": "https://example.com/placeholder",
"outbound": {"outbound_voice_profile_id": ovp_id},
},
)telephony_settingsdefault_texml_app_idscheduled_events.create()assistant = client.ai.assistants.create(
name="My Voice Assistant",
model="openai/gpt-4o",
instructions=(
"You are a helpful phone assistant. "
"Keep your answers concise and conversational since this is a phone call."
),
greeting="Hello! How can I help you today?",
telephony_settings={"default_texml_app_id": app_id},
)client.ai.assistants.update(
assistant_id="your-assistant-id",
telephony_settings={"default_texml_app_id": app_id},
)scheduled_events.create()from datetime import datetime, timezone, timedelta
event = client.ai.assistants.scheduled_events.create(
assistant_id=assistant.id,
telnyx_conversation_channel="phone_call",
telnyx_end_user_target="+13125550001", # Number to call (recipient)
telnyx_agent_target=phone, # Your Telnyx number (caller ID)
scheduled_at_fixed_datetime=(
datetime.now(timezone.utc) + timedelta(seconds=5)
).isoformat(),
)
print(f"Status: {event.status}") # "pending"| Parameter | Type | Required | Description |
|---|---|---|---|
| string (UUID) | Yes | The AI assistant that handles the call. |
| string | Yes | Must be |
| string (E.164) | Yes | Phone number to call (recipient). |
| string (E.164) | Yes | Your Telnyx number (caller ID). Must be assigned to the TeXML app. |
| string (ISO 8601) | Yes | When to place the call. ~5s in the future for immediate. |
| object | No | Variables to pass to the assistant. |
| object | No | Metadata to attach to the conversation. |
import os, time
from datetime import datetime, timezone, timedelta
from telnyx import Telnyx
import requests
api_key = os.environ["TELNYX_API_KEY"]
client = Telnyx(api_key=api_key)
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# 1. Buy a number
available = client.available_phone_numbers.list()
phone = available.data[0].phone_number
order = client.number_orders.create(phone_numbers=[{"phone_number": phone}])
time.sleep(3)
# 2. Create TeXML app
app = client.texml_applications.create(
friendly_name="AI Outbound App",
voice_url="https://example.com/placeholder",
)
app_id = app.data.id
# 3. Assign number
requests.patch(
f"https://api.telnyx.com/v2/phone_numbers/{phone}",
headers=headers,
json={"connection_id": app_id},
)
# 4. Configure outbound profile
ovp = requests.get("https://api.telnyx.com/v2/outbound_voice_profiles", headers=headers).json()["data"][0]
requests.patch(
f"https://api.telnyx.com/v2/outbound_voice_profiles/{ovp['id']}",
headers=headers,
json={"whitelisted_destinations": ["US", "CA"]},
)
requests.patch(
f"https://api.telnyx.com/v2/texml_applications/{app_id}",
headers=headers,
json={
"friendly_name": "AI Outbound App",
"voice_url": "https://example.com/placeholder",
"outbound": {"outbound_voice_profile_id": ovp["id"]},
},
)
# 5. Create assistant with telephony
assistant = client.ai.assistants.create(
name="Outbound Bot",
model="openai/gpt-4o",
instructions="You are a helpful phone assistant.",
telephony_settings={"default_texml_app_id": app_id},
)
# 6. Trigger call
client.ai.assistants.scheduled_events.create(
assistant_id=assistant.id,
telnyx_conversation_channel="phone_call",
telnyx_end_user_target="+13125550001",
telnyx_agent_target=phone,
scheduled_at_fixed_datetime=(datetime.now(timezone.utc) + timedelta(seconds=5)).isoformat(),
)telephony_settings={"default_texml_app_id": app_id}default_texml_app_id"outbound": {"outbound_voice_profile_id": ovp_id}detail.code == "D13"whitelisted_destinationsscheduled_at_fixed_datetimetelnyx_agent_targettelnyx_end_user_targetfor a in client.ai.assistants.list().data:
print(a.model)