Expand description
§Asterisk Manager Library
A modern, strongly-typed, stream-based library for integration with the Asterisk Manager Interface (AMI).
- Typed AMI messages: Actions, Events, and Responses as Rust enums/structs.
- Stream-based API: Consume events via
tokio_stream. - Asynchronous operations: Fully based on Tokio.
- Resilient connections: Optional resilient module with heartbeat and automatic reconnection.
§Usage Example
use asterisk_manager::{Manager, ManagerOptions, AmiAction};
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() {
let options = ManagerOptions {
port: 5038,
host: "127.0.0.1".to_string(),
username: "admin".to_string(),
password: "password".to_string(),
events: true,
};
let mut manager = Manager::new();
manager.connect_and_login(options).await.unwrap();
let mut events = manager.all_events_stream().await;
tokio::spawn(async move {
while let Some(Ok(ev)) = events.next().await {
println!("Event: {:?}", ev);
}
});
let resp = manager.send_action(AmiAction::Ping { action_id: None }).await.unwrap();
println!("Ping response: {:?}", resp);
manager.disconnect().await.unwrap();
}§Resilient Connections
For production applications that need automatic reconnection and heartbeat monitoring,
use the resilient module:
use asterisk_manager::resilient::{ResilientOptions, connect_resilient};
use asterisk_manager::ManagerOptions;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = ResilientOptions {
manager_options: ManagerOptions {
port: 5038,
host: "127.0.0.1".to_string(),
username: "admin".to_string(),
password: "password".to_string(),
events: true,
},
buffer_size: 2048,
enable_heartbeat: true,
enable_watchdog: true,
heartbeat_interval: 30,
watchdog_interval: 1,
max_retries: 3,
metrics: None,
cumulative_attempts_counter: None,
};
let manager = connect_resilient(options).await?;
// Manager now has heartbeat and automatic reconnection enabled
Ok(())
}§Features
- Login/logout, sending actions, and receiving AMI events.
- Support for common events (
Newchannel,Hangup,PeerStatus) and fallback for unknown events. - Detailed error handling via the
AmiErrorenum. - Configurable buffer sizes for high-throughput applications.
- Heartbeat monitoring with configurable interval and automatic disconnection on failure.
- Watchdog for automatic reconnection with configurable check interval when not authenticated.
- Infinite event streams that handle lag and reconnection automatically.
§Requirements
- Rust 1.70+
- Tokio (asynchronous runtime)
§License
MIT
Modules§
- resilient
- Resilient AMI connection module