0

Send an RCS message

by
Published Apr 8, 2025
Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Send an RCS message
7
 *
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	body: {
12
		bot_id: string
13
		to: string
14
		messaging_profile_id: string
15
		type?: 'RCS'
16
		agent_message: {
17
			content_message?: {
18
				suggestions?: {
19
					reply?: { text?: string; postback_data?: string }
20
					action?: {
21
						text?: string
22
						postback_data?: string
23
						fallback_url?: string
24
						dial_action?: { phone_number: string }
25
						view_location_action?: {
26
							lat_long?: { latitude: number; longitude: number }
27
							label?: string
28
							query?: string
29
						}
30
						create_calendar_event_action?: {
31
							start_time?: string
32
							end_time?: string
33
							title?: string
34
							description?: string
35
						}
36
						open_url_action?: {
37
							url: string
38
							application: 'OPEN_URL_APPLICATION_UNSPECIFIED' | 'BROWSER' | 'WEBVIEW'
39
							webview_view_mode: 'WEBVIEW_VIEW_MODE_UNSPECIFIED' | 'FULL' | 'HALF' | 'TALL'
40
							description?: string
41
						}
42
						share_location_action?: {}
43
						compose_action?: {
44
							compose_text_message?: { phone_number: string; text?: string }
45
							compose_recording_message?: {
46
								phone_number?: string
47
								type:
48
									| 'COMPOSE_RECORDING_ACTION_TYPE_UNSPECIFIED'
49
									| 'ACTION_TYPE_AUDIO'
50
									| 'ACTION_TYPE_VIDEO'
51
							}
52
						}
53
					}
54
				}[]
55
				text?: string
56
				rich_card?: {
57
					carousel_card?: {
58
						card_width?: 'CARD_WIDTH_UNSPECIFIED' | 'SMALL' | 'MEDIUM'
59
						card_contents?: {
60
							title?: string
61
							description?: string
62
							media?: {
63
								height?: 'TALL' | 'MEDIUM' | 'HEIGHT_UNSPECIFIED' | 'SHORT'
64
								content_info?: {
65
									file_url?: string
66
									thumbnail_url?: string
67
									force_refresh?: false | true
68
								}
69
							}
70
							suggestions?: {
71
								reply?: { text?: string; postback_data?: string }
72
								action?: {
73
									text?: string
74
									postback_data?: string
75
									fallback_url?: string
76
									dial_action?: { phone_number: string }
77
									view_location_action?: {
78
										lat_long?: { latitude: number; longitude: number }
79
										label?: string
80
										query?: string
81
									}
82
									create_calendar_event_action?: {
83
										start_time?: string
84
										end_time?: string
85
										title?: string
86
										description?: string
87
									}
88
									open_url_action?: {
89
										url: string
90
										application: 'OPEN_URL_APPLICATION_UNSPECIFIED' | 'BROWSER' | 'WEBVIEW'
91
										webview_view_mode: 'WEBVIEW_VIEW_MODE_UNSPECIFIED' | 'FULL' | 'HALF' | 'TALL'
92
										description?: string
93
									}
94
									share_location_action?: {}
95
									compose_action?: {
96
										compose_text_message?: {
97
											phone_number: string
98
											text?: string
99
										}
100
										compose_recording_message?: {
101
											phone_number?: string
102
											type:
103
												| 'COMPOSE_RECORDING_ACTION_TYPE_UNSPECIFIED'
104
												| 'ACTION_TYPE_AUDIO'
105
												| 'ACTION_TYPE_VIDEO'
106
										}
107
									}
108
								}
109
							}[]
110
						}[]
111
					}
112
					standalone_card?: {
113
						card_orientation: 'CARD_ORIENTATION_UNSPECIFIED' | 'HORIZONTAL' | 'VERTICAL'
114
						thumbnail_image_alignment: 'THUMBNAIL_IMAGE_ALIGNMENT_UNSPECIFIED' | 'LEFT' | 'RIGHT'
115
						card_content: {
116
							title?: string
117
							description?: string
118
							media?: {
119
								height?: 'TALL' | 'MEDIUM' | 'HEIGHT_UNSPECIFIED' | 'SHORT'
120
								content_info?: {
121
									file_url?: string
122
									thumbnail_url?: string
123
									force_refresh?: false | true
124
								}
125
							}
126
							suggestions?: {
127
								reply?: { text?: string; postback_data?: string }
128
								action?: {
129
									text?: string
130
									postback_data?: string
131
									fallback_url?: string
132
									dial_action?: { phone_number: string }
133
									view_location_action?: {
134
										lat_long?: { latitude: number; longitude: number }
135
										label?: string
136
										query?: string
137
									}
138
									create_calendar_event_action?: {
139
										start_time?: string
140
										end_time?: string
141
										title?: string
142
										description?: string
143
									}
144
									open_url_action?: {
145
										url: string
146
										application: 'OPEN_URL_APPLICATION_UNSPECIFIED' | 'BROWSER' | 'WEBVIEW'
147
										webview_view_mode: 'WEBVIEW_VIEW_MODE_UNSPECIFIED' | 'FULL' | 'HALF' | 'TALL'
148
										description?: string
149
									}
150
									share_location_action?: {}
151
									compose_action?: {
152
										compose_text_message?: {
153
											phone_number: string
154
											text?: string
155
										}
156
										compose_recording_message?: {
157
											phone_number?: string
158
											type:
159
												| 'COMPOSE_RECORDING_ACTION_TYPE_UNSPECIFIED'
160
												| 'ACTION_TYPE_AUDIO'
161
												| 'ACTION_TYPE_VIDEO'
162
										}
163
									}
164
								}
165
							}[]
166
						}
167
					}
168
				}
169
				content_info?: {
170
					file_url?: string
171
					thumbnail_url?: string
172
					force_refresh?: false | true
173
				}
174
			}
175
			event?: { event_type?: 'TYPE_UNSPECIFIED' | 'IS_TYPING' | 'READ' }
176
			expire_time?: string
177
			ttl?: string
178
		}
179
	}
180
) {
181
	const url = new URL(`https://api.telnyx.com/v2/messsages/rcs`)
182

183
	const response = await fetch(url, {
184
		method: 'POST',
185
		headers: {
186
			'Content-Type': 'application/json',
187
			Authorization: 'Bearer ' + auth.apiKey
188
		},
189
		body: JSON.stringify(body)
190
	})
191
	if (!response.ok) {
192
		const text = await response.text()
193
		throw new Error(`${response.status} ${text}`)
194
	}
195
	return await response.json()
196
}
197