用途
telnyx-texml-go 是 Telnyx 的 Go 客户端库(SDK),面向使用 TeXML(一种与 TwiML 兼容的标记语言)构建语音应用的开发场景。通过该库,可以在 Go 项目中调用 Telnyx 的 TeXML REST 接口,完成呼叫控制、会议管理、录音与媒体流等操作。
安装与初始化
安装命令:
go get github.com/team-telnyx/telnyx-go
初始化客户端示例:
import ( "context" "fmt" "os"
"github.com/team-telnyx/telnyx-go" "github.com/team-telnyx/telnyx-go/option" )
client := telnyx.NewClient( option.WithAPIKey(os.Getenv("TELNYX_API_KEY")), )
后续所有示例均假设 client 已按上述方式初始化。
主要能力
资料中列出的接口覆盖以下资源:
- 通话(Calls):批量获取账号下的通话资源(该端点最终一致);发起外呼 TeXML 通话(POST /texml/Accounts/{account_sid}/Calls,必填 Url,可选 Texml 对象);获取单个通话(按 CallSid,最终一致);更新通话(请求体字段需按示例使用 CamelCase);获取某通话的录音列表;为通话请求录音;更新通话录音;为通话请求 siprec 会话;更新 siprec 会话;开始向指定 WebSocket 地址推送媒体流;更新通话的流资源。
- 会议(Conferences):列出会议资源;获取单个会议;更新会议;列出会议参会者;拨打新的参会者;获取参会者资源;更新参会者;删除参会者;列出会议录音(资料在会议录音部分被截断)。
通话资源返回字段包括 account_sid、answered_by(human/machine/not_sure)、caller_name、date_created、date_updated、direction(inbound/outbound)、duration、end_time、from、from_formatted、price、price_unit、sid、start_time、status(ringing/in-progress/canceled/completed/failed/busy/no-answer)、to、to_formatted、uri。
录音资源返回字段包括 account_sid、call_sid、channels(1 或 2)、conference_sid、date_created、date_updated、duration、error_code、price、price_unit、sid、source(StartCallRecordingAPI/StartConferenceRecordingAPI/OutboundAPI/DialVerb/Conference/RecordVerb/Trunking)、start_time、track(inbound/outbound/both)、uri。
siprec 资源返回字段包括 account_sid、call_sid、date_created、date_updated、error_code、sid、start_time、status(in-progress/stopped)、track(both_tracks/inbound_track/outbound_track)、uri。
流资源返回字段包括 account_sid、call_sid、date_updated、name、sid、status、uri。
会议资源返回字段包括 account_sid、api_version、call_sid_ending_conference、date_created、date_updated、friendly_name、reason_conference_ended、region、sid、status(init/in-progress/completed)、subresource_uris、uri。
参会者资源返回字段包括 account_sid、api_version、call_sid、call_sid_legacy、coaching、coaching_call_sid、coaching_call_sid_legacy、conference_sid、date_created、date_updated、end_conference_on_exit、hold、muted、status(connecting/connected/completed)、uri。
使用方式
调用接口的典型模式为先构造带上下文的请求参数,再调用对应方法,最后处理返回值和错误。例如发起外呼:
response, err := client.Texml.Accounts.Calls.Calls( context.Background(), "account_sid", telnyx.TexmlAccountCallCallsParams{ OfWithURL: &telnyx.TexmlAccountCallCallsParamsParamsWithURL{ URL: "https://www.example.com/texml.xml", }, }, )
错误处理:所有 API 调用都可能因网络错误、限流(429)、校验错误(422)或认证错误(401)而失败,生产代码中应始终处理错误:
result, err := client.Messages.Send(ctx, params) if err != nil { var apiErr *telnyx.Error if errors.As(err, &apiErr) { switch apiErr.StatusCode { case 422: fmt.Println("Validation error — check required fields and formats") case 429: fmt.Println("Rate limited, retrying...") default: fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Error()) } } else { fmt.Println("Network error — check connectivity and retry") } }
常见错误码:401 无效 API key,403 权限不足,404 资源未找到,422 校验错误,429 被限流(建议指数退避重试)。
分页:使用 ListAutoPaging() 自动遍历,例如 iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }。
适用场景
适用于需要在 Go 服务中通过 TeXML 标记语言编排语音流程的场景,例如外呼、通话录音、会议与参会者管理、siprec 会话以及媒体流转发等。发起外呼时,Telnyx 会从 Mission Control Portal 中为该连接配置的 XML Request URL 请求 TeXML 文档。
限制与说明
资料仅提供接口定义、示例与错误码说明,未提及收费、价格、平台兼容性、性能或效果等信息,这些内容暂未提供。部分内容(如会议录音列表示例)在资料中被截断。