Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
piphone
piphone-sip
Commits
c7e65d25
Commit
c7e65d25
authored
May 09, 2016
by
okhin
Browse files
Using the ari.py in the main application
parent
b57018d9
Changes
1
Show whitespace changes
Inline
Side-by-side
app.py
View file @
c7e65d25
...
@@ -16,10 +16,10 @@ from operator import itemgetter
...
@@ -16,10 +16,10 @@ from operator import itemgetter
import
jwt
import
jwt
import
websockets
import
websockets
import
requests
from
bottle
import
request
,
abort
,
Bottle
,
JSONPlugin
from
bottle
import
request
,
abort
,
Bottle
,
JSONPlugin
from
bottle_sqlite
import
SQLitePlugin
from
bottle_sqlite
import
SQLitePlugin
from
daemonize
import
Daemonize
import
ari
arg_parser
=
argparse
.
ArgumentParser
(
description
=
'Manage the SIP Backend for the piphone'
)
arg_parser
=
argparse
.
ArgumentParser
(
description
=
'Manage the SIP Backend for the piphone'
)
config
=
configparser
.
ConfigParser
()
config
=
configparser
.
ConfigParser
()
...
@@ -150,16 +150,15 @@ async def hold_bridge():
...
@@ -150,16 +150,15 @@ async def hold_bridge():
We will create a bridge that will be used to park caller waiting for callee to join.
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.
It will play a song in a loop.
'''
'''
payload
=
{}
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
'piphone:passpiphone'
payload
[
'mode'
]
=
'holding'
try
:
try
:
r
=
requests
.
post
(
'http://185.34.33.12:8088/ari/bridges/piphone-moh'
,
data
=
payload
)
bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
config
[
'moh'
][
'name'
],
'holding'
)
r
.
raise_for_status
()
except
Exception
as
e
:
payload
[
'mohClass'
]
=
'piphone'
phone_logger
.
critical
(
"No Music On Hold (moh) section in config file. Exiting"
)
r
=
requests
.
post
(
'http://185.34.33.12:8088/ari/bridges/piphone-moh/moh'
,
data
=
payload
)
phone_logger
.
Exception
(
e
)
r
.
raise_for_status
()
raise
e
try
:
result
=
bridge
.
create
()
result
=
bridge
.
startMoh
(
self
.
config
[
'moh'
][
'class'
])
except
Exception
as
e
:
except
Exception
as
e
:
phone_logger
.
error
(
"Cannot start 'on hold' bridge"
)
phone_logger
.
error
(
"Cannot start 'on hold' bridge"
)
phone_logger
.
exception
(
e
)
phone_logger
.
exception
(
e
)
...
@@ -205,7 +204,7 @@ async def dispatch(self, event):
...
@@ -205,7 +204,7 @@ async def dispatch(self, event):
class
Call
(
object
):
class
Call
(
object
):
"""
"""
This Class is used to manage operatio
s
n on a call, to print it and dump it.
This Class is used to manage operation
s
on a call, to print it and dump it.
"""
"""
history
=
[]
history
=
[]
actions
=
{
'Created'
:
'call_caller'
actions
=
{
'Created'
:
'call_caller'
...
@@ -276,23 +275,21 @@ class Call(object):
...
@@ -276,23 +275,21 @@ class Call(object):
return
return
# Now, we're connectig the other side
# Now, we're connectig the other side
# But first we should stop the playback
# But first we should stop the playback
results
=
self
.
db
.
execute
(
'SELECT login_ari, pass_ari FROM users WHERE api = ?'
,
(
self
.
owner
,))
login_ari
,
token_ari
=
results
.
fetchone
()
payload
=
{}
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
phone_logger
.
debug
(
'Stopping the playback currently running'
.
format
(
payload
,))
phone_logger
.
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
# 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
)
playback
=
ari
.
Playback
(
self
.
config
[
'asterisk'
],
event
[
'channel'
][
'id'
],
'sound:mario'
)
playback
.
stop
()
# Now we're moving the channel to the MOH Bridge
# Now we're moving the channel to the MOH Bridge
payload
[
'channel'
]
=
event
[
'channel'
][
'id'
]
payload
[
'channel'
]
=
event
[
'channel'
][
'id'
]
phone_logger
.
debug
(
'Moving call {} to the garage'
.
format
(
payload
[
'channel'
]))
phone_logger
.
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
)
moh_bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
config
[
'moh'
][
'name'
])
moh_bridge
.
addChannel
(
event
[
'channel'
][
'id'
])
# Next we need to originate a call to the other side
# Next we need to originate a call to the other side
phone_logger
.
info
(
'Will now connect {} to {}'
.
format
(
self
.
caller
,
self
.
callee
,))
phone_logger
.
info
(
'Will now connect {} to {}'
.
format
(
self
.
caller
,
self
.
callee
,))
payload
[
'endpoint'
]
=
'SIP/'
+
sanitize_phonenumber
(
self
.
callee
)
+
'@forfait-kwaoo'
phone_logger
.
debug
(
'Preparing to send a request to the ARI with payload {}'
.
format
(
payload
,))
phone_logger
.
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
)
endpoint
=
'SIP/'
+
sanitize_phonenumber
(
self
.
callee
)
+
'@'
+
config
[
'asterisk'
][
'sip-context'
]
channel
=
ari
.
Channel
(
self
.
config
[
'asterisk'
],
self
.
id
+
'-'
+
sanitize_phonenumber
(
self
.
callee
))
channel
.
originate
(
endpoint
)
except
AssertionError
as
e
:
except
AssertionError
as
e
:
logging
.
error
(
"Received a DTMF sequence out le being in a '{}' state, ignoring: {}"
.
format
(
self
.
state
(),
event
[
'digit'
]))
logging
.
error
(
"Received a DTMF sequence out le being in a '{}' state, ignoring: {}"
.
format
(
self
.
state
(),
event
[
'digit'
]))
...
@@ -303,35 +300,27 @@ class Call(object):
...
@@ -303,35 +300,27 @@ class Call(object):
self
.
update
((
':'
.
join
([
event
[
'channel'
][
'state'
],
event
[
'channel'
][
'id'
].
split
(
'-'
)[
-
1
]]),
event
[
'timestamp'
],))
self
.
update
((
':'
.
join
([
event
[
'channel'
][
'state'
],
event
[
'channel'
][
'id'
].
split
(
'-'
)[
-
1
]]),
event
[
'timestamp'
],))
phone_logger
.
info
(
"New state for call {}: {}"
.
format
(
event
[
'channel'
][
'id'
],
event
[
'channel'
][
'state'
]))
phone_logger
.
info
(
"New state for call {}: {}"
.
format
(
event
[
'channel'
][
'id'
],
event
[
'channel'
][
'state'
]))
# We now need to take action according to our new 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'
:
if
event
[
'channel'
][
'state'
]
==
'Up'
:
# Are we the caller orthe callee?
# Are we the caller orthe callee?
if
event
[
'channel'
][
'id'
].
endswith
(
self
.
callee
):
if
event
[
'channel'
][
'id'
].
endswith
(
self
.
callee
):
# Step 1 create a bridge
# Step 1 create a bridge
payload
=
{}
bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
id
,
'mixing'
)
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
payload
[
'type'
]
=
'mixing'
phone_logger
.
debug
(
"Creating a bridges to connect {} to {}"
.
format
(
self
.
caller
,
self
.
callee
,))
phone_logger
.
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
)
bridge
.
create
(
)
# Step 2, moving channels
# Step 2, moving channels
payload
[
'
channel
'
]
=
","
.
join
([
self
.
id
+
'-'
+
self
.
caller
,
self
.
id
+
'-'
+
self
.
callee
])
channel
s
=
","
.
join
([
self
.
id
+
'-'
+
self
.
caller
,
self
.
id
+
'-'
+
self
.
callee
])
phone_logger
.
debug
(
"Moving channels to the created bridge: {}"
.
format
(
payload
[
'channel'
]))
phone_logger
.
debug
(
"Moving channels to the created bridge: {}"
.
format
(
payload
[
'channel'
]))
r
=
requests
.
post
(
'http://185.34.33.12:8088/ari/
bridge
s/{}/
addChannel
'
.
format
(
self
.
id
),
data
=
payload
)
bridge
.
addChannel
(
channels
)
phone_logger
.
info
(
"Call now fully connected: {} <-> {}"
.
format
(
self
.
caller
,
self
.
callee
))
phone_logger
.
info
(
"Call now fully connected: {} <-> {}"
.
format
(
self
.
caller
,
self
.
callee
))
return
return
# Call is being picked up, we want to play a song
# Call is being picked up, we want to play a song
payload
=
{}
try
:
try
:
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
payload
[
'media'
]
=
'sound:mario'
payload
[
'media'
]
=
'sound:mario'
payload
[
'lang'
]
=
'en_US'
payload
[
'lang'
]
=
'en_US'
channel
=
ari
.
Channel
(
self
.
config
[
'asterisk'
],
event
[
'channel'
][
'id'
])
logging
.
debug
(
'Preparing to send a request to the ARI with payload {}'
.
format
(
payload
,))
logging
.
debug
(
'Preparing to send a request to the ARI with payload {}'
.
format
(
payload
,))
# We're starting a playback, with the same ID as the channel, to keep track of it
# We're starting a playback, with the same ID as the channel, to keep track of it
r
=
requests
.
post
(
'http://185.34.33.12:8088/ari/channels/{0}/play/{0}'
.
format
(
event
[
'channel'
][
'id'
]),
data
=
payload
)
channel
.
playback
(
channel
.
name
,
self
.
config
[
'asterisk'
][
'playback'
])
logging
.
debug
(
"Now playing a sound on channel {}"
.
format
(
self
.
id
))
except
Exception
as
e
:
except
Exception
as
e
:
logging
.
exception
(
e
)
logging
.
exception
(
e
)
raise
e
raise
e
...
@@ -344,28 +333,21 @@ class Call(object):
...
@@ -344,28 +333,21 @@ class Call(object):
The bridge needed to connect the calls together will be created later.
The bridge needed to connect the calls together will be created later.
'''
'''
self
.
update
((
':'
.
join
([
event
[
'type'
],
event
[
'channel'
][
'id'
].
split
(
'-'
)[
-
1
]]),
event
[
'timestamp'
],))
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
,))
login_ari
,
token_ari
=
results
.
fetchone
()
payload
=
{}
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
# We want to check if the moh bridge is opened
# We want to check if the moh bridge is opened
moh_bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
config
[
'moh'
][
'name'
],
'holding'
)
try
:
try
:
r
=
requests
.
get
(
'http://185.34.33.12:8088/ari/bridges/{}-moh'
.
format
(
payload
[
'app'
]),
data
=
payload
)
moh_bridge
.
status
()
r
.
raise_for_status
()
except
HTTPError
as
e
:
except
HTTPError
as
e
:
# The bridge dos not exist
# The bridge dos not exist
logging
.
error
(
"The Hold bridges does not exists"
)
logging
.
error
(
"The Hold bridges does not exists"
)
raise
e
raise
e
# Now, let's create the channel
# Now, let's create the channel
payload
=
{}
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
try
:
try
:
payload
[
'
endpoint
'
]
=
'SIP/'
+
sanitize_phonenumber
(
self
.
caller
)
+
'@
forfait-kwaoo'
endpoint
=
'SIP/'
+
sanitize_phonenumber
(
self
.
caller
)
+
'@
'
+
self
.
config
[
'asterisk'
][
'sip-context'
]
logging
.
debug
(
'Preparing to send a request to the ARI with payload {}'
.
format
(
payload
,))
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
.
caller
),),
data
=
payload
)
channel
=
ari
.
Channel
(
self
.
config
[
'asterisk'
],
self
.
id
+
'-'
+
sanitize_phonenumber
(
self
.
caller
))
channel
.
originate
()
except
Exception
as
e
:
except
Exception
as
e
:
logging
.
exception
(
e
)
logging
.
exception
(
e
)
raise
e
raise
e
...
@@ -466,5 +448,5 @@ if __name__ == '__main__':
...
@@ -466,5 +448,5 @@ if __name__ == '__main__':
phone_logger
.
info
(
"Starting the piphone SIP backend"
)
phone_logger
.
info
(
"Starting the piphone SIP backend"
)
try
:
try
:
start
()
start
()
except
:
except
(
KeyboardException
,
SystemExit
)
:
stop
()
stop
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment