asterisk.manager module
This module provides a Python API for interfacing with the asterisk manager.
Example
import sys
from asterisk.manager import Manager
def handle_shutdown(event, manager):
print("Received shutdown event")
manager.close()
# we could analyze the event and reconnect here
def handle_event(event, manager):
print("Received event: %s" % event.name)
manager = Manager()
try:
# connect to the manager
try:
manager.connect("host")
manager.login("user", "secret")
# register some callbacks
manager.register_event(
"Shutdown", handle_shutdown
) # shutdown
manager.register_event("*", handle_event) # catch all
# get a status report
response = manager.status()
manager.logoff()
except asterisk.manager.ManagerSocketException as e:
print(
"Error connecting to the manager: %s" % e.strerror
)
sys.exit(1)
except asterisk.manager.ManagerAuthException as e:
print(
"Error logging in to the manager: %s" % e.strerror
)
sys.exit(1)
except asterisk.manager.ManagerException as e:
print("Error: %s" % e.strerror)
sys.exit(1)
finally:
# remember to clean up
manager.close()
Remember all header, response, and event names are case sensitive.
Not all manager actions are implemented as of yet, feel free to add them and submit patches.
Specification
- class asterisk.manager.Event(message)[source]
Bases:
object
Manager interface Events, __init__ expects a ‘ManagerMsg’ message
- class asterisk.manager.Manager[source]
Bases:
object
TODO: add a docstring.
- get_actionID()[source]
Return an unique actionID, with a shared prefix for all actionIDs generated by this Manager instance.
- message_loop()[source]
The method for the event thread.
This actually receives all types of messages and places them in the proper queues.
- originate(channel, exten, context='', priority='', timeout='', application='', data='', caller_id='', run_async=False, earlymedia='false', account='', variables={})[source]
Originate a call
- register_event(event, function)[source]
Register a callback for the specified event.
If a callback function returns True, no more callbacks for that event will be executed.
- send_action(cdict: Dict, **kwargs)[source]
Send a command to the manager
If a list is passed to the cdict argument, each item in the list will be sent to asterisk under the same header in the following manner:
cdict = {"Action": "Originate", "Variable": ["var1=value", "var2=value"]} send_action(cdict) ...
Action: Originate Variable: var1=value Variable: var2=value
- exception asterisk.manager.ManagerAuthException[source]
Bases:
ManagerException
- exception asterisk.manager.ManagerSocketException[source]
Bases:
ManagerException