0

Speak text to conference participants

by
Published Apr 8, 2025

Convert text to speech and play it to all or some participants.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Speak text to conference participants
7
 * Convert text to speech and play it to all or some participants.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	id: string,
12
	body: {
13
		call_control_ids?: string[]
14
		payload: string
15
		payload_type?: 'text' | 'ssml'
16
		voice: string
17
		voice_settings?: { api_key_ref?: string }
18
		language?:
19
			| 'arb'
20
			| 'cmn-CN'
21
			| 'cy-GB'
22
			| 'da-DK'
23
			| 'de-DE'
24
			| 'en-AU'
25
			| 'en-GB'
26
			| 'en-GB-WLS'
27
			| 'en-IN'
28
			| 'en-US'
29
			| 'es-ES'
30
			| 'es-MX'
31
			| 'es-US'
32
			| 'fr-CA'
33
			| 'fr-FR'
34
			| 'hi-IN'
35
			| 'is-IS'
36
			| 'it-IT'
37
			| 'ja-JP'
38
			| 'ko-KR'
39
			| 'nb-NO'
40
			| 'nl-NL'
41
			| 'pl-PL'
42
			| 'pt-BR'
43
			| 'pt-PT'
44
			| 'ro-RO'
45
			| 'ru-RU'
46
			| 'sv-SE'
47
			| 'tr-TR'
48
		command_id?: string
49
	}
50
) {
51
	const url = new URL(`https://api.telnyx.com/v2/conferences/${id}/actions/speak`)
52

53
	const response = await fetch(url, {
54
		method: 'POST',
55
		headers: {
56
			'Content-Type': 'application/json',
57
			Authorization: 'Bearer ' + auth.apiKey
58
		},
59
		body: JSON.stringify(body)
60
	})
61
	if (!response.ok) {
62
		const text = await response.text()
63
		throw new Error(`${response.status} ${text}`)
64
	}
65
	return await response.json()
66
}
67