#manager #voip #asterisk #api-bindings #ami

asterisk-manager

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

2 releases

0.1.1 Jan 22, 2025
0.1.0 Jan 22, 2025

#789 in Network programming

Download history 232/week @ 2025-01-22

232 downloads per month

Custom license

36KB
519 lines

Asterisk Manager Library

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

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 and integration.
  • 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;
    });
}

Optional Parameters

The event_sender and event_callback parameters in the Manager::new method are optional. If you do not need to handle events or use callbacks, you can pass None for these parameters:

let mut manager = Manager::new(Some(options), None, None);

Installation

Add this to your Cargo.toml:

[dependencies]
asterisk_manager = "0.1.1"

Based On

This library is based on the NodeJS-AsteriskManager repository and the node-asterisk repository.

Dependencies

~3–9.5MB
~92K SLoC