Advertisement
MaxMokryi

Asterisk AMI

Aug 10th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import time
  2. from asterisk.ami import AMIClient
  3. from asterisk.ami import EventListener
  4.  
  5. connection = {
  6.     'address': 'sip.*********.com',
  7.     'port': 5038
  8. }
  9.  
  10. login = {
  11.     'username': 'admin',
  12.     'secret': '***********'
  13. }
  14.  
  15.  
  16. def event_notification(source, event):
  17.     if event.name in ['AgentConnect']:
  18.         # print(f'name: {event.name}')
  19.         # print(f'event: {vars(event)}')
  20.         print(f'event: {event.keys.get("CallerIDNum", "Nope")} -> {event.keys.get("ConnectedLineNum", "Nope")}')
  21.     return
  22.  
  23.  
  24. client = AMIClient(**connection)
  25. future = client.login(**login)
  26. if future.response.is_error():
  27.     raise Exception(str(future.response))
  28.  
  29. client.add_event_listener(
  30.     EventListener(
  31.         on_event=event_notification,
  32.         # white_list='Newstate',
  33.         # ChannelStateDesc='Ringing'
  34.     )
  35. )
  36.  
  37. try:
  38.     print('Listener started')
  39.     while True:
  40.         time.sleep(10)
  41. except (KeyboardInterrupt, SystemExit):
  42.     client.logoff()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement