Loading...
Loading...
Compare original and translation side by side
pip install telnyxpip install telnyximport os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)clientimport os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)clientimport telnyx
try:
result = client.messages.send(to="+13125550001", from_="+13125550002", text="Hello")
except telnyx.APIConnectionError:
print("Network error — check connectivity and retry")
except telnyx.RateLimitError:
# 429: rate limited — wait and retry with exponential backoff
import time
time.sleep(1) # Check Retry-After header for actual delay
except telnyx.APIStatusError as e:
print(f"API error {e.status_code}: {e.message}")
if e.status_code == 422:
print("Validation error — check required fields and formats")401403404422429import telnyx
try:
result = client.messages.send(to="+13125550001", from_="+13125550002", text="Hello")
except telnyx.APIConnectionError:
print("Network error — check connectivity and retry")
except telnyx.RateLimitError:
# 429: 触发速率限制 — 等待后使用指数退避策略重试
import time
time.sleep(1) # 可查看Retry-After请求头获取建议的等待时长
except telnyx.APIStatusError as e:
print(f"API error {e.status_code}: {e.message}")
if e.status_code == 422:
print("校验错误 — 请检查必填字段和格式是否正确")401403404422429POST /calls/{call_control_id}/actions/ai_assistant_add_messagesclient_statecommand_idmessagesresponse = client.calls.actions.add_ai_assistant_messages(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultPOST /calls/{call_control_id}/actions/ai_assistant_add_messagesclient_statecommand_idmessagesresponse = client.calls.actions.add_ai_assistant_messages(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultcall.conversation.endedcall.conversation_insights.generatedPOST /calls/{call_control_id}/actions/ai_assistant_startassistantclient_statecommand_idgreetinginterruption_settingsmessage_historyparticipantssend_message_history_updatestranscriptionvoicevoice_settingsresponse = client.calls.actions.start_ai_assistant(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)conversation_idresultcall.conversation.endedcall.conversation_insights.generatedPOST /calls/{call_control_id}/actions/ai_assistant_startassistantclient_statecommand_idgreetinginterruption_settingsmessage_historyparticipantssend_message_history_updatestranscriptionvoicevoice_settingsresponse = client.calls.actions.start_ai_assistant(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)conversation_idresultPOST /calls/{call_control_id}/actions/ai_assistant_stopclient_statecommand_idresponse = client.calls.actions.stop_ai_assistant(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultPOST /calls/{call_control_id}/actions/ai_assistant_stopclient_statecommand_idresponse = client.calls.actions.stop_ai_assistant(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultAnswergatherPOST /calls/{call_control_id}/actions/gatherclient_statecommand_idgather_idinitial_timeout_millisinter_digit_timeout_millismaximum_digitsminimum_digitsterminating_digittimeout_millisvalid_digitsresponse = client.calls.actions.gather(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
minimum_digits=1,
maximum_digits=4,
)
print(response.data)resultgatherAnswerPOST /calls/{call_control_id}/actions/gatherclient_statecommand_idgather_idinitial_timeout_millisinter_digit_timeout_millismaximum_digitsminimum_digitsterminating_digittimeout_millisvalid_digitsresponse = client.calls.actions.gather(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
minimum_digits=1,
maximum_digits=4,
)
print(response.data)resultcall.gather.endedPOST /calls/{call_control_id}/actions/gather_stopclient_statecommand_idresponse = client.calls.actions.stop_gather(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultcall.gather.endedPOST /calls/{call_control_id}/actions/gather_stopclient_statecommand_idresponse = client.calls.actions.stop_gather(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultPOST /calls/{call_control_id}/actions/gather_using_aiparametersassistantclient_statecommand_idgather_ended_speechgreetinginterruption_settingslanguagemessage_historysend_message_history_updatessend_partial_resultstranscriptionuser_response_timeout_msvoicevoice_settingsresponse = client.calls.actions.gather_using_ai(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
parameters={
"properties": "bar",
"required": "bar",
"type": "bar",
},
)
print(response.data)conversation_idresultPOST /calls/{call_control_id}/actions/gather_using_aiparametersassistantclient_statecommand_idgather_ended_speechgreetinginterruption_settingslanguagemessage_historysend_message_history_updatessend_partial_resultstranscriptionuser_response_timeout_msvoicevoice_settingsresponse = client.calls.actions.gather_using_ai(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
parameters={
"properties": "bar",
"required": "bar",
"type": "bar",
},
)
print(response.data)conversation_idresultPOST /calls/{call_control_id}/actions/gather_using_audioaudio_urlclient_statecommand_idinter_digit_timeout_millisinvalid_audio_urlinvalid_media_namemaximum_digitsmaximum_triesmedia_nameminimum_digitsterminating_digittimeout_millisvalid_digitsresponse = client.calls.actions.gather_using_audio(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultinvalid_audio_urlPOST /calls/{call_control_id}/actions/gather_using_audioaudio_urlclient_statecommand_idinter_digit_timeout_millisinvalid_audio_urlinvalid_media_namemaximum_digitsmaximum_triesmedia_nameminimum_digitsterminating_digittimeout_millisvalid_digitsresponse = client.calls.actions.gather_using_audio(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultPOST /calls/{call_control_id}/actions/gather_using_speakvoicepayloadclient_statecommand_idinter_digit_timeout_millisinvalid_payloadlanguagemaximum_digitsmaximum_triesminimum_digitspayload_typeservice_levelterminating_digittimeout_millisvalid_digitsvoice_settingsresponse = client.calls.actions.gather_using_speak(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
payload="say this on call",
voice="male",
)
print(response.data)resultinvalid_payloadPOST /calls/{call_control_id}/actions/gather_using_speakvoicepayloadclient_statecommand_idinter_digit_timeout_millisinvalid_payloadlanguagemaximum_digitsmaximum_triesminimum_digitspayload_typeservice_levelterminating_digittimeout_millisvalid_digitsvoice_settingsresponse = client.calls.actions.gather_using_speak(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
payload="say this on call",
voice="male",
)
print(response.data)resulttelnyx-signature-ed25519telnyx-timestampundefinedtelnyx-signature-ed25519telnyx-timestampundefined
The following webhook events are sent to your configured webhook URL.
All webhooks include `telnyx-timestamp` and `telnyx-signature-ed25519` headers for Ed25519 signature verification. Use `client.webhooks.unwrap()` to verify.
| Event | Description |
|-------|-------------|
| `CallAIGatherEnded` | Call AI Gather Ended |
| `CallAIGatherMessageHistoryUpdated` | Call AI Gather Message History Updated |
| `CallAIGatherPartialResults` | Call AI Gather Partial Results |
| `callGatherEnded` | Call Gather Ended |
以下Webhook事件会发送到你配置的Webhook URL。所有Webhooks都包含`telnyx-timestamp`和`telnyx-signature-ed25519`头用于Ed25519签名验证,可使用`client.webhooks.unwrap()`方法完成验证。
| 事件 | 描述 |
|-------|-------------|
| `CallAIGatherEnded` | 通话AI采集结束 |
| `CallAIGatherMessageHistoryUpdated` | 通话AI采集消息历史更新 |
| `CallAIGatherPartialResults` | 通话AI采集返回部分结果 |
| `callGatherEnded` | 通话采集结束 |CallAIGatherEnded| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.ai_gather.ended | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Telnyx connection ID used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Number or SIP URI placing the call. |
| string | Destination number or SIP URI of the call. |
| array[object] | The history of the messages exchanged during the AI gather |
| object | The result of the AI gather, its type depends of the |
| enum: valid, invalid | Reflects how command ended. |
CallAIGatherMessageHistoryUpdated| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.ai_gather.message_history_updated | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Telnyx connection ID used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Number or SIP URI placing the call. |
| string | Destination number or SIP URI of the call. |
| array[object] | The history of the messages exchanged during the AI gather |
CallAIGatherPartialResults| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.ai_gather.partial_results | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Telnyx connection ID used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Number or SIP URI placing the call. |
| string | Destination number or SIP URI of the call. |
| array[object] | The history of the messages exchanged during the AI gather |
| object | The partial result of the AI gather, its type depends of the |
callGatherEnded| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.gather.ended | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Number or SIP URI placing the call. |
| string | Destination number or SIP URI of the call. |
| string | The received DTMF digit or symbol. |
| enum: valid, invalid, call_hangup, cancelled, cancelled_amd, timeout | Reflects how command ended. |
CallAIGatherEnded| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型 |
| 枚举: call.ai_gather.ended | 交付的事件类型 |
| uuid | 资源标识ID |
| date-time | 事件发生的ISO 8601格式时间戳 |
| 字符串 | 用于通过呼叫控制API下发指令的通话ID |
| 字符串 | 本次通话使用的Telnyx连接ID |
| 字符串 | 通话唯一ID,可用于关联Webhook事件 |
| 字符串 | 通话会话唯一ID,可用于关联Webhook事件 |
| 字符串 | 从指令中获取的状态信息 |
| 字符串 | 主叫号码或SIP URI |
| 字符串 | 被叫号码或SIP URI |
| 对象数组 | AI采集过程中 exchanged的消息历史 |
| 对象 | AI采集的结果,类型取决于指令中传入的 |
| 枚举: valid, invalid | 指令结束的状态 |
CallAIGatherMessageHistoryUpdated| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型 |
| 枚举: call.ai_gather.message_history_updated | 交付的事件类型 |
| uuid | 资源标识ID |
| date-time | 事件发生的ISO 8601格式时间戳 |
| 字符串 | 用于通过呼叫控制API下发指令的通话ID |
| 字符串 | 本次通话使用的Telnyx连接ID |
| 字符串 | 通话唯一ID,可用于关联Webhook事件 |
| 字符串 | 通话会话唯一ID,可用于关联Webhook事件 |
| 字符串 | 从指令中获取的状态信息 |
| 字符串 | 主叫号码或SIP URI |
| 字符串 | 被叫号码或SIP URI |
| 对象数组 | AI采集过程中 exchanged的消息历史 |
CallAIGatherPartialResults| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型 |
| 枚举: call.ai_gather.partial_results | 交付的事件类型 |
| uuid | 资源标识ID |
| date-time | 事件发生的ISO 8601格式时间戳 |
| 字符串 | 用于通过呼叫控制API下发指令的通话ID |
| 字符串 | 本次通话使用的Telnyx连接ID |
| 字符串 | 通话唯一ID,可用于关联Webhook事件 |
| 字符串 | 通话会话唯一ID,可用于关联Webhook事件 |
| 字符串 | 从指令中获取的状态信息 |
| 字符串 | 主叫号码或SIP URI |
| 字符串 | 被叫号码或SIP URI |
| 对象数组 | AI采集过程中 exchanged的消息历史 |
| 对象 | AI采集的部分结果,类型取决于指令中传入的 |
callGatherEnded| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型 |
| 枚举: call.gather.ended | 交付的事件类型 |
| uuid | 资源标识ID |
| date-time | 事件发生的ISO 8601格式时间戳 |
| 字符串 | 用于通过呼叫控制API下发指令的通话ID |
| 字符串 | 本次通话使用的呼叫控制应用ID(原Telnyx连接ID) |
| 字符串 | 通话唯一ID,可用于关联Webhook事件 |
| 字符串 | 通话会话唯一ID,可用于关联Webhook事件 |
| 字符串 | 从指令中获取的状态信息 |
| 字符串 | 主叫号码或SIP URI |
| 字符串 | 被叫号码或SIP URI |
| 字符串 | 收到的DTMF数字或符号 |
| 枚举: valid, invalid, call_hangup, cancelled, cancelled_amd, timeout | 指令结束的状态 |