Crate asterisk_manager

Source
Expand description

§Asterisk Manager Library

This library provides an implementation to manage connections and authentication with an Asterisk Call Manager (AMI) server.

It offers a robust and efficient way to interact with the AMI server, handling connection retries, authentication, and event processing seamlessly. The library is built using Tokio for asynchronous operations, ensuring high performance and scalability. Additionally, it processes and formats data into JSON objects for easy manipulation and integration.

§Features

  • Reconnection Logic: Automatically handles reconnections to the AMI server in case of connection drops.
  • Event Handling: Processes various AMI events and provides a mechanism to handle them.
  • Asynchronous Operations: Utilizes Tokio for non-blocking, asynchronous operations.
  • Participant Management: Manages call participants and their states efficiently.
  • JSON Data Handling: Formats and processes data into JSON objects for easy manipulation.
  • Event Callbacks: Allows registration of callbacks for specific events, all events, raw events, and response events.

§Usage Example

use asterisk_manager::{ManagerBuilder, ManagerOptions};
use tokio::runtime::Runtime;
use serde_json::Value;

fn main() {
    let rt = Runtime::new().unwrap();
    rt.block_on(async {
        let options = ManagerOptions {
            port: 5038,
            host: "example.com".to_string(),
            username: "admin".to_string(),
            password: "password".to_string(),
            events: false,
        };

        let callback1 = Box::new(|event: Value| {
            println!("Callback1 received event: {}", event);
        });

        let callback2 = Box::new(|event: Value| {
            println!("Callback2 received event: {}", event);
        });

        let global_callback = Box::new(|event: Value| {
            println!("Global callback received event: {}", event);
        });

        let raw_event_callback = Box::new(|event: Value| {
            println!("Raw event callback received event: {}", event);
        });

        let response_event_callback = Box::new(|event: Value| {
            println!("Response event callback received event: {}", event);
        });

        let mut manager = ManagerBuilder::new(options)
            .on_event("Newchannel", callback1)
            .on_event("Hangup", callback2)
            .on_all_events(global_callback)
            .on_all_raw_events(raw_event_callback)
            .on_all_response_events(response_event_callback)
            .build();

        manager.connect_with_retries().await;

        if !manager.is_authenticated() {
            println!("Authentication failed");
            return;
        }

        let action = serde_json::json!({
            "action": "QueueStatus",
        });
        if let Err(err) = manager.send_action(action).await {
            println!("Error sending action: {}", err);
            return;
        }

        manager.read_data_with_retries().await;

        manager.disconnect().await;
    });
}

§Participant Structure

The Participant structure represents a participant in a call.

#[derive(Debug)]
pub struct Participant {
    name: String,
    number: String,
    with: Option<String>,
}

§Manager Structure

The Manager structure handles connections and options for the AMI server.

use asterisk_manager::ManagerOptions;
use tokio::net::TcpStream;
use std::collections::HashMap;
use tokio::sync::mpsc;
use asterisk_manager::Participant;
use std::sync::{Arc, Mutex};

pub struct Manager {
    options: ManagerOptions,
    connection: Option<TcpStream>,
    authenticated: bool,
    emitter: Arc<Mutex<Vec<String>>>,
    event_sender: Option<mpsc::Sender<String>>,
    participants: HashMap<String, Participant>,
    event_callbacks: HashMap<String, Box<dyn Fn(serde_json::Value) + Send + Sync>>,
    global_callback: Option<Box<dyn Fn(serde_json::Value) + Send + Sync>>,
    raw_event_callback: Option<Box<dyn Fn(serde_json::Value) + Send + Sync>>,
    response_event_callback: Option<Box<dyn Fn(serde_json::Value) + Send + Sync>>,
}

§Main Methods

§Manager::new

Creates a new instance of the Manager. The event_sender and event_callback parameters are optional.

§Manager::connect

Connects to the AMI server.

§Manager::connect_with_retries

Connects to the AMI server with reconnection logic.

§Manager::authenticate

Authenticates with the AMI server.

§Manager::send_action

Sends an action to the AMI server.

§Manager::read_data

Reads data from the AMI server.

§Manager::read_data_with_retries

Reads data from the AMI server with reconnection logic.

§Manager::on_event

Processes an event received from the AMI server.

§Manager::parse_event

Parses an event received from the AMI server.

§Manager::disconnect

Disconnects from the AMI server.

§Manager::is_authenticated

Returns whether the manager is authenticated.

§ManagerBuilder Structure

The ManagerBuilder structure helps in building a Manager instance with various event callbacks.

§ManagerBuilder::new

Creates a new instance of the ManagerBuilder.

§ManagerBuilder::on_event

Registers a callback for a specific event.

§ManagerBuilder::on_all_events

Registers a callback for all events.

§ManagerBuilder::on_all_raw_events

Registers a callback for all raw events.

§ManagerBuilder::on_all_response_events

Registers a callback for all response events.

§ManagerBuilder::build

Builds and returns a Manager instance.

Structs§

Manager
Structure that represents the Manager. Represents a manager that handles connections and options.
ManagerBuilder
ManagerOptions
Structure that contains the Manager configuration options. Represents the configuration options for the manager.
Participant