voiceflousion 0.3.1

A crate that provides toolkit for Voiceflow AI bots integrations and pre-built functionality for quick integration with popular messengers
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use std::sync::Arc;
use crate::core::session_wrappers::Session;
use crate::core::voiceflow::{State, VoiceflowClient};


/// Builds a client with the necessary configurations.
///
/// `ClientBuilder` provides a builder pattern for constructing a client with various configurations,
/// including session management, cleanup settings, and API interactions. This struct allows for
/// flexible client setup tailored to specific needs.
pub struct ClientBuilder {
    /// The client ID used to uniquely identify the client.
    client_id: String,
    /// The API key for authentication with the Voiceflow API.
    api_key: String,
    /// The Voiceflow client used for API interactions.
    voiceflow_client: Arc<VoiceflowClient>,
    /// Optional sessions to initialize the client with. This can be used to preload existing sessions.
    sessions: Option<Vec<Session>>,
    /// The maximum number of connections allowed at any given moment.
    max_connections_per_moment: usize,
    /// Optional duration of the HTTP connection in seconds. This controls how long connections are kept alive.
    connection_duration: Option<u64>,
    /// The optional duration for session validity in seconds. This defines how long a session remains valid before expiring.
    session_duration: Option<i64>,
    /// The optional interval for session cleanup in seconds. This defines how frequently the system checks for expired sessions and cleans them up.
    sessions_cleanup_interval: Option<u64>,
    /// The launch state of the Voiceflow client, which may include configurations like default states or variables.
    launch_state: State,
    /// A status flag indicating whether the client is active or not.
    status: bool,
    /// The optional bot authentication token, used for additional security or identification purposes.
    bot_auth_token: Option<String>,
}

impl ClientBuilder {
    /// Creates a new `ClientBuilder`.
    ///
    /// # Parameters
    ///
    /// * `client_id` - The client ID.
    /// * `api_key` - The API key for authentication.
    /// * `voiceflow_client` - The Voiceflow client for API interactions.
    /// * `max_connections_per_moment` - The maximum number of connections per moment.
    ///
    /// # Returns
    ///
    /// A new instance of `ClientBuilder`.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// ```
    pub fn new(client_id: String, api_key: String, voiceflow_client: Arc<VoiceflowClient>, max_connections_per_moment: usize) -> Self {
        Self {
            client_id,
            api_key,
            voiceflow_client,
            sessions: None,
            max_connections_per_moment,
            connection_duration: None,
            session_duration: None,
            sessions_cleanup_interval: None,
            launch_state: State::default(),
            status: true,
            bot_auth_token: None
        }
    }

    /// Sets sessions for the client builder.
    ///
    /// # Parameters
    ///
    /// * `sessions` - A vector of sessions to set.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::session_wrappers::Session;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let sessions: Vec<Session> = vec![];
    ///
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.set_sessions(sessions);
    /// ```
    pub fn set_sessions(mut self, sessions: Vec<Session>) -> Self {
        self.sessions = Some(sessions);
        self
    }

    /// Allows session cleaning and sets the cleanup interval.
    ///
    /// # Parameters
    ///
    /// * `interval` - The interval for session cleanup in seconds.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.allow_sessions_cleaning(600);
    /// ```
    pub fn allow_sessions_cleaning(mut self, interval: u64) -> Self {
        self.sessions_cleanup_interval = Some(interval);
        self
    }

    /// Sets the session duration.
    ///
    /// # Parameters
    ///
    /// * `duration` - The duration for session validity in seconds.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.set_session_duration(3600);
    /// ```
    pub fn set_session_duration(mut self, duration: i64) -> Self {
        self.session_duration = Some(duration);
        self
    }

    /// Sets the connection duration for the client builder.
    ///
    /// # Parameters
    ///
    /// * `duration` - The duration for HTTP connection validity in seconds.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.set_connection_duration(120);
    /// ```
    pub fn set_connection_duration(mut self, duration: u64) -> Self {
        self.connection_duration = Some(duration);
        self
    }

    /// Sets the initial launch state for the client builder.
    ///
    /// This method allows setting the initial state of the Voiceflow interaction when the client is started.
    /// The state is used to configure the initial conditions or environment of the Voiceflow session.
    ///
    /// # Parameters
    ///
    /// * `state` - The initial state to set for the Voiceflow client interaction.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance with the new launch state set.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::{VoiceflowClient, State};
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let initial_state = State::default();
    ///
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.set_launch_state(initial_state);
    /// ```
    pub fn set_launch_state(mut self, state: State) -> Self {
        self.launch_state = state;
        self
    }

