0

Gather using speak

by
Published Apr 8, 2025

Convert text to speech and play it on the call until the required DTMF signals are gathered to build interactive menus.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Gather using speak
7
 * Convert text to speech and play it on the call until the required DTMF signals are gathered to build interactive menus.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	call_control_id: string,
12
	body: {
13
		payload: string
14
		invalid_payload?: string
15
		payload_type?: 'text' | 'ssml'
16
		service_level?: 'basic' | 'premium'
17
		voice: string
18
		voice_settings?: { api_key_ref?: string }
19
		language?:
20
			| 'arb'
21
			| 'cmn-CN'
22
			| 'cy-GB'
23
			| 'da-DK'
24
			| 'de-DE'
25
			| 'en-AU'
26
			| 'en-GB'
27
			| 'en-GB-WLS'
28
			| 'en-IN'
29
			| 'en-US'
30
			| 'es-ES'
31
			| 'es-MX'
32
			| 'es-US'
33
			| 'fr-CA'
34
			| 'fr-FR'
35
			| 'hi-IN'
36
			| 'is-IS'
37
			| 'it-IT'
38
			| 'ja-JP'
39
			| 'ko-KR'
40
			| 'nb-NO'
41
			| 'nl-NL'
42
			| 'pl-PL'
43
			| 'pt-BR'
44
			| 'pt-PT'
45
			| 'ro-RO'
46
			| 'ru-RU'
47
			| 'sv-SE'
48
			| 'tr-TR'
49
		minimum_digits?: number
50
		maximum_digits?: number
51
		maximum_tries?: number
52
		timeout_millis?: number
53
		terminating_digit?: string
54
		valid_digits?: string
55
		inter_digit_timeout_millis?: number
56
		client_state?: string
57
		command_id?: string
58
	}
59
) {
60
	const url = new URL(
61
		`https://api.telnyx.com/v2/calls/${call_control_id}/actions/gather_using_speak`
62
	)
63

64
	const response = await fetch(url, {
65
		method: 'POST',
66
		headers: {
67
			'Content-Type': 'application/json',
68
			Authorization: 'Bearer ' + auth.apiKey
69
		},
70
		body: JSON.stringify(body)
71
	})
72
	if (!response.ok) {
73
		const text = await response.text()
74
		throw new Error(`${response.status} ${text}`)
75
	}
76
	return await response.json()
77
}
78