0

Streaming start

by
Published Apr 8, 2025

Start streaming the media from a call to a specific WebSocket address or Dialogflow connection in near-realtime. Audio will be delivered as base64-encoded RTP payload (raw audio), wrapped in JSON payloads. Please find more details about media streaming messages specification under the [link](https://developers.telnyx.com/docs/voice/programmable-voice/media-streaming).

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Streaming start
7
 * Start streaming the media from a call to a specific WebSocket address or Dialogflow connection in near-realtime. Audio will be delivered as base64-encoded RTP payload (raw audio), wrapped in JSON payloads.
8

9
Please find more details about media streaming messages specification under the [link](https://developers.telnyx.com/docs/voice/programmable-voice/media-streaming).
10
 */
11
export async function main(
12
	auth: Telnyx,
13
	call_control_id: string,
14
	body: {
15
		stream_url?: string
16
		stream_track?: 'inbound_track' | 'outbound_track' | 'both_tracks'
17
		stream_bidirectional_mode?: 'mp3' | 'rtp'
18
		stream_bidirectional_codec?: 'PCMU' | 'PCMA' | 'G722'
19
		stream_bidirectional_target_legs?: 'both' | 'self' | 'opposite'
20
		enable_dialogflow?: false | true
21
		dialogflow_config?: {
22
			analyze_sentiment?: false | true
23
			partial_automated_agent_reply?: false | true
24
		}
25
		client_state?: string
26
		command_id?: string
27
	}
28
) {
29
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/streaming_start`)
30

31
	const response = await fetch(url, {
32
		method: 'POST',
33
		headers: {
34
			'Content-Type': 'application/json',
35
			Authorization: 'Bearer ' + auth.apiKey
36
		},
37
		body: JSON.stringify(body)
38
	})
39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`${response.status} ${text}`)
42
	}
43
	return await response.json()
44
}
45