Commit 25653f44 authored by okhin's avatar okhin 🚴

Fixing all the bridge process

parent f1b7815a
Pipeline #23 skipped
......@@ -130,20 +130,15 @@ class Call(object):
logging.debug('Stopping the playback currently running'.format(payload,))
# We're stoping the playback, with the same ID as the channel, to keep track of it
r = requests.delete('http://185.34.33.12:8088/ari/playbacks/{0}'.format(event['channel']['id']), data=payload)
# Now we're moving the channel to the MOH Bridge
payload['channel'] = event['channel']['id']
logging.debug('Moving call {} to the garage'.format(payload['channel']))
r = requests.post('http://185.34.33.12:8088/ari/bridges/piphone-moh/addChannel', data=payload)
# Next we need to originate a call to the other side
logging.info('Will now connect {} to {}'.format(self.caller, self.callee,))
payload = {}
payload['app'] = 'piphone'
payload['api_key'] = ':'.join([login_ari, token_ari])
payload['endpoint'] = 'SIP/' + sanitize_phonenumber(self.callee) + '@forfait-kwaoo'
logging.debug('Preparing to send a request to the ARI with payload {}'.format(payload,))
r = requests.post('http://185.34.33.12:8088/ari/channels/{}-{}'.format(self.id, sanitize_phonenumber(self.callee),), data=payload)
# And now, just need to add the channel to the bridge
payload = {}
payload['app'] = 'piphone'
payload['api_key'] = ':'.join([login_ari, token_ari])
payload['channel'] = '{}-{}'.format(self.id, sanitize_phonenumber(self.callee),)
r = requests.post('http://185.34.33.12:8088/ari/bridges/{}/addChannel'.format(self.id,), data=payload)
except AssertionError as e:
logging.error("Received a DTMF sequence out le being in a '{}' state, ignoring: {}".format(self.state(), event['digit']))
......@@ -154,16 +149,27 @@ class Call(object):
self.update((':'.join([event['channel']['state'], event['channel']['id'].split('-')[-1]]), event['timestamp'],))
logging.info("New state for call {}: {}".format(event['channel']['id'], event['channel']['state']))
# We now need to take action according to our new state
results = self.db.execute('SELECT login_ari, pass_ari FROM users WHERE api = ?', (self.owner,))
login_ari, token_ari = results.fetchone()
if event['channel']['state'] == 'Up':
# Are we the caller orthe callee?
if event['channel']['id'].endswith(self.callee):
# Callee side, let's just move
# Step 1 create a bridge
payload = {}
payload['app'] = 'piphone'
payload['api_key'] = ':'.join([login_ari, token_ari])
payload['type'] = 'mixing'
logging.debug("Creating a bridges to connect {} to {}".format(self.caller, self.callee,))
r = requests.post('http://185.34.33.12:8088/ari/bridges/{}'.format(self.id), data=payload)
# Step 2, moving channels
payload['channel'] = ",".join([self.id + '-' + self.caller, self.id + '-' + self.callee])
logging.debug("Moving channels to the created bridge: {}".format(payload['channel']))
r = requests.post('http://185.34.33.12:8088/ari/bridges/{}/addChannel'.format(self.id), data=payload)
logging.info("Call now fully connected: {} <-> {}".format(self.caller, self.callee))
return
# Call is being picked up, we want to play a song
payload = {}
try:
results = self.db.execute('SELECT login_ari, pass_ari FROM users WHERE api = ?', (self.owner,))
login_ari, token_ari = results.fetchone()
payload['app'] = 'piphone'
payload['api_key'] = ':'.join([login_ari, token_ari])
payload['media'] = 'sound:mario'
......@@ -178,13 +184,10 @@ class Call(object):
def call_caller(self, event):
'''
We want to call the caller. Which needs to be done via a bridge.
Fisrt we set-up a bridge, then we originate a channel, postfixing the bridge id
with the caller number.
Let's call the caller. It's a simple originate. We will also check if the MoH bridge is ready,
because it will be used to store people in it waiting for the callee to pick up the phone.
And then we connect the caller to the bridge.
The bridge is a mixed one.
The bridge needed to connect the calls together will be created later.
'''
self.update((':'.join([event['type'], event['channel']['id'].split('-')[-1]]), event['timestamp'],))
results = self.db.execute('SELECT login_ari, pass_ari FROM users WHERE api = ?', (self.owner,))
......@@ -192,13 +195,13 @@ class Call(object):
payload = {}
payload['app'] = 'piphone'
payload['api_key'] = ':'.join([login_ari, token_ari])
# Let's establish a bridge over the Kwai River
# We want to check if the moh bridge is opened
try:
payload['type'] = 'mixing'
logging.debug("Initiating a bridge for call {}".format(self.id,))
r = requests.post('http://185.34.33.12:8088/ari/bridges/{}'.format(self.id,), data=payload)
except Exception as e:
logging.exception(e)
r = requests.get('http://185.34.33.12:8088/ari/bridges/{}-moh'.format(payload['app']), data=payload)
r.raise_for_status()
except HTTPError as e:
# The bridge dos not exist
logging.error("The Hold bridges does not exists")
raise e
# Now, let's create the channel
......@@ -212,17 +215,6 @@ class Call(object):
except Exception as e:
logging.exception(e)
raise e
# Let's connect the channel to the bridge.
payload = {}
payload['app'] = 'piphone'
payload['api_key'] = ':'.join([login_ari, token_ari])
try:
payload['channel'] = '{}-{}'.format(self.id, sanitize_phonenumber(self.caller),)
logging.debug("Connection the channel to the bridge for call {}".format(self.id,))
r = requests.post('http://185.34.33.12:8088/ari/bridges/{}/addChannel'.format(self.id,), data=payload)
except Exception as e:
logging.exception(e)
raise e
def save(self):
'''
......@@ -304,12 +296,12 @@ class Server(ServerAdapter):
ws_logger.setLevel(logging.DEBUG)
ws_logger.addHandler(logging.FileHandler('app.log'))
async with websockets.connect('ws://185.34.33.12:8088/ari/events?app=piphone&api_key=piphone:passpiphone') as websocket:
await self.hold_bridges()
while self.running == True:
ws_logger.info("Waiting for events on websocket")
event = await websocket.recv()
# Let's call the applications function
await self.dispatch(json.loads(event))
# This is now where I want to create callbacks from events
async def dispatch(self, event):
"""
......@@ -325,6 +317,26 @@ class Server(ServerAdapter):
call = Call.load(call_id, self.db)
call.event_handler(event)
async def hold_bridges(self):
'''
We will create a bridge that will be used to park caller waiting for callee to join.
It will play a song in a loop.
'''
payload = {}
payload['app'] = 'piphone'
payload['api_key'] = 'piphone:passpiphone'
payload['mode'] = 'holding'
try:
r = requests.post('http://185.34.33.12:8088/ari/bridges/piphone-moh', data=payload)
r.raise_for_status()
payload['mohClass'] = 'piphone'
r = requests.post('http://185.34.33.12:8088/ari/bridges/piphone-moh/moh', data=payload)
r.raise_for_status()
except Exception as e:
logging.error("Cannot start MOH Bridges")
logging.exception(e)
raise e
def run(self, handler):
"""
We're starting the REST application, and the launch applications
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment