-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
73 lines (61 loc) · 2.19 KB
/
Program.cs
File metadata and controls
73 lines (61 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Verbara.Sdk - Basic AMI Example
// Demonstrates: DI setup, connect, PingAction, event subscription, disconnect.
using Verbara.Sdk;
using Verbara.Sdk.Ami.Actions;
using Verbara.Sdk.Ami.Connection;
using Verbara.Sdk.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Console.WriteLine("Verbara.Sdk - Basic AMI Example");
Console.WriteLine("===================================");
// 1. Configure services with DI
var services = new ServiceCollection();
services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Information));
services.AddVerbara(options =>
{
options.Ami.Hostname = "localhost";
options.Ami.Port = 5038;
options.Ami.Username = "admin";
options.Ami.Password = "secret";
});
await using var provider = services.BuildServiceProvider();
// 2. Resolve the AMI connection
var ami = provider.GetRequiredService<IAmiConnection>();
try
{
// 3. Connect to Asterisk
Console.WriteLine("Connecting to Asterisk AMI...");
await ami.ConnectAsync();
Console.WriteLine($"Connected! Asterisk version: {ami.AsteriskVersion}");
// 4. Subscribe to events
using var subscription = ami.Subscribe(new EventPrinter());
// 5. Send a PingAction
var response = await ami.SendActionAsync(new PingAction());
Console.WriteLine($"Ping response: {response.Response} - {response.Message}");
// 6. Wait for some events
Console.WriteLine("Listening for events (press Ctrl+C to stop)...");
await Task.Delay(Timeout.InfiniteTimeSpan, new CancellationTokenSource().Token);
}
catch (OperationCanceledException)
{
// Normal shutdown
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
}
finally
{
await ami.DisconnectAsync();
Console.WriteLine("Disconnected.");
}
// Simple event observer that prints events to console
sealed class EventPrinter : IObserver<ManagerEvent>
{
public void OnNext(ManagerEvent value) =>
Console.WriteLine($"[Event] {value.EventType}: UniqueId={value.UniqueId}");
public void OnError(Exception error) =>
Console.Error.WriteLine($"[Error] {error.Message}");
public void OnCompleted() =>
Console.WriteLine("[Completed] Event stream ended.");
}