通过Python+AMI发起SIP外呼失败,Asterisk报通道未注册错误
问题:Python调用AMI发起外呼时Asterisk报错"No channel type registered for 'SIP'"
我尝试用Python调用Asterisk管理接口(AMI)发起外呼,代码如下:
import socket ami_host = 'localhost' ami_port = 5038 ami_username = 'user' ami_password = 'pass' destination_number = 'dest_nubmber' caller_id = 'caller_id' def connect_to_ami(): ami_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ami_socket.connect((ami_host, ami_port)) response = ami_socket.recv(4096).decode('utf-8') print(response) ami_socket.send(f'Action: Login\r\nUsername: {ami_username}\r\nSecret: {ami_password}\r\n\r\n'.encode('utf-8')) response = ami_socket.recv(4096).decode('utf-8') print(response) return ami_socket def make_outgoing_call(destination, caller_id): ami_socket.send(f'Action: Originate\r\nChannel: SIP/sipus/{destination}\r\nExten: {destination}\r\nContext: from-internal\r\nCallerID: {caller_id}\r\nAsync: true\r\n\r\n'.encode('utf-8')) response = ami_socket.recv(4096).decode('utf-8') print(response) def close_ami_connection(): ami_socket.send('Action: Logoff\r\n\r\n'.encode('utf-8')) ami_socket.close() ami_socket = connect_to_ami() make_outgoing_call(destination_number, caller_id) close_ami_connection()
代码运行无异常,但Asterisk日志中出现如下错误:
WARNING[3907] channel.c: No channel type registered for 'SIP'
解决方案
这个错误核心是Asterisk无法识别你指定的SIP通道类型,可从以下几个方向排查修复:
检查SIP模块加载状态
- 进入Asterisk控制台(执行
asterisk -rvv),输入module show like sip查看模块状态:- 若显示
chan_sip.so未加载,修改/etc/asterisk/modules.conf,添加load => chan_sip.so后重启Asterisk - 新版本Asterisk(13+)默认用PJSIP,需确认
chan_pjsip.so已加载
- 若显示
- 进入Asterisk控制台(执行
修正通道参数格式
- 传统SIP(chan_sip)的正确Channel格式为
SIP/{destination}@{trunk_name},而非你代码中的SIP/sipus/{destination} - 若使用PJSIP,通道类型需改为
PJSIP,格式为PJSIP/{destination}@{endpoint_name},修正代码示例:def make_outgoing_call(destination, caller_id): # 替换为你的PJSIP端点名称 ami_socket.send(f'Action: Originate\r\nChannel: PJSIP/{destination}@sipus\r\nExten: {destination}\r\nContext: from-internal\r\nCallerID: {caller_id}\r\nAsync: true\r\n\r\n'.encode('utf-8')) response = ami_socket.recv(4096).decode('utf-8') print(response)
- 传统SIP(chan_sip)的正确Channel格式为
确认端点/中继配置有效性
- 检查
/etc/asterisk/sip.conf(传统SIP)或pjsip.conf(PJSIP)中,是否存在名为sipus的中继/端点配置,确保配置参数正确
- 检查
内容的提问来源于stack exchange,提问作者Ace
相关产品推荐
相关产品推荐

