Crate asterisk_ari

Source
Expand description

Asterisk ARI Client

This crate provides a simple yet powerful Rust library for managing the Asterisk ARI (Asterisk REST Interface). It offers full implementation of Asterisk’s REST APIs and WebSocket event handling, enabling developers to build custom telephony applications with ease.

§Features

  • Comprehensive coverage of Asterisk REST APIs.
  • WebSocket support for real-time ARI event streaming.
  • Designed for simplicity and ease of use.
  • Ideal for telephony application development and integration.

§Quick Start

To use this crate, add it to your Cargo.toml:

[dependencies]
asterisk-ari-rs = "x.y.z" // Replace with the latest version

§Example

use asterisk_ari::apis::channels;
use asterisk_ari::AriClient;
use asterisk_ari::Config;
use asterisk_ari::Result;

#[tokio::main]
async fn main() -> Result<()> {

    let config = Config::new("http://localhost:8088", "asterisk", "asterisk");

    let mut client = AriClient::with_config(config);

    client.on_stasis_start(|client, event| async move {
        println!("Handling StasisStart event: {:?}", event);

        client
            .channels()
            .answer(&event.data.channel.id)
            .await
            .unwrap();

        client
            .channels()
            .play(channels::params::PlayRequest::new(
                &event.data.channel.id,
                "sound:tt-monkeys",
            ))
            .await
            .unwrap();
    });

    println!("Applications: {:?}", client.applications().list().await?);
    println!("Ping: {:?}", client.asterisk().ping().await?);
    println!("Info: {:?}", client.asterisk().info().await?);

    let _client = client.clone();
    tokio::spawn(async move {
        _client.start("my-application".to_string()).await.unwrap();
    });

    tokio::time::sleep(std::time::Duration::from_secs(30)).await;

    println!("Stopping client");
    client.stop();

    println!("Await client to stop");
    tokio::time::sleep(std::time::Duration::from_secs(4)).await;

    Ok(())
}

§License

This crate is dual-licensed under the Apache License 2.0 or MIT License. Choose the license that suits your project.

Modules§

apis
Apis implementation
ws
WebSocket implementation

Structs§

ApiError
Represents an API-specific error.
AriClient
AriClient is a client for interacting with the Asterisk REST Interface (ARI). It manages the connection to the ARI and handles events.
Config
Configuration for the ARI client.

Enums§

AriError
Represents various errors that can occur in the ARI client.

Type Aliases§

Result
Result type alias for operations that can return an AriError.