freeswitch_esl/
dp_tools.rs1use std::collections::HashMap;
2
3use serde_json::Value;
4
5const PLAY_AND_GET_DIGITS_APP: &str = "play_and_get_digits";
6const PLAYBACK_APP: &str = "playback";
7
8use crate::{EslConnection, EslError, Event};
9
10impl EslConnection {
11 pub async fn playback(&self, file_path: &str) -> Result<Event, EslError> {
13 self.execute(PLAYBACK_APP, file_path).await
14 }
15
16 pub async fn record_session(&self, file_path: &str) -> Result<Event, EslError> {
18 self.execute("record_session", file_path).await
19 }
20
21 pub async fn send_dtmf(&self, dtmf_str: &str) -> Result<Event, EslError> {
23 self.execute("send_dtmf", dtmf_str).await
24 }
25
26 pub async fn wait_for_silence(&self, silence_str: &str) -> Result<Event, EslError> {
28 self.execute("wait_for_silence", silence_str).await
29 }
30
31 pub async fn sleep(&self, millis: u32) -> Result<Event, EslError> {
33 self.execute("sleep", &millis.to_string()).await
34 }
35
36 pub async fn set_variable(&self, var: &str, value: &str) -> Result<Event, EslError> {
38 let args = format!("{}={}", var, value);
39 self.execute("set", &args).await
40 }
41
42 pub async fn fs_log(&self, loglevel: &str, msg: &str) -> Result<Event, EslError> {
44 let args = format!("{} {}", loglevel, msg);
45 self.execute("log", &args).await
46 }
47
48 #[allow(clippy::too_many_arguments)]
49 pub async fn play_and_get_digits(
51 &self,
52 min: u8,
53 max: u8,
54 tries: u8,
55 timeout: u64,
56 terminators: &str,
57 file: &str,
58 invalid_file: &str,
59 ) -> Result<String, EslError> {
60 let variable_name = uuid::Uuid::new_v4().to_string();
61 let app_args = format!(
62 "{min} {max} {tries} {timeout} {terminators} {file} {invalid_file} {variable_name}",
63 );
64 let data = self.execute(PLAY_AND_GET_DIGITS_APP, &app_args).await?;
65 let body = data.body.as_ref().unwrap();
66 let body = parse_json_body(body).unwrap();
67 let result = body.get(&format!("variable_{}", variable_name));
68 let Some(digit) = result else {
69 return Err(EslError::NoInput);
70 };
71 let digit = digit.as_str().unwrap().to_string();
72 Ok(digit)
73 }
74}
75
76fn parse_json_body(body: &str) -> Result<HashMap<String, Value>, EslError> {
77 Ok(serde_json::from_str(body)?)
78}