Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
piphone-sip
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
4
Issues
4
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
La Quadrature du Net
piphone
piphone-sip
Commits
c7e65d25
Commit
c7e65d25
authored
May 09, 2016
by
okhin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using the ari.py in the main application
parent
b57018d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
48 deletions
+30
-48
app.py
app.py
+30
-48
No files found.
app.py
View file @
c7e65d25
...
...
@@ -16,10 +16,10 @@ from operator import itemgetter
import
jwt
import
websockets
import
requests
from
bottle
import
request
,
abort
,
Bottle
,
JSONPlugin
from
bottle_sqlite
import
SQLitePlugin
from
daemonize
import
Daemonize
import
ari
arg_parser
=
argparse
.
ArgumentParser
(
description
=
'Manage the SIP Backend for the piphone'
)
config
=
configparser
.
ConfigParser
()
...
...
@@ -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.
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
()
bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
config
[
'moh'
][
'name'
],
'holding'
)
except
Exception
as
e
:
phone_logger
.
critical
(
"No Music On Hold (moh) section in config file. Exiting"
)
phone_logger
.
Exception
(
e
)
raise
e
try
:
result
=
bridge
.
create
()
result
=
bridge
.
startMoh
(
self
.
config
[
'moh'
][
'class'
])
except
Exception
as
e
:
phone_logger
.
error
(
"Cannot start 'on hold' bridge"
)
phone_logger
.
exception
(
e
)
...
...
@@ -205,7 +204,7 @@ async def dispatch(self, event):
class
Call
(
object
):
"""
This Class is used to manage operatio
sn
on a call, to print it and dump it.
This Class is used to manage operatio
ns
on a call, to print it and dump it.
"""
history
=
[]
actions
=
{
'Created'
:
'call_caller'
...
...
@@ -276,23 +275,21 @@ class Call(object):
return
# Now, we're connectig the other side
# 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
,))
# 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
payload
[
'channel'
]
=
event
[
'channel'
][
'id'
]
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
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
,))
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
:
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):
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'
]))
# 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
):
# Step 1 create a bridge
payload
=
{}
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
payload
[
'type'
]
=
'mixing'
bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
id
,
'mixing'
)
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
payload
[
'channel'
]
=
","
.
join
([
self
.
id
+
'-'
+
self
.
caller
,
self
.
id
+
'-'
+
self
.
callee
])
channels
=
","
.
join
([
self
.
id
+
'-'
+
self
.
caller
,
self
.
id
+
'-'
+
self
.
callee
])
phone_logger
.
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
)
bridge
.
addChannel
(
channels
)
phone_logger
.
info
(
"Call now fully connected: {} <-> {}"
.
format
(
self
.
caller
,
self
.
callee
))
return
# Call is being picked up, we want to play a song
payload
=
{}
try
:
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
payload
[
'media'
]
=
'sound:mario'
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
,))
# 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
)
logging
.
debug
(
"Now playing a sound on channel {}"
.
format
(
self
.
id
))
channel
.
playback
(
channel
.
name
,
self
.
config
[
'asterisk'
][
'playback'
])
except
Exception
as
e
:
logging
.
exception
(
e
)
raise
e
...
...
@@ -344,28 +333,21 @@ class Call(object):
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
,))
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
moh_bridge
=
ari
.
Bridge
(
self
.
config
[
'asterisk'
],
self
.
config
[
'moh'
][
'name'
],
'holding'
)
try
:
r
=
requests
.
get
(
'http://185.34.33.12:8088/ari/bridges/{}-moh'
.
format
(
payload
[
'app'
]),
data
=
payload
)
r
.
raise_for_status
()
moh_bridge
.
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
payload
=
{}
payload
[
'app'
]
=
'piphone'
payload
[
'api_key'
]
=
':'
.
join
([
login_ari
,
token_ari
])
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
,))
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
:
logging
.
exception
(
e
)
raise
e
...
...
@@ -466,5 +448,5 @@ if __name__ == '__main__':
phone_logger
.
info
(
"Starting the piphone SIP backend"
)
try
:
start
()
except
:
except
(
KeyboardException
,
SystemExit
)
:
stop
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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