Loading...
Loading...
Play audio files, use text-to-speech, and record calls. Use when building IVR systems, playing announcements, or recording conversations. This skill provides Go SDK examples.
npx skill4agent add team-telnyx/skills telnyx-voice-media-gogo get github.com/team-telnyx/telnyx-goimport (
"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")),
)clientimport "errors"
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:
// Rate limited — wait and retry with exponential backoff
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")
}
}401403404422429overlaytarget_legsselfPOST /calls/{call_control_id}/actions/playback_startaudio_typeaudio_urlcache_audioclient_statecommand_idloopmedia_nameoverlayplayback_contentstoptarget_legs response, err := client.Calls.Actions.StartPlayback(
context.Background(),
"call_control_id",
telnyx.CallActionStartPlaybackParams{
AudioURL: "https://example.com/audio.mp3",
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Data)resultcall.playback.endedcall.speak.endedPOST /calls/{call_control_id}/actions/playback_stopclient_statecommand_idoverlaystop response, err := client.Calls.Actions.StopPlayback(
context.Background(),
"call_control_id",
telnyx.CallActionStopPlaybackParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Data)resultPOST /calls/{call_control_id}/actions/record_pauseclient_statecommand_idrecording_id response, err := client.Calls.Actions.PauseRecording(
context.Background(),
"call_control_id",
telnyx.CallActionPauseRecordingParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Data)resultPOST /calls/{call_control_id}/actions/record_resumeclient_statecommand_idrecording_id response, err := client.Calls.Actions.ResumeRecording(
context.Background(),
"call_control_id",
telnyx.CallActionResumeRecordingParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Data)resultcall.recording.savedcall.recording.transcription.savedcall.recording.errorPOST /calls/{call_control_id}/actions/record_startformatchannelsclient_statecommand_idcustom_file_namemax_lengthplay_beeprecording_tracktimeout_secstranscriptiontranscription_enginetranscription_languagetranscription_max_speaker_counttranscription_min_speaker_counttranscription_profanity_filtertranscription_speaker_diarizationtrim response, err := client.Calls.Actions.StartRecording(
context.Background(),
"call_control_id",
telnyx.CallActionStartRecordingParams{
Channels: telnyx.CallActionStartRecordingParamsChannelsSingle,
Format: telnyx.CallActionStartRecordingParamsFormatWav,
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Data)resultcall.recording.savedPOST /calls/{call_control_id}/actions/record_stopclient_statecommand_idrecording_id response, err := client.Calls.Actions.StopRecording(
context.Background(),
"call_control_id",
telnyx.CallActionStopRecordingParams{
StopRecordingRequest: telnyx.StopRecordingRequestParam{},
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Data)resultcall.speak.startedcall.speak.endedPOST /calls/{call_control_id}/actions/speakpayloadvoiceclient_statecommand_idlanguagelooppayload_typeservice_levelstoptarget_legsvoice_settings response, err := client.Calls.Actions.Speak(
context.Background(),
"call_control_id",
telnyx.CallActionSpeakParams{
Payload: "Say this on the call",
Voice: "female",
},
)
if err != nil {
log.Fatal(err)
Language: "en-US",
}
fmt.Printf("%+v\n", response.Data)resulttelnyx-signature-ed25519telnyx-timestamp// In your webhook handler:
func handleWebhook(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
event, err := client.Webhooks.Unwrap(body, r.Header)
if err != nil {
http.Error(w, "Invalid signature", http.StatusBadRequest)
return
}
// Signature valid — event is the parsed webhook payload
fmt.Println("Received event:", event.Data.EventType)
w.WriteHeader(http.StatusOK)
}telnyx-timestamptelnyx-signature-ed25519client.webhooks.unwrap()| Event | Description |
|---|---|
| Call Playback Ended |
| Call Playback Started |
| Call Recording Error |
| Call Recording Saved |
| Call Recording Transcription Saved |
| Call Speak Ended |
| Call Speak Started |
callPlaybackEnded| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.playback.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 | The audio URL being played back, if audio_url has been used to start. |
| string | The name of the audio media file being played back, if media_name has been used to start. |
| boolean | Whether the stopped audio was in overlay mode or not. |
| enum: file_not_found, call_hangup, unknown, cancelled, cancelled_amd, completed, failed | Reflects how command ended. |
| string | Provides details in case of failure. |
callPlaybackStarted| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.playback.started | 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 | The audio URL being played back, if audio_url has been used to start. |
| string | The name of the audio media file being played back, if media_name has been used to start. |
| boolean | Whether the audio is going to be played in overlay mode or not. |
callRecordingError| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.recording.error | 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. |
| enum: Failed to authorize with storage using custom credentials, Invalid credentials json, Unsupported backend, Internal server error | Indication that there was a problem recording the call. |
callRecordingSaved| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.recording.saved | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| 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 | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | State received from a command. |
| date-time | ISO 8601 datetime of when recording started. |
| date-time | ISO 8601 datetime of when recording ended. |
| enum: single, dual | Whether recording was recorded in |
callRecordingTranscriptionSaved| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.recording.transcription.saved | 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. |
| enum: pstn, sip | The type of calling party connection. |
| string | ID that is unique to the recording session and can be used to correlate webhook events. |
| string | ID that is unique to the transcription process and can be used to correlate webhook events. |
| enum: completed | The transcription status. |
| string | The transcribed text |
callSpeakEnded| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.speak.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. |
| enum: completed, call_hangup, cancelled_amd | Reflects how the command ended. |
callSpeakStarted| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.speak.started | 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. |