Python上的Asterisk Call Manager挂起事件

时间:2014-05-06 08:13:09

标签: python asterisk

如何通过AMI挂断最近发起的呼叫。我使用下面的代码来发起呼叫,但我不知道我应该如何挂断相同的来电。

import socket

HOST = 'localhost'
PORT = 5038

p = '''Action: login
Events: off
Username: %(username)s
Secret: %(password)s

Action: originate
Channel: Mobile/g1/%(phone_to_dial)s
WaitTime: 30
Timeout: 5
Exten: %(phone_to_dial)s
Context: home
Priority: 1



Action: Logoff
'''
def click_to_call(phone_to_dial, username, password, local_user):
    pattern = p % {
        'phone_to_dial': phone_to_dial,
        'username': username,
        'password': password,
        'local_user': local_user}
    s = socket.socket()
    s.connect((HOST,PORT))

    data = s.recv(1024)
    for l in pattern.split('\n'):
        print 'sending>>',1
        s.send(l+'\r\n')
        if l == "":
            data = s.recv(1024)
            print data
    data = s.recv(1024)
    s.close()

if __name__=='__main__':
    click_to_call('918800726989',username='600',password='yourpass',local_user='11111')

我尝试添加Action: hangup,但它似乎无法正常工作。请帮忙。?

更新代码&添加操作,状态和挂断后出现错误:

import socket

HOST = '192.168.1.3'
PORT = 5038

p = '''Action: Login
Events: off
Username: %(username)s
Secret: %(password)s
ActionID: 1

Action: Originate
ActionID: 2
Channel: SIP/11111
Exten: %(phone_to_dial)s
Context: home
priority: 1

Action: Status
ActionID: 3
Channel: SIP/11111

Action: Hangup
ActionID: 4
Channel: SIP/11111

Action: Logoff
ActionID: 5

'''
def click_to_call(phone_to_dial, username, password, local_user):
    pattern = p % {
        'phone_to_dial': phone_to_dial,
        'username': username,
        'password': password,
        'local_user': local_user}
    s = socket.socket()
    s.connect((HOST,PORT))

    data = s.recv(1024)
    for l in pattern.split('\n'):
        s.send(l+'\r\n')
        if l == "":
            data = s.recv(1024)
            print data
    data = s.recv(1024)

    s.close()



if __name__=='__main__':
    click_to_call('55555',username='600',password='yourpass',local_user='11111')

运行上述代码后得到的消息:

>>> ================================ RESTART ================================
>>> 
Response: Success


ActionID: 1

Message: Authentication accepted




Response: Success


ActionID: 2

Message: Originate successfully queued



Response: Error

ActionID: 3

Message: No such channel




Response: Error

ActionID: 4

Message: No such channel




Response: Goodbye


>>> 

1 个答案:

答案 0 :(得分:1)

你需要

1)监听事件并查看新创建频道的频道ID

http://www.voip-info.org/wiki/view/asterisk+manager+events

2)将Hangup操作(NOT事件)与频道名称

放在一起

http://www.voip-info.org/wiki/view/Asterisk+Manager+API+Action+Hangup