用途
Telnyx Account Notifications - Python 是一个 Python SDK 技能示例,用于配置 Telnyx 账户告警与事件的通知渠道和设置。它面向需要在 Python 应用中管理 Telnyx 通知体系的开发者,提供可直接参考的调用示例。
安装与初始化
安装命令:
pip install telnyx
初始化方式:
import os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)
后续所有示例均假设 client 已按上述方式初始化。
主要能力
该技能覆盖以下资源的管理操作:
- 通知渠道(notification_channels):列表、创建、获取、更新、删除。渠道类型枚举为 sms、voice、email、webhook;渠道目标为字符串(如电话号码或 URL)。
- 通知事件条件(notification_event_conditions):列表。返回字段包括 allow_multiple_channels、associated_record_type(枚举:account、phone_number)、asynchronous、description、enabled、name、notification_event_id、parameters、supported_channels 等。
- 通知事件(notification_events):列表。返回字段包括 enabled、name、notification_category 等。
- 通知配置文件(notification_profiles):列表、创建、获取、更新、删除。
- 通知设置(notification_settings):列表、创建、获取、删除。返回字段包括 associated_record_type、associated_record_type_value、notification_channel_id、notification_event_condition_id、notification_profile_id、parameters、status(枚举:enabled、enable-received、enable-pending、enable-submitted、delete-received、delete-pending、delete-submitted、deleted)等。
使用方式
以创建通知渠道为例:
notification_channel = client.notification_channels.create(
channel_type_id="webhook",
channel_destination="https://example.com/webhooks",
)
print(notification_channel.data)
获取通知渠道:
notification_channel = client.notification_channels.retrieve(
"182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
)
print(notification_channel.data)
创建通知配置文件:
notification_profile = client.notification_profiles.create(
name="My Notification Profile",
)
print(notification_profile.data)
列表方法返回自动分页迭代器,可使用 for item in page_result: 自动遍历所有页。
错误处理
所有 API 调用都可能因网络错误、限流(429)、校验错误(422)或认证错误(401)而失败。生产代码中应始终处理错误:
import 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")
常见错误码:401 无效 API 密钥,403 权限不足,404 资源未找到,422 校验错误(检查字段格式),429 限流(使用指数退避重试)。
适用场景
适用于需要在 Python 应用中集成 Telnyx 账户通知管理的开发者,例如通过 webhook、短信、语音或邮件接收账户告警与事件通知,并按通知配置文件与事件条件组织通知设置。
限制
资料未提供收费、价格、平台支持范围、性能指标或具体使用效果等信息。