Skip to content

Commit

Permalink
channel api record added ->v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
adambezecny committed Dec 12, 2023
1 parent 5406209 commit 763945e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "asterisk-ari-client-rs"
version = "0.1.1"
version = "0.1.2"
authors = ["abezecny"]
edition = "2018"
description = "Simple Asterisk ARI library"
license = "MIT OR Apache-2.0"
keywords = ["telephony", "asterisk"]
categories = ["multimedia", "multimedia::audio"]
repository = "https://github.com/jabber-tools/asterisk-ari-client-rs"
documentation = "https://docs.rs/jabber-tools/0.1.1"
documentation = "https://docs.rs/jabber-tools/0.1.2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Apart from that following channels' operations are supported:
* set_variable
* hangup
* continue_in_dialplan
* record

This is by no means ready library. It is used for now on single purpose project and needs to be extended to support other ARI APIs. Pull requests welcome!

Expand Down
10 changes: 6 additions & 4 deletions examples/simple_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ fn stasis_start(event: StasisStart) {
info!("stasis_start: {:#?}", event);
debug!("Answering channel {} now!", &event.channel.id);
ARICLIENT.answer(&event.channel.id).await.unwrap();
// do the recording
ARICLIENT
.record(&event.channel.id, None, None, None, None, None, None, None)
.await
.unwrap();
debug!("Channel {} answered!", &event.channel.id);
});
}
Expand Down Expand Up @@ -120,10 +125,7 @@ async fn main() -> Result<()> {
client.set_channel_var_set_sender(Some(tx_channel_var_set));

tokio::spawn(async move {
if let Err(some_error) = client
.ari_processing_loop(vec!["my-ast-app".into()])
.await
{
if let Err(some_error) = client.ari_processing_loop(vec!["my-ast-app".into()]).await {
error!("Error in ari_processing_loop {:?}", some_error);
}
});
Expand Down
15 changes: 14 additions & 1 deletion src/apis/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ pub trait ChannelsAPI {
/// Set the value of a channel variable
async fn set_variable(&self, channel_id: &str, var_name: &str, var_value: &str) -> Result<()>;

/// Hangs up the channel
/// Hang up the channel
async fn hangup(&self, channel_id: &str) -> Result<()>;

/// Exit application; continue execution in the dialplan
async fn continue_in_dialplan(&self, channel_id: &str) -> Result<()>;

/// Record audio from a channel. Default filepath: /var/spool/asterisk/recording/channel_id.wav
async fn record(
&self,
channel_id: &str,
filepath: Option<&str>,
audio_format: Option<&str>,
terminate_on: Option<&str>,
max_duration: Option<usize>,
max_silence: Option<usize>,
if_exists: Option<&str>,
beep: Option<bool>,
) -> Result<()>;
}
44 changes: 44 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,48 @@ impl ChannelsAPI for AriClient {
eval_status_code!(status, StatusCode::NO_CONTENT, Some(body_str));
Ok(())
}

async fn record(
&self,
channel_id: &str,
filepath: Option<&str>,
audio_format: Option<&str>,
terminate_on: Option<&str>,
max_duration: Option<usize>,
max_silence: Option<usize>,
if_exists: Option<&str>,
beep: Option<bool>,
) -> Result<()> {
let req_body = format!(
r#"
{{
"name": "{_filepath_}",
"format": "{_audio_format_}",
"terminateOn": "{_terminate_on_}",
"maxDuration": {_max_duration_},
"maxSilence": {_max_silence_},
"ifExists": "{_if_exists_}",
"beep": {_beep_}
}}
"#,
_filepath_ = filepath.unwrap_or(channel_id),
_audio_format_ = audio_format.unwrap_or("wav"),
_terminate_on_ = terminate_on.unwrap_or("none"),
_max_duration_ = max_duration.unwrap_or(0),
_max_silence_ = max_silence.unwrap_or(0),
_if_exists_ = if_exists.unwrap_or("fail"),
_beep_ = beep.unwrap_or(false),
);
let resp = HTTP_CLIENT
.post(format!("{}/channels/{}/record", self.url, channel_id))
.headers(self.get_common_headers()?)
.body(req_body)
.send()
.await?;

let status = resp.status();
let body_str = resp.text().await?;
eval_status_code!(status, StatusCode::CREATED, Some(body_str));
Ok(())
}
}

0 comments on commit 763945e

Please sign in to comment.