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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, ACCEPT};
use serde_json::Value;
use crate::core::subtypes::HttpClient;
use crate::core::voiceflow::request_structures::{ActionBuilder, ActionType, VoiceflowRequestBody, VoiceflowRequestBodyBuilder};
use crate::core::voiceflow::response_structures::VoiceflowResponse;
use crate::core::voiceflow::{State, VoiceflowBlock, VoiceflowMessage, VoiceflowSession};
use crate::core::voiceflow::dialog_blocks::VoiceflowText;
use crate::errors::VoiceflousionError;
/// Voiceflow API runtime interaction URL.
static VOICEFLOW_API_URL: &str = "https://general-runtime.voiceflow.com/v2beta1/interact";
/// Represents a client for the Voiceflow API.
///
/// `VoiceflowClient` is used to interact with the Voiceflow API using the provided API key,
/// version ID, and project ID.
pub struct VoiceflowClient {
/// The API key for accessing Voiceflow.
voiceflow_api_key: String,
/// The version ID for the Voiceflow project.
version_id: String,
/// The project ID for the Voiceflow project.
project_id: String,
/// The HTTP client for sending requests.
client: HttpClient,
/// The message to return when an unexpected error occurs.
unavailable_message: String,
/// The message to return when the bot sends invalid response.
invalid_response_message: String
}
impl VoiceflowClient {
/// Creates a new Voiceflow client.
///
/// # Parameters
///
/// * `voiceflow_api_key` - The API key for accessing Voiceflow.
/// * `project_id` - The project ID for the Voiceflow project.
/// * `version_id` - The version ID for the Voiceflow project.
/// * `max_sessions_per_moment` - The maximum number of idle connections per host.
/// * `connection_duration` - The optional duration for which connections can remain idle (in seconds).
///
/// # Returns
///
/// A new instance of `VoiceflowClient`.
///
/// # Example
///
/// ```
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = VoiceflowClient::new("api_key".to_string(), "project_id".to_string(), "version_id".to_string(), 10, Some(120));
/// let default_duration_client = VoiceflowClient::new("api_key".to_string(), "project_id".to_string(), "version_id".to_string(), 10, None);
/// ```
pub fn new(voiceflow_api_key: String, project_id: String, version_id: String, max_sessions_per_moment: usize, connection_duration: Option<u64>) -> Self {
Self {
voiceflow_api_key,
version_id,
project_id,
client: HttpClient::new(max_sessions_per_moment, connection_duration),
unavailable_message: "Bot is temporary unavailable".to_string(),
invalid_response_message: "Can't read response from bot".to_string()
}
}
/// Changes the message to return when an unexpected error occurs.
///
/// # Parameters
///
/// * `message` - The new message to return.
///
/// # Returns
///
/// The updated `VoiceflowClient` instance.
///
/// # Example
///
/// ```
/// use voiceflousion::core::voiceflow::VoiceflowClient;
/// let vf_client = VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None)
/// .change_unavailable_message("New error message".to_string());
/// ```
pub fn change_unavailable_message(mut self, message: String) -> Self {
self.unavailable_message = message;
self
}
/// Changes the message to return when the bot is temporarily unavailable.
///
/// # Parameters
///
/// * `message` - The new message to return.
///
/// # Returns
///
/// The updated `VoiceflowClient` instance.
///
/// # Example
///
/// ```
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None)
/// .change_unexpected_error_message("New unavailable message".to_string());
/// ```
pub fn change_unexpected_error_message(mut self, message: String) -> Self {
self.invalid_response_message = message;
self
}
/// Returns the version ID.
///
/// # Returns
///
/// A reference to the version ID string.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let version_id = vf_client.version_id();
/// ```
pub fn version_id(&self) -> &String {
&self.version_id
}
/// Returns the project ID.
///
/// # Returns
///
/// A reference to the project ID string.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let project_id = vf_client.project_id();
/// ```
pub fn project_id(&self) -> &String {
&self.project_id
}
/// Returns the API key.
///
/// # Returns
///
/// A reference to the API key string.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let api_key = vf_client.voiceflow_api_key();
/// ```
pub fn voiceflow_api_key(&self) -> &String {
&self.voiceflow_api_key
}
/// Returns the maximum number of idle connections per host.
///
/// # Returns
///
/// A `usize` representing the maximum number of idle connections per host.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let max_sessions = vf_client.max_sessions_per_moment();
/// ```
pub fn max_sessions_per_moment(&self) -> usize {
self.client.max_connections_per_moment()
}
/// Returns the message to return when an unexpected error occurs.
///
/// # Returns
///
/// A reference to the error message string.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let error_message = vf_client.unavailable_message();
/// ```
pub fn unavailable_message(&self) -> &String {
&self.unavailable_message
}
/// Returns the message to return when the bot is temporarily unavailable.
///
/// # Returns
///
/// A reference to the unavailable message string.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::voiceflow::VoiceflowClient;
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let unavailable_message = vf_client.unexpected_error_message();
/// ```
pub fn unexpected_error_message(&self) -> &String {
&self.invalid_response_message
}
/// Launches a dialog with the Voiceflow Bot chosen session.
///
/// # Parameters
///
/// * `session` - The Voiceflow session.
/// * `state` - The state for variables in the bot for the session.
///
/// # Returns
///
/// A `VoiceflowMessage` containing the response from the Voiceflow API.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::session_wrappers::{LockedSession, Session};
/// use voiceflousion::core::voiceflow::{State, VoiceflowClient, VoiceflowSession};
/// use tokio;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let session = Arc::new(Session::new("chat_id".to_string(), None, true));
/// let locked_session = LockedSession::try_from_session(&session)?;
/// let session = locked_session.voiceflow_session();
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let state = State::default();
///
/// let response = vf_client.launch_dialog(&session, state).await;
///
/// Ok(())
/// }
/// ```
pub async fn launch_dialog(&self, session: &VoiceflowSession, state: State) -> VoiceflowMessage {
let action = ActionBuilder::new(ActionType::Launch).build();
let body = VoiceflowRequestBodyBuilder::new(action).session(Some(session)).state(Some(state)).build();
self.send_stream_request(body).await
}
/// Sends a text message to the Voiceflow Bot's chosen session.
///
/// # Parameters
///
/// * `session` - The Voiceflow session.
/// * `state` - The optional state for variables in the bot for the session.
/// * `text` - The text message to send.
///
/// # Returns
///
/// A `VoiceflowMessage` containing the response from the Voiceflow API.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use voiceflousion::core::session_wrappers::{LockedSession, Session};
/// use voiceflousion::core::voiceflow::{State, VoiceflowClient, VoiceflowSession};
/// use tokio;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let session = Arc::new(Session::new("chat_id".to_string(), None, true));
/// let locked_session = LockedSession::try_from_session(&session)?;
/// let session = locked_session.voiceflow_session();
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let state = State::default();
///
/// let response = vf_client.send_message(session, Some(state), &"Hello".to_string()).await;
///
/// Ok(())
/// }
/// ```
pub async fn send_message(&self, session: &VoiceflowSession, state: Option<State>, text: &String) -> VoiceflowMessage {
let action = ActionBuilder::new(ActionType::Text).text(text.clone()).build();
let body = VoiceflowRequestBodyBuilder::new(action).session(Some(session)).state(state).build();
self.send_stream_request(body).await
}
/// Sends a button selection to the Voiceflow Bot's chosen session.
///
/// # Parameters
///
/// * `session` - The Voiceflow session.
/// * `state` - The optional state for variables in the bot for the session.
/// * `text` - The text associated with the button.
/// * `button_path` - The path of the button.
/// * `payload` - The payload associated with the button.
///
/// # Returns
///
/// A `VoiceflowMessage` containing the response from the Voiceflow API.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use serde_json::{json, Value};
/// use voiceflousion::core::session_wrappers::{LockedSession, Session};
/// use voiceflousion::core::voiceflow::{State, VoiceflowClient, VoiceflowSession};
/// use tokio;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let session = Arc::new(Session::new("chat_id".to_string(), None, true));
/// let locked_session = LockedSession::try_from_session(&session)?;
/// let session = locked_session.voiceflow_session();
///
/// let vf_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, None));
/// let state = State::default();
///
/// let response = vf_client.choose_button(&session, Some(state), json!({"path": "path"})).await;
///
/// Ok(())
/// }
/// ```
pub async fn choose_button(&self, session: &VoiceflowSession, state: Option<State>, mut payload: Value) -> VoiceflowMessage {
let path = payload.as_object_mut().unwrap().remove("path").expect("Button has no path!").as_str().unwrap().to_string();
let action = ActionBuilder::new(ActionType::Path(path.to_string())).path(payload).build();
let body = VoiceflowRequestBodyBuilder::new(action).session(Some(session)).state(state).build();
self.send_stream_request(body).await
}
/// Sends a request to the Voiceflow API and returns the response.
///
/// # Parameters
///
/// * `body` - The request body to send.
///
/// # Returns
///
/// A `VoiceflowMessage` containing the response from the Voiceflow API.
async fn send_stream_request<'a>(&self, body: VoiceflowRequestBody<'a>) -> VoiceflowMessage{
let general_runtime_url = format!("{}/{}/{}/stream", VOICEFLOW_API_URL, &self.project_id, &self.version_id);
let response = self.client.post(general_runtime_url)
.header(AUTHORIZATION, &self.voiceflow_api_key)
.header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "text/event-stream")
.body(body.to_json()).send().await;
let result_message = match response{
Ok(valid_response) => {
let voiceflow_response = VoiceflowResponse::new(valid_response);
voiceflow_response.to_message().await
},
Err(e) => {
let error = VoiceflousionError::VoiceflowRequestError(self.project_id.clone(), self.version_id.clone(), e.to_string());
println!("{:?}", error);
let mut message = VoiceflowMessage::default();
message.add_block(VoiceflowBlock::Text(VoiceflowText::new(self.unavailable_message.clone())));
Ok(message)
}
};
match result_message{
Ok(message) => message,
Err(error) =>{
println!("{:?}", error);
let mut message = VoiceflowMessage::default();
message.add_block(VoiceflowBlock::Text(VoiceflowText::new(self.invalid_response_message.clone())));
message
}
}
}
}