Python AMI Client
Project description
A simple Python AMI client
See the code of conduct.
Install
Install asterisk-ami
pip install asterisk-ami
Install latest asterisk-ami
pip install git+https://github.com/ettoreleandrotognoli/python-ami
Usage
Connect
from asterisk.ami import AMIClient
client = AMIClient(address='127.0.0.1',port=5038)
client.login(username='username',secret='password')
Disconnect
client.logoff()
Send an action
from asterisk.ami import SimpleAction
action = SimpleAction(
'Originate',
Channel='SIP/2010',
Exten='2010',
Priority=1,
Context='default',
CallerID='python',
)
client.send_action(action)
Send an action with adapter
from asterisk.ami import AMIClientAdapter
adapter = AMIClientAdapter(client)
adapter.Originate(
Channel='SIP/2010',
Exten='2010',
Priority=1,
Context='default',
CallerID='python',
)
Synchronous Response
#without adapter
future = client.send_action(action)
response = future.response
#with adapter
future = adapter.Originate(...)
response = future.response
Asynchronous Response
def callback_response(response):
print(response)
#without adapter
future = client.send_action(action,callback=callback_response)
#with adapter
future = adapter.Originate(...,_callback=callback_response)
#you can use the future to wait the callback execute
reponse = future.response
Listen Events
def event_listener(event,**kwargs):
print(event)
client.add_event_listener(event_listener)
Filter Events
With a custom class
from asterisk.ami import EventListener
class RegistryEventListener(EventListener):
def on_Registry(event,**kwargs):
print('Registry Event',event)
client.add_event_listener(RegistryEventListener())
class AllEventListener(EventListener):
def on_event(event,**kwargs):
print('Event',event)
client.add_event_listener(AllEventListener())
With black or white list
def event_listener(event,**kwargs):
print(event)
client.add_event_listener(
event_listener, white_list=['Registry','PeerStatus']
)
client.add_event_listener(
event_listener, black_list=['VarSet']
)
Like a custom class
def event_listener(event,**kwargs):
print(event)
client.add_event_listener(
on_VarSet=event_listener,
on_ExtensionStatus=event_listener
)
client.add_event_listener(
on_event=event_listener
)
Filter Event Value
def event_listener(event,**kwargs):
print('Ringing',event)
client.add_event_listener(
event_listener,
white_list='Newstate',
ChannelStateDesc='Ringing',
ConnectedLineNum='2004',
)
Filter with regex
import re
def event_listener(event,**kwargs):
print(event)
client.add_event_listener(
on_Newstate=event_listener,
white_list=re.compile('.*'),
ChannelStateDesc=re.compile('^Ring.*'),
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
asterisk-ami-0.1.7.tar.gz
(33.4 kB
view details)
Built Distribution
File details
Details for the file asterisk-ami-0.1.7.tar.gz
.
File metadata
- Download URL: asterisk-ami-0.1.7.tar.gz
- Upload date:
- Size: 33.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de954116b7b03fb1b5420d9d83d847a3ba0d4cf1449847eada88b4bfde080136 |
|
MD5 | b8b70e5ada4d05d6ce619ce91583bd6f |
|
BLAKE2b-256 | 4601ae378a9b0ae4516bc2eee7332e2296a3574bb42feae5ccf2bd8cf7f1968c |
File details
Details for the file asterisk_ami-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: asterisk_ami-0.1.7-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b94baabc1107214f3369a804fd1efda0649ba6afcb21095c6b438b6245fef7ff |
|
MD5 | 43191867f9734ff3cfd5ddfbee6d235f |
|
BLAKE2b-256 | 3798a29af5201e7a2c9c948debe134def3a636f73fb884710e6afb30771e487f |