0

Answer call

by
Published Apr 8, 2025

Answer an incoming call. You must issue this command before executing subsequent commands on an incoming call. **Expected Webhooks (see [callback schema](https://developers.telnyx.com/api/call-control/answer-call#callbacks) below):** - `call.answered` - `streaming.started`, `streaming.stopped` or `streaming.failed` if `stream_url` was set

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Answer call
7
 * Answer an incoming call. You must issue this command before executing subsequent commands on an incoming call.
8

9
**Expected Webhooks (see [callback schema](https://developers.telnyx.com/api/call-control/answer-call#callbacks) below):**
10

11
- `call.answered`
12
- `streaming.started`, `streaming.stopped` or `streaming.failed` if `stream_url` was set
13

14
 */
15
export async function main(
16
	auth: Telnyx,
17
	call_control_id: string,
18
	body: {
19
		billing_group_id?: string
20
		client_state?: string
21
		command_id?: string
22
		custom_headers?: { name: string; value: string }[]
23
		preferred_codecs?: 'G722,PCMU,PCMA,G729,OPUS,VP8,H264'
24
		sip_headers?: { name: 'User-to-User'; value: string }[]
25
		sound_modifications?: {
26
			pitch?: number
27
			semitone?: number
28
			octaves?: number
29
			track?: string
30
		}
31
		stream_url?: string
32
		stream_track?: 'inbound_track' | 'outbound_track' | 'both_tracks'
33
		stream_bidirectional_mode?: 'mp3' | 'rtp'
34
		stream_bidirectional_codec?: 'PCMU' | 'PCMA' | 'G722'
35
		stream_bidirectional_target_legs?: 'both' | 'self' | 'opposite'
36
		send_silence_when_idle?: false | true
37
		webhook_url?: string
38
		webhook_url_method?: 'POST' | 'GET'
39
		transcription?: false | true
40
		transcription_config?: {
41
			transcription_engine?: 'A' | 'B'
42
			language?:
43
				| 'af'
44
				| 'sq'
45
				| 'am'
46
				| 'ar'
47
				| 'hy'
48
				| 'az'
49
				| 'eu'
50
				| 'bn'
51
				| 'bs'
52
				| 'bg'
53
				| 'my'
54
				| 'ca'
55
				| 'yue'
56
				| 'zh'
57
				| 'hr'
58
				| 'cs'
59
				| 'da'
60
				| 'nl'
61
				| 'en'
62
				| 'et'
63
				| 'fil'
64
				| 'fi'
65
				| 'fr'
66
				| 'gl'
67
				| 'ka'
68
				| 'de'
69
				| 'el'
70
				| 'gu'
71
				| 'iw'
72
				| 'hi'
73
				| 'hu'
74
				| 'is'
75
				| 'id'
76
				| 'it'
77
				| 'ja'
78
				| 'jv'
79
				| 'kn'
80
				| 'kk'
81
				| 'km'
82
				| 'ko'
83
				| 'lo'
84
				| 'lv'
85
				| 'lt'
86
				| 'mk'
87
				| 'ms'
88
				| 'ml'
89
				| 'mr'
90
				| 'mn'
91
				| 'ne'
92
				| 'no'
93
				| 'fa'
94
				| 'pl'
95
				| 'pt'
96
				| 'pa'
97
				| 'ro'
98
				| 'ru'
99
				| 'rw'
100
				| 'sr'
101
				| 'si'
102
				| 'sk'
103
				| 'sl'
104
				| 'ss'
105
				| 'st'
106
				| 'es'
107
				| 'su'
108
				| 'sw'
109
				| 'sv'
110
				| 'ta'
111
				| 'te'
112
				| 'th'
113
				| 'tn'
114
				| 'tr'
115
				| 'ts'
116
				| 'uk'
117
				| 'ur'
118
				| 'uz'
119
				| 've'
120
				| 'vi'
121
				| 'xh'
122
				| 'zu'
123
				| 'he'
124
				| 'la'
125
				| 'mi'
126
				| 'cy'
127
				| 'br'
128
				| 'sn'
129
				| 'yo'
130
				| 'so'
131
				| 'oc'
132
				| 'be'
133
				| 'tg'
134
				| 'sd'
135
				| 'yi'
136
				| 'fo'
137
				| 'ht'
138
				| 'ps'
139
				| 'tk'
140
				| 'nn'
141
				| 'mt'
142
				| 'sa'
143
				| 'lb'
144
				| 'bo'
145
				| 'tl'
146
				| 'mg'
147
				| 'as'
148
				| 'tt'
149
				| 'haw'
150
				| 'ln'
151
				| 'ha'
152
				| 'ba'
153
				| 'jw'
154
				| 'auto_detect'
155
			interim_results?: false | true
156
			enable_speaker_diarization?: false | true
157
			min_speaker_count?: number
158
			max_speaker_count?: number
159
			client_state?: string
160
			transcription_tracks?: string
161
			command_id?: string
162
		}
163
		record?: 'record-from-answer'
164
		record_channels?: 'single' | 'dual'
165
		record_format?: 'mp3' | 'wav'
166
		record_max_length?: number
167
		record_timeout_secs?: number
168
		record_track?: 'both' | 'inbound' | 'outbound'
169
		record_trim?: 'trim-silence'
170
		record_custom_file_name?: string
171
	}
172
) {
173
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/answer`)
174

175
	const response = await fetch(url, {
176
		method: 'POST',
177
		headers: {
178
			'Content-Type': 'application/json',
179
			Authorization: 'Bearer ' + auth.apiKey
180
		},
181
		body: JSON.stringify(body)
182
	})
183
	if (!response.ok) {
184
		const text = await response.text()
185
		throw new Error(`${response.status} ${text}`)
186
	}
187
	return await response.json()
188
}
189