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()
-
I wanted to fix the code, but the forum won't let me do it.royun– royun2024年03月12日 08:38:17 +00:00Commented Mar 12, 2024 at 8:38
-
you need more reputation and thats all.arheops– arheops2024年03月12日 18:11:16 +00:00Commented Mar 12, 2024 at 18:11
1 Answer 1
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
answered Mar 12, 2024 at 18:11
arheops
15.3k1 gold badge23 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
royun
Is it really impossible to implement this on an ami?
arheops
What? AMI has action Originate.
royun
Indeed, I can make one call without any problems. It is not possible to make exactly several calls.
arheops
Do it twice? What prevent you from doing that?
royun
I apologize for not responding for a long time. I made a typo at the top, I meant ARI. Is it possible to make multiple calls using the API?
lang-py