1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2025 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use livekit_protocol as proto;
use std::collections::HashMap;
use super::{ServiceBase, ServiceResult, LIVEKIT_PACKAGE};
use crate::{access_token::VideoGrants, get_env_keys, services::twirp_client::TwirpClient};
const SVC: &str = "Connector";
/// Options for dialing a WhatsApp call
#[derive(Default, Clone, Debug)]
pub struct DialWhatsAppCallOptions {
/// Optional - An arbitrary string useful for tracking and logging purposes
pub biz_opaque_callback_data: Option<String>,
/// Optional - What LiveKit room should this participant be connected to
pub room_name: Option<String>,
/// Optional - Agents to dispatch the call to
pub agents: Option<Vec<proto::RoomAgentDispatch>>,
/// Optional - Identity of the participant in LiveKit room
pub participant_identity: Option<String>,
/// Optional - Name of the participant in LiveKit room
pub participant_name: Option<String>,
/// Optional - User-defined metadata attached to the participant in the room
pub participant_metadata: Option<String>,
/// Optional - User-defined attributes attached to the participant in the room
pub participant_attributes: Option<HashMap<String, String>>,
/// Optional - Country where the call terminates as ISO 3166-1 alpha-2
pub destination_country: Option<String>,
}
/// Options for accepting a WhatsApp call
#[derive(Default, Clone, Debug)]
pub struct AcceptWhatsAppCallOptions {
/// Optional - An arbitrary string useful for tracking and logging purposes
pub biz_opaque_callback_data: Option<String>,
/// Optional - What LiveKit room should this participant be connected to
pub room_name: Option<String>,
/// Optional - Agents to dispatch the call to
pub agents: Option<Vec<proto::RoomAgentDispatch>>,
/// Optional - Identity of the participant in LiveKit room
pub participant_identity: Option<String>,
/// Optional - Name of the participant in LiveKit room
pub participant_name: Option<String>,
/// Optional - User-defined metadata attached to the participant in the room
pub participant_metadata: Option<String>,
/// Optional - User-defined attributes attached to the participant in the room
pub participant_attributes: Option<HashMap<String, String>>,
/// Optional - Country where the call terminates as ISO 3166-1 alpha-2
pub destination_country: Option<String>,
}
/// Options for connecting a Twilio call
#[derive(Default, Clone, Debug)]
pub struct ConnectTwilioCallOptions {
/// Optional - Agents to dispatch the call to
pub agents: Option<Vec<proto::RoomAgentDispatch>>,
/// Optional - Identity of the participant in LiveKit room
pub participant_identity: Option<String>,
/// Optional - Name of the participant in LiveKit room
pub participant_name: Option<String>,
/// Optional - User-defined metadata attached to the participant in the room
pub participant_metadata: Option<String>,
/// Optional - User-defined attributes attached to the participant in the room
pub participant_attributes: Option<HashMap<String, String>>,
/// Optional - Country where the call terminates as ISO 3166-1 alpha-2
pub destination_country: Option<String>,
}
#[derive(Debug)]
pub struct ConnectorClient {
base: ServiceBase,
client: TwirpClient,
}
impl ConnectorClient {
pub fn with_api_key(host: &str, api_key: &str, api_secret: &str) -> Self {
Self {
base: ServiceBase::with_api_key(api_key, api_secret),
client: TwirpClient::new(host, LIVEKIT_PACKAGE, None),
}
}
pub fn new(host: &str) -> ServiceResult<Self> {
let (api_key, api_secret) = get_env_keys()?;
Ok(Self::with_api_key(host, &api_key, &api_secret))
}
/// Dials a WhatsApp call
///
/// # Arguments
/// * `phone_number_id` - The identifier of the number for business initiating the call
/// * `to_phone_number` - The number of the user that should receive the call
/// * `api_key` - The API key of the business initiating the call
/// * `cloud_api_version` - WhatsApp Cloud API version (e.g., "23.0", "24.0")
/// * `options` - Additional options for the call
///
/// # Returns
/// Information about the dialed call including the WhatsApp call ID and room name
pub async fn dial_whatsapp_call(
&self,
phone_number_id: impl Into<String>,
to_phone_number: impl Into<String>,
api_key: impl Into<String>,
cloud_api_version: impl Into<String>,
options: DialWhatsAppCallOptions,
) -> ServiceResult<proto::DialWhatsAppCallResponse> {
self.client
.request(
SVC,
"DialWhatsAppCall",
proto::DialWhatsAppCallRequest {
whatsapp_phone_number_id: phone_number_id.into(),
whatsapp_to_phone_number: to_phone_number.into(),
whatsapp_api_key: api_key.into(),
whatsapp_cloud_api_version: cloud_api_version.into(),
whatsapp_biz_opaque_callback_data: options
.biz_opaque_callback_data
.unwrap_or_default(),
room_name: options.room_name.unwrap_or_default(),
agents: options.agents.unwrap_or_default(),
participant_identity: options.participant_identity.unwrap_or_default(),
participant_name: options.participant_name.unwrap_or_default(),
participant_metadata: options.participant_metadata.unwrap_or_default(),
participant_attributes: options.participant_attributes.unwrap_or_default(),
destination_country: options.destination_country.unwrap_or_default(),
ringing_timeout: Default::default(),
},
self.base
.auth_header(VideoGrants { room_create: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
}
/// Disconnects a WhatsApp call
///
/// # Arguments
/// * `call_id` - Call ID sent by Meta
/// * `api_key` - The API key of the business disconnecting the call
///
/// # Returns
/// Empty response on success
pub async fn disconnect_whatsapp_call(
&self,
call_id: impl Into<String>,
api_key: impl Into<String>,
) -> ServiceResult<proto::DisconnectWhatsAppCallResponse> {
self.client
.request(
SVC,
"DisconnectWhatsAppCall",
proto::DisconnectWhatsAppCallRequest {
whatsapp_call_id: call_id.into(),
whatsapp_api_key: api_key.into(),
..Default::default()
},
self.base
.auth_header(VideoGrants { room_create: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
}
/// Connects a WhatsApp call (handles the SDP exchange)
///
/// # Arguments
/// * `call_id` - Call ID sent by Meta
/// * `sdp` - The SDP from Meta (answer SDP for business-initiated call)
///
/// # Returns
/// Empty response on success
pub async fn connect_whatsapp_call(
&self,
call_id: impl Into<String>,
sdp: proto::SessionDescription,
) -> ServiceResult<proto::ConnectWhatsAppCallResponse> {
self.client
.request(
SVC,
"ConnectWhatsAppCall",
proto::ConnectWhatsAppCallRequest {
whatsapp_call_id: call_id.into(),
sdp: Some(sdp),
},
self.base
.auth_header(VideoGrants { room_create: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
}
/// Accepts an incoming WhatsApp call
///
/// # Arguments
/// * `phone_number_id` - The identifier of the number for business initiating the call
/// * `api_key` - The API key of the business connecting the call
/// * `cloud_api_version` - WhatsApp Cloud API version (e.g., "23.0", "24.0")
/// * `call_id` - Call ID sent by Meta
/// * `sdp` - The SDP from Meta (for user-initiated call)
/// * `options` - Additional options for the call
///
/// # Returns
/// Information about the accepted call including the room name
pub async fn accept_whatsapp_call(
&self,
phone_number_id: impl Into<String>,
api_key: impl Into<String>,
cloud_api_version: impl Into<String>,
call_id: impl Into<String>,
sdp: proto::SessionDescription,
options: AcceptWhatsAppCallOptions,
) -> ServiceResult<proto::AcceptWhatsAppCallResponse> {
self.client
.request(
SVC,
"AcceptWhatsAppCall",
proto::AcceptWhatsAppCallRequest {
whatsapp_phone_number_id: phone_number_id.into(),
whatsapp_api_key: api_key.into(),
whatsapp_cloud_api_version: cloud_api_version.into(),
whatsapp_call_id: call_id.into(),
whatsapp_biz_opaque_callback_data: options
.biz_opaque_callback_data
.unwrap_or_default(),
sdp: Some(sdp),
room_name: options.room_name.unwrap_or_default(),
agents: options.agents.unwrap_or_default(),
participant_identity: options.participant_identity.unwrap_or_default(),
participant_name: options.participant_name.unwrap_or_default(),
participant_metadata: options.participant_metadata.unwrap_or_default(),
participant_attributes: options.participant_attributes.unwrap_or_default(),
destination_country: options.destination_country.unwrap_or_default(),
ringing_timeout: Default::default(),
wait_until_answered: Default::default(),
},
self.base
.auth_header(VideoGrants { room_create: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
}
/// Connects a Twilio call
///
/// # Arguments
/// * `direction` - The direction of the call (inbound or outbound)
/// * `room_name` - What LiveKit room should this call be connected to
/// * `options` - Additional options for the call
///
/// # Returns
/// The WebSocket URL which Twilio media stream should connect to
pub async fn connect_twilio_call(
&self,
direction: proto::connect_twilio_call_request::TwilioCallDirection,
room_name: impl Into<String>,
options: ConnectTwilioCallOptions,
) -> ServiceResult<proto::ConnectTwilioCallResponse> {
self.client
.request(
SVC,
"ConnectTwilioCall",
proto::ConnectTwilioCallRequest {
twilio_call_direction: direction as i32,
room_name: room_name.into(),
agents: options.agents.unwrap_or_default(),
participant_identity: options.participant_identity.unwrap_or_default(),
participant_name: options.participant_name.unwrap_or_default(),
participant_metadata: options.participant_metadata.unwrap_or_default(),
participant_attributes: options.participant_attributes.unwrap_or_default(),
destination_country: options.destination_country.unwrap_or_default(),
},
self.base
.auth_header(VideoGrants { room_create: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
}
}