0
# Manager API Connection and Actions
1
2
Core functionality for connecting to the Asterisk Manager API and executing actions. This module provides the main interface for controlling and monitoring Asterisk PBX systems programmatically.
3
4
## Capabilities
5
6
### Manager Connection Classes
7
8
#### Manager
9
10
```python { .api }
11
class Manager(BaseManager, CoreActions, ZapataActions):
12
"""
13
Full-featured Manager API client with protocol implementation,
14
core actions, Zapata actions, and event handling.
15
"""
16
def __init__(address: tuple, username: str, secret: str,
17
listen_events: bool = True, timeout: float = None):
18
"""
19
Connect to Asterisk Manager API.
20
21
Args:
22
address: (host, port) tuple for Manager API connection
23
username: Manager API username
24
secret: Manager API password
25
listen_events: Whether to receive real-time events
26
timeout: Socket timeout in seconds
27
"""
28
```
29
30
#### CoreManager
31
32
```python { .api }
33
class CoreManager(BaseManager, CoreActions, ZapataActions):
34
"""
35
Manager API client with actions but without event handlers.
36
Use when you don't need event processing.
37
"""
38
```
39
40
#### BaseManager
41
42
```python { .api }
43
class BaseManager:
44
"""
45
Base protocol implementation for Manager API communication.
46
"""
47
def __init__(address: tuple, username: str, secret: str,
48
listen_events: bool = True, timeout: float = None): ...
49
50
def get_channel(channel_id: str) -> BaseChannel:
51
"""Get a channel object for the given channel ID."""
52
53
def read_response(action_id: str) -> dict:
54
"""Read response for a specific action ID."""
55
```
56
57
### Core Actions
58
59
The CoreActions class provides methods for all standard Asterisk Manager API actions:
60
61
#### Basic Operations
62
63
```python { .api }
64
def Login(username: str, secret: str) -> dict:
65
"""Authenticate with the Manager API."""
66
67
def Logoff() -> dict:
68
"""Disconnect from the Manager API."""
69
70
def Command(command: str) -> list:
71
"""Execute Asterisk console command and return output lines."""
72
73
def Ping() -> dict:
74
"""Send keepalive ping to server."""
75
76
def ListCommands() -> dict:
77
"""Get list of available Manager API commands."""
78
```
79
80
#### Channel Operations
81
82
```python { .api }
83
def CoreShowChannels() -> list:
84
"""Get information about all active channels."""
85
86
def Status(channel: str = None) -> dict:
87
"""Get status of specific channel or all channels."""
88
89
def Hangup(channel: str) -> dict:
90
"""Hang up the specified channel."""
91
92
def AbsoluteTimeout(channel: str, timeout: int) -> dict:
93
"""Set absolute timeout for channel."""
94
95
def Originate(channel: str, context: str, exten: str, priority: int,
96
timeout: int = None, callerid: str = None,
97
variable: dict = None, **kwargs) -> dict:
98
"""Originate a new call."""
99
```
100
101
#### Channel Variables
102
103
```python { .api }
104
def Getvar(channel: str, variable: str) -> dict:
105
"""Get value of channel variable."""
106
107
def Setvar(channel: str, variable: str, value: str) -> dict:
108
"""Set channel variable to value."""
109
```
110
111
#### Monitoring and Recording
112
113
```python { .api }
114
def Monitor(channel: str, filename: str = None, format: str = 'wav',
115
mix: bool = False) -> dict:
116
"""Start monitoring/recording channel."""
117
118
def StopMonitor(channel: str) -> dict:
119
"""Stop monitoring channel."""
120
121
def ChangeMonitor(channel: str, pathname: str) -> dict:
122
"""Change monitor filename for channel."""
123
```
124
125
#### Queue Operations
126
127
```python { .api }
128
def QueueStatus(queue: str = None) -> dict:
129
"""Get status of call queues."""
130
131
def QueueAdd(queue: str, interface: str, penalty: int = 0,
132
paused: bool = False, membername: str = None) -> dict:
133
"""Add member to call queue."""
134
135
def QueueRemove(queue: str, interface: str) -> dict:
136
"""Remove member from call queue."""
137
```
138
139
#### Conference Bridge Operations
140
141
```python { .api }
142
def ConfbridgeListRooms() -> list:
143
"""
144
List active conference rooms.
145
Returns list of dicts with room_name, users_count, marked_users, locked.
146
"""
147
148
def ConfbridgeList(room: str) -> list:
149
"""
150
List participants in conference room.
151
Returns list of dicts with channel, user_profile, bridge_profile,
152
menu, caller_id, muted.
153
"""
154
155
def ConfbridgeKick(room: str, channel: str) -> bool:
156
"""Kick channel from conference room."""
157
158
def ConfbridgeMute(room: str, channel: str) -> bool:
159
"""Mute channel in conference room."""
160
161
def ConfbridgeUnmute(room: str, channel: str) -> bool:
162
"""Unmute channel in conference room."""
163
164
def ConfbridgeStartRecord(room: str, rFile: str = None) -> bool:
165
"""Start recording conference room."""
166
167
def ConfbridgeStopRecord(room: str) -> bool:
168
"""Stop recording conference room."""
169
170
def ConfbridgeisRecording(room: str) -> bool:
171
"""Check if conference room is being recorded."""
172
```
173
174
#### MeetMe Conference Operations
175
176
```python { .api }
177
def MeetMe() -> list:
178
"""
179
List active MeetMe conferences.
180
Returns list of dicts with conference details.
181
"""
182
183
def MeetMeList(confnum: str) -> list:
184
"""
185
List participants in MeetMe conference.
186
187
Args:
188
confnum: Conference number
189
190
Returns:
191
List of dicts with participant details
192
"""
193
```
194
195
#### SIP Operations
196
197
```python { .api }
198
def SipShowPeer(peer: str) -> dict:
199
"""Show detailed information about SIP peer."""
200
201
def SipShowRegistry() -> dict:
202
"""Show SIP registration status."""
203
204
def SipPeers() -> dict:
205
"""List all SIP peers with their status."""
206
```
207
208
#### System Operations
209
210
```python { .api }
211
def Reload(module: str = None) -> dict:
212
"""Reload Asterisk configuration or specific module."""
213
214
def ExtensionState(exten: str, context: str) -> dict:
215
"""Get state of extension."""
216
217
def ParkedCalls() -> dict:
218
"""Get list of parked calls."""
219
220
def SetCDRUserField(channel: str, userfield: str) -> dict:
221
"""Set CDR user field for channel."""
222
223
def VoicemailUsersList(context: str = None) -> dict:
224
"""List voicemail users."""
225
226
def DBGet(family: str, key: str) -> dict:
227
"""Get value from Asterisk database."""
228
229
def DBPut(family: str, key: str, value: str) -> dict:
230
"""Set value in Asterisk database."""
231
232
def Events(categories: str) -> dict:
233
"""Filter events to specific categories."""
234
235
def ExtensionStates() -> dict:
236
"""Get extension states for all extensions."""
237
238
def MailboxCount(mailbox: str) -> tuple:
239
"""Get mailbox message count as (new, old) tuple."""
240
241
def MailboxStatus(mailbox: str) -> int:
242
"""Get mailbox status as integer."""
243
244
def Bridge(channel1: str, channel2: str, tone: bool = False) -> dict:
245
"""Bridge two channels together."""
246
247
def PlayDTMF(channel: str, digit: str) -> dict:
248
"""Play DTMF digit on channel."""
249
250
def Originate2(channel: str, parameters: dict) -> dict:
251
"""Originate call using parameter dictionary."""
252
253
def Queues() -> dict:
254
"""Alias for QueueStatus() - get all queue statuses."""
255
```
256
257
### Zapata/DAHDI Actions
258
259
```python { .api }
260
class ZapataActions:
261
def ZapShowChannels() -> dict:
262
"""Show Zapata/DAHDI channel information."""
263
264
def ZapHangup(channel: int) -> dict:
265
"""Hang up Zapata channel by number."""
266
267
def ZapTransfer(channel: int) -> dict:
268
"""Transfer Zapata channel."""
269
270
def ZapDialOffhook(channel: int, number: str) -> dict:
271
"""Dial number on off-hook Zapata channel."""
272
273
def ZapDNDon(channel: int) -> dict:
274
"""Enable Do Not Disturb on Zapata channel."""
275
276
def ZapDNDoff(channel: int) -> dict:
277
"""Disable Do Not Disturb on Zapata channel."""
278
```
279
280
## Usage Examples
281
282
### Basic Manager Connection
283
284
```python
285
from Asterisk.Manager import Manager
286
287
# Connect to local Asterisk instance
288
manager = Manager(('127.0.0.1', 5038), 'admin', 'secret123')
289
290
try:
291
# Execute commands
292
channels = manager.CoreShowChannels()
293
print(f"Active channels: {len(channels)}")
294
295
# Originate a call
296
result = manager.Originate(
297
channel='SIP/1001',
298
context='default',
299
exten='1002',
300
priority=1,
301
timeout=30,
302
callerid='Test Call <1001>'
303
)
304
305
finally:
306
manager.Logoff()
307
```
308
309
### Working with Queues
310
311
```python
312
# Check queue status
313
queues = manager.QueueStatus()
314
for queue_name, queue_info in queues.items():
315
print(f"Queue {queue_name}: {queue_info.get('calls')} calls waiting")
316
317
# Add member to queue
318
manager.QueueAdd('support', 'SIP/1001', penalty=1, membername='Agent 1')
319
```