基于Asterisk与AMI实现ATA自动外呼并播放音频的API搭建咨询
Got it, let's walk through how to set up that automated outbound call using Asterisk AMI without needing a softphone. Here's a step-by-step breakdown tailored to your setup:
First, make sure your Asterisk Manager Interface (AMI) is properly enabled and configured with a user that has the right permissions:
- Open your Asterisk manager config file (
/etc/asterisk/manager.conf) and add/update these settings:[general] enabled = yes port = 5038 bindaddr = 0.0.0.0 ; Allow local or remote access as needed [api_user] secret = your_strong_password_here permit = 127.0.0.1/255.255.255.0 ; Restrict to trusted IPs for security read = originate, call, playback write = originate, call, playback ; Grant only necessary permissions - Restart Asterisk to apply changes:
asterisk -rx "core restart now"
We'll use Python with the pyst2 library (a lightweight AMI binding) to create a script that initiates calls when triggered (you can hook this up to a web button later).
- Install the library first:
pip install pyst2 - Here's a sample script to handle the outbound call and audio playback:
from asterisk.manager import Manager def trigger_outbound_call(target_number, audio_file): manager = Manager() try: # Connect to local AMI instance manager.connect('localhost', 5038) manager.login('api_user', 'your_strong_password_here') # Define the originate action # Replace SIP/ata_line with your actual ATA channel name (check via `sip show peers`) originate_cmd = { 'Action': 'Originate', 'Channel': f'SIP/ata_line/{target_number}', 'Context': 'auto_outbound', 'Exten': 's', 'Priority': 1, 'Variable': f'AUDIO_FILE={audio_file}', 'Async': 'true' ; Run asynchronously so the script doesn't block } # Send the command to Asterisk response = manager.send_action(originate_cmd) print(f"Call initiated successfully: {response.response}") except Exception as e: print(f"Error triggering call: {str(e)}") finally: manager.logoff() manager.close() # Test the function (replace with your target number and audio path) if __name__ == "__main__": trigger_outbound_call('1234567890', '/var/lib/asterisk/sounds/custom/your_greeting.wav')
Add a custom dialplan context in /etc/asterisk/extensions.conf to handle the audio playback once the call connects:
[auto_outbound] exten => s,1,NoOp(Starting automated outbound call playback) exten => s,n,Playback(${AUDIO_FILE}) ; Play the passed audio file exten => s,n,Hangup() ; Hang up after playback finishes
- Notes:
- Place your audio file in Asterisk's default sounds directory (
/var/lib/asterisk/sounds/) or use an absolute path. - Ensure the file is in a compatible format (WAV, GSM) and that the
asteriskuser has read permissions to it.
- Place your audio file in Asterisk's default sounds directory (
To make this triggerable via a button click, you can wrap the Python script in a simple web endpoint (using Flask or FastAPI):
- Example Flask endpoint:
from flask import Flask app = Flask(__name__) @app.route('/call/<phone_number>') def make_call(phone_number): trigger_outbound_call(phone_number, '/var/lib/asterisk/sounds/custom/your_greeting.wav') return "Call initiated!" if __name__ == "__main__": app.run(host='0.0.0.0', port=5000) - Now you can create a simple HTML button that sends a request to
http://your-server-ip:5000/call/1234567890when clicked.
- If calls fail, check Asterisk's full log (
/var/log/asterisk/full) for error messages (common issues: wrong channel name, invalid audio file, AMI permissions). - Verify your ATA channel name with
asterisk -rx "sip show peers". - Ensure port 5038 isn't blocked by your firewall.
内容的提问来源于stack exchange,提问作者Amit