    /// Sets the status for the client builder.
    ///
    /// # Parameters
    ///
    /// * `status` - The status indicating whether the client is active.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.set_status(true);
    /// ```
    pub fn set_status(mut self, status: bool) -> Self {
        self.status = status;
        self
    }

    /// Sets the bot authentication token for the client builder.
    ///
    /// # Parameters
    ///
    /// * `bot_auth_token` - The bot authentication token.
    ///
    /// # Returns
    ///
    /// The updated `ClientBuilder` instance.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let builder = builder.set_bot_auth_token("new_auth_token".to_string());
    /// ```
    pub fn set_bot_auth_token(mut self, bot_auth_token: String) -> Self {
        self.bot_auth_token = Some(bot_auth_token);
        self
    }

    /// Returns the client ID.
    ///
    /// # Returns
    ///
    /// A reference to the client ID string.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let client_id = builder.client_id();
    /// ```
    pub fn client_id(&self) -> &String {
        &self.client_id
    }

    /// Returns the API key.
    ///
    /// # Returns
    ///
    /// A reference to the API key string.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let api_key = builder.api_key();
    /// ```
    pub fn api_key(&self) -> &String {
        &self.api_key
    }

    /// Returns the Voiceflow client.
    ///
    /// # Returns
    ///
    /// A reference to the `Arc<VoiceflowClient>`.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let voiceflow_client = builder.voiceflow_client();
    /// ```
    pub fn voiceflow_client(&self) -> &Arc<VoiceflowClient> {
        &self.voiceflow_client
    }

    /// Returns the optional sessions.
    ///
    /// # Returns
    ///
    /// An `Option` containing a vector of `Session`.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let sessions = builder.sessions();
    /// ```
    pub fn sessions(self) -> Option<Vec<Session>> {
        self.sessions
    }

    /// Returns the maximum number of connections per moment.
    ///
    /// # Returns
    ///
    /// A `usize` representing the maximum number of connections per moment.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let max_connections = builder.max_connections_per_moment();
    /// ```
    pub fn max_connections_per_moment(&self) -> usize {
        self.max_connections_per_moment
    }

    /// Returns the session duration.
    ///
    /// # Returns
    ///
    /// An `Option<i64>` representing the session duration in seconds.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let session_duration = builder.session_duration();
    /// ```
    pub fn session_duration(&self) -> Option<i64> {
        self.session_duration
    }

    /// Returns the connection duration.
    ///
    /// # Returns
    ///
    /// An `Option<u64>` representing the connection duration in seconds.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let connection_duration = builder.connection_duration();
    /// ```
    pub fn connection_duration(&self) -> Option<u64> {self.connection_duration}

    /// Returns the session cleanup interval.
    ///
    /// # Returns
    ///
    /// An `Option<u64>` representing the session cleanup interval in seconds.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let cleanup_interval = builder.sessions_cleanup_interval();
    /// ```
    pub fn sessions_cleanup_interval(&self) -> Option<u64> {
        self.sessions_cleanup_interval
    }

    /// Returns a reference to the current launch state of the Voiceflow client.
    ///
    /// This method allows accessing the initial state that has been set for the Voiceflow client interaction.
    /// It provides a way to retrieve the state that dictates the initial conditions or behaviors
    /// of the Voiceflow session when the client is launched.
    ///
    /// # Returns
    ///
    /// A reference to the `State` object representing the initial state of the Voiceflow client.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::{VoiceflowClient, State};
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let mut builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let initial_state = builder.launch_state();
    /// ```
    pub fn launch_state(&self) -> &State{
        &self.launch_state
    }

    /// Returns the status of the client.
    ///
    /// # Returns
    ///
    /// A boolean indicating whether the client is active.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let status = builder.status();
    /// ```
    pub fn status(&self) -> bool {
        self.status
    }

    /// Returns the bot authentication token.
    ///
    /// # Returns
    ///
    /// A reference to the optional bot authentication token.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use voiceflousion::core::ClientBuilder;
    /// use voiceflousion::core::voiceflow::VoiceflowClient;
    ///
    /// let voiceflow_client = Arc::new(VoiceflowClient::new("vf_api_key".to_string(), "bot_id".to_string(), "version_id".to_string(), 10, Some(120)));
    /// let builder = ClientBuilder::new("client_id".to_string(), "api_key".to_string(), voiceflow_client, 10);
    /// let bot_auth_token = builder.bot_auth_token();
    /// ```
    pub fn bot_auth_token(&self) -> &Option<String> {
        &self.bot_auth_token
    }
}