I wrote a script on python 3 that makes only one call, if the caller answered, then we play the melody and hang up. I use ari asterisk. How can this be done for only a few numbers?
#!/usr/bin/env python3
import json
import sys
import websocket
import requests
import time
class ARIInterface(object):
def __init__(self, server_addr, username, password):
self._req_base = "http://%s:8088/ari/" % server_addr
self._username = username
self._password = password
def answer_call(self, channel_id):
req_str = self._req_base + "channels/%s/answer" % channel_id
self._send_post_request(req_str)
def play_sound(self, channel_id, sound_name):
req_str = self._req_base + ("channels/%s/play?media=sound:%s" % (channel_id, sound_name))
self._send_post_request(req_str)
def cr_channel(self, number):
req_str = self._req_base + ("channels/create?endpoint=SIP/25002/%s&app=hello" % (number))
self._send_post_request(req_str)
def dial_channel(self, channel_id):
req_str = self._req_base + ("channels/%s/dial?timeout=30" % (channel_id))
self._send_post_request(req_str)
def del_channel(self, channel_id):
req_str = self._req_base + ("channels/%s" % (channel_id))
self._send_delete_request(req_str)
def _send_post_request(self, req_str):
r = requests.post(req_str, auth=(self._username, self._password))
def _send_delete_request(self, req_str):
r = requests.delete(req_str, auth=(self._username, self._password))
class ARIApp(object):
def __init__(self, server_addr):
app_name = 'hello'
username = 'ari-user'
password = 'ari-ewsd-135'
url = "ws://%s:8088/ari/events?app=%s&api_key=%s:%s" % (server_addr, app_name, username, password)
ari = ARIInterface(server_addr, username, password)
ws = websocket.create_connection(url)
numbers = [21018, 35000]
ari.cr_channel('21018')
channel_id = json.loads(ws.recv())['channel']['id']
ari.dial_channel(channel_id)
try:
for event_str in iter(lambda: ws.recv(), None):
event_json = json.loads(event_str)
json.dump(event_json, sys.stdout, indent=2, sort_keys=True,
separators=(',', ': '))
print("\n\nWebsocket event***************************************************\n")
try:
if event_json['dialstatus'] == 'ANSWER':
peer_id = event_json['peer']['id']
ari.play_sound(peer_id, 'hello-world')
time.sleep(2)
ari.del_channel(peer_id)
except:
pass
except websocket.WebSocketConnectionClosedException:
print("Websocket connection closed")
except KeyboardInterrupt:
print("Keyboard interrupt")
finally:
if ws:
ws.close()
if __name__ == "__main__":
app = ARIApp('localhost')
I tried to change the Ariapp class as follows:
class ARIApp(object):
def __init__(self, server_addr):
app_name = 'hello'
username = 'ari-user'
password = 'ari-ewsd-135'
url = "ws://%s:8088/ari/events?app=%s&api_key=%s:%s" % (server_addr, app_name, username, password)
ari = ARIInterface(server_addr, username, password)
ws = websocket.create_connection(url)
numbers = [21018, 35000]
for number in numbers:
ari.cr_channel(number)
channel_id = json.loads(ws.recv())['channel']['id']
ari.dial_channel(channel_id)
try:
for event_str in iter(lambda: ws.recv(), None):
event_json = json.loads(event_str)
json.dump(event_json, sys.stdout, indent=2, sort_keys=True,
separators=(',', ': '))
print("\n\nWebsocket event***************************************************\n")
try:
if event_json['dialstatus'] == 'ANSWER':
peer_id = event_json['peer']['id']
ari.play_sound(peer_id, 'hello-world')
time.sleep(2)
ari.del_channel(peer_id)
except:
pass
except websocket.WebSocketConnectionClosedException:
print("Websocket connection closed")
except KeyboardInterrupt:
print("Keyboard interrupt")
finally:
if ws:
ws.close()
You can add as many channels as you want. There is no limit
You should use Originate AMI call or call files if you want independent channels