2016-09-14 89 views
-1

我有以下功能,目標是從Asterisk服務器的Python Web界面接聽來電。星號AMI來電不會返回任何電話

def fetch_events(event, manager): 

with app.app_context(): 

    if event.name == 'CoreShowChannel': 
     id = event.message['accountcode'] 
     data = { 
       'user_id': id, 
       'caller_id': event.message['CallerIDnum'], 
       'channel': event.message['Channel'], 
       'duration': event.message['Duration'], 
       'context': event.message['Context'], 
       'extension': event.message['Extension'], 
       'line': event.message['ConnectedLineNum'], 
       #'channel_state': event.message['ChannelState'], 
       'channel_state': event.message['ChannelStateDesc'], 
       } 
     user = System().getUserById(id) 
     if user: 
      profile = { 
         'firstname': user['firstname'], 
         'lastname': user['lastname'] 
         'email': user['email'] 
        } 

     else: 
      profile = { 
         'first_name': "No firstname", 
         'last_name': "No lastname" 
        } 
     data.update(profile) 
     g.channels.append(data) 
    if event.name == 'CoreShowChannelsComplete': 
     g.complete = True 

    if not event.name: 
     data = { 
      "connectivity":"Not connected", 
      "event-name":"No event name" 
     } 
     g.channels.append(data) 
     g.complete = True 


    @app.route('/incoming-calls') 
    def incoming_calls(): 
    /**** I have already login and connect *****/ 
    g.channels = [] 
    g.complete = False 
    manager.register_event('*', fetch_events) 
    res = manager.send_action({'Action':'CoreShowChannels'}) 

    try: 

     while not g.complete: 

      time.sleep(0.5) 
     manager.close() 
     return json.dumps(g.channels) 

但是,當我嘗試在註冊handle_event方法後獲得傳入調用事件時,我得到一個空數組。看起來handle_event方法有問題,但我找不到它。

回答

0

您應該使用(偵聽)'Newexten'事件並從該事件中提取'context',然後查看來自傳入上下文的調用是否爲true,然後使用來自該事件的其他變量。不要使用操作'CoreShowChannel',因爲它會列出當前活動的頻道。

+0

請Milorad,你能更好地向我解釋它在代碼中。我仍然不明白你。 – Oladapo

+0

甚至我目前使用的CoreShowChannel也不會顯示任何活動的調用。 – Oladapo

+0

我是檢查這樣的事件名稱 - >如果event.name =='Newexten',註冊併發送類似這樣的操作 - > manager.register_event('*',handle_event) res = manager.send_action({ Action':'Newevent'}) – Oladapo

0

當有人打電話給Asterisk,例如呼入到中繼線號碼時,Asterisk發射'Newexten'事件。該事件的數據結構是:

`{ event: 'Newexten', 
    privilege: 'call,all', 
    channel: 'SIP/NameOfyourSIPprovider-0000ae7d',//channel that will be bridged 
    channelstate: '0', 
    channelstatedesc: 'Down',//the channel is currently down because no one is answered 
    calleridnum: '060444333',// num of calling party 
    calleridname: '<unknown>', 
    connectedlinenum: '+34312345678',//your trunk num 
    connectedlinename: '<unknown>', 
    language: 'en', 
    accountcode: '', 
    context: 'from-trunk',//context(extensions.conf) for incoming calls 
    exten: '060444333',//num of calling party 
    priority: '1', 
    uniqueid: '1471265139.357602',//unique id of channel 
    linkedid: '1471265138.357596',//linked id of channel that will be bridged 
    extension: '060444333',//num of calling party 
    application: 'AppDial', 
    appdata: '(Outgoing Line)' }` 
+0

Oladapo您必須閱讀本文檔https://wiki.asterisk.org/wiki/display/AST/AMI+v2+Specification以瞭解呼叫流程和AMI協議。我個人使用Node.js來操縱AMI。我不熟悉python和AMI界面。 –

+0

Milorad,當我打電話時,它還沒有進入生產模式,我得到一個IVR,你認爲這可能是原因嗎?因爲即使當我使用CoreShowChannel時,我也沒有得到任何活動的調用對象 – Oladapo

+0

我的問題是這樣的,如果你在github上查看我的源代碼https://github.com/Pinsonbavard/myproject,我註冊併發送了什麼動作/斑點/主/星號/ AMI。我知道我要註冊哪個我註冊(*)意思是全部,併發送行動 – Oladapo