YAYPM:Bridge and then unbridge
From Yate Documentation
(Difference between revisions)
(Created page with "This script is given as an example, don't forget to change some information such as yate's extmodule binding ip address and port. This script will: 1 - make a phone call to...") |
|||
(3 intermediate revisions by one user not shown) | |||
Line 3: | Line 3: | ||
This script will: | This script will: | ||
− | + | * 1 - make a phone call to 'jmfoucher' | |
− | + | * 2 - play a sound to jmfoucher | |
− | + | * 3 - wait for DTMF "1" | |
− | + | * 4 - once "1" is pressed, toto is called | |
− | + | * 5 - when toto answers, toto and jmfoucher are bridged | |
− | + | * 6 - after 3 seconds, toto and jmfoucher will be unbridged (the two new channels are independant) | |
− | + | * 7 - two different sounds are played on the different channels | |
#!/usr/bin/python | #!/usr/bin/python | ||
Line 15: | Line 15: | ||
from yaypm import TCPDispatcherFactory, AbandonedException | from yaypm import TCPDispatcherFactory, AbandonedException | ||
from yaypm.flow import go, getResult | from yaypm.flow import go, getResult | ||
− | + | ||
from yaypm.utils import ConsoleFormatter | from yaypm.utils import ConsoleFormatter | ||
− | + | ||
import logging, yaypm | import logging, yaypm | ||
− | + | ||
logger = logging.getLogger("test3") | logger = logging.getLogger("test3") | ||
− | + | ||
class myTest(object): | class myTest(object): | ||
− | + | ||
def __init__(self, client): | def __init__(self, client): | ||
logger.debug(">>> myTest.__init__()") | logger.debug(">>> myTest.__init__()") | ||
self.client = client | self.client = client | ||
self.run() | self.run() | ||
− | + | ||
def run(self): | def run(self): | ||
logger.debug(">>> myTest.run()") | logger.debug(">>> myTest.run()") | ||
go(self.test()) | go(self.test()) | ||
− | + | ||
− | + | ||
def test(self): | def test(self): | ||
logger.debug(">>> myTest.test()") | logger.debug(">>> myTest.test()") | ||
− | + | ||
firsttime = True | firsttime = True | ||
− | + | ||
yield self.client.installMsgHandler("call.route", prio = 50) | yield self.client.installMsgHandler("call.route", prio = 50) | ||
getResult() | getResult() | ||
Line 45: | Line 45: | ||
yield self.client.installWatchHandler("chan.hangup") | yield self.client.installWatchHandler("chan.hangup") | ||
getResult() | getResult() | ||
− | + | ||
execute = self.client.msg("call.execute", | execute = self.client.msg("call.execute", | ||
{"callto": "dumb/", | {"callto": "dumb/", | ||
Line 63: | Line 63: | ||
"chan.hangup", | "chan.hangup", | ||
lambda m, callid = execute["id"] : m["id"] == callid) | lambda m, callid = execute["id"] : m["id"] == callid) | ||
− | + | ||
yield self.client.onwatch( | yield self.client.onwatch( | ||
"call.answered", | "call.answered", | ||
Line 70: | Line 70: | ||
getResult() | getResult() | ||
logger.debug("Monitor connection answered.") | logger.debug("Monitor connection answered.") | ||
− | + | ||
dumbid = execute["id"] | dumbid = execute["id"] | ||
monitorid = execute["targetid"] | monitorid = execute["targetid"] | ||
callid = execute["id"] | callid = execute["id"] | ||
logger.debug("callid = s" % (callid, monitorid)) | logger.debug("callid = s" % (callid, monitorid)) | ||
− | + | ||
logger.debug("on joue le son") | logger.debug("on joue le son") | ||
− | + | ||
self.client.msg("chan.masquerade", | self.client.msg("chan.masquerade", | ||
{"message" : "chan.attach", | {"message" : "chan.attach", | ||
Line 83: | Line 83: | ||
"id": callid, | "id": callid, | ||
"source": "wave/play/../../sounds/wp_star_seller_ask.wav"}).enqueue() | "source": "wave/play/../../sounds/wp_star_seller_ask.wav"}).enqueue() | ||
− | + | ||
logger.debug("le son est parti") | logger.debug("le son est parti") | ||
− | + | ||
− | + | ||
while True: | while True: | ||
− | + | ||
logger.debug("dans le while True") | logger.debug("dans le while True") | ||
− | + | ||
yield self.client.onmsg( | yield self.client.onmsg( | ||
"chan.dtmf", | "chan.dtmf", | ||
Line 97: | Line 97: | ||
end) | end) | ||
dtmf = getResult() | dtmf = getResult() | ||
− | + | ||
logger.debug("Dtmf dtmf["text"]) | logger.debug("Dtmf dtmf["text"]) | ||
− | + | ||
dtmf.ret(True) | dtmf.ret(True) | ||
− | + | ||
if dtmf["text"] == "1": | if dtmf["text"] == "1": | ||
if firsttime: | if firsttime: | ||
firsttime = False | firsttime = False | ||
− | + | ||
self.client.msg("chan.masquerade", | self.client.msg("chan.masquerade", | ||
{"message" : "chan.attach", | {"message" : "chan.attach", | ||
"id": callid, | "id": callid, | ||
"source": "wave/play/../../sounds/wp_star_seller_wait.wav"}).enqueue() | "source": "wave/play/../../sounds/wp_star_seller_wait.wav"}).enqueue() | ||
− | + | ||
− | + | ||
execute = self.client.msg("call.execute", | execute = self.client.msg("call.execute", | ||
{"callto": "dumb/", | {"callto": "dumb/", | ||
Line 125: | Line 125: | ||
logger.debug("callid=s" % (callid, monitorid)) | logger.debug("callid=s" % (callid, monitorid)) | ||
logger.debug("callid2=s" % (callid2, targetid2)) | logger.debug("callid2=s" % (callid2, targetid2)) | ||
− | + | ||
− | + | ||
end = self.client.onmsg("chan.hangup", | end = self.client.onmsg("chan.hangup", | ||
lambda m, callid = targetid2 : m["id"] == callid, autoreturn = True) | lambda m, callid = targetid2 : m["id"] == callid, autoreturn = True) | ||
− | + | ||
yield self.client.onwatch("call.answered", | yield self.client.onwatch("call.answered", | ||
lambda m : m["targetid"] == callid2, until = end) | lambda m : m["targetid"] == callid2, until = end) | ||
− | + | ||
answered = getResult() | answered = getResult() | ||
logger.debug("answered= answered) | logger.debug("answered= answered) | ||
− | + | ||
logger.debug("on appelle self.conf") | logger.debug("on appelle self.conf") | ||
logger.debug("callid = s" % (callid, callid2)) | logger.debug("callid = s" % (callid, callid2)) | ||
− | + | ||
connect = self.client.msg( | connect = self.client.msg( | ||
"chan.connect", | "chan.connect", | ||
Line 144: | Line 144: | ||
"targetid": targetid2}) | "targetid": targetid2}) | ||
yield connect.dispatch() | yield connect.dispatch() | ||
− | + | ||
if getResult(): | if getResult(): | ||
logger.debug("connect SUCCESS") | logger.debug("connect SUCCESS") | ||
else: | else: | ||
logger.debug("connect FAILURE") | logger.debug("connect FAILURE") | ||
− | + | ||
def sound(): | def sound(): | ||
go(sound2()) | go(sound2()) | ||
− | + | ||
def sound2(): | def sound2(): | ||
logger.debug("dans sound") | logger.debug("dans sound") | ||
− | + | ||
transfer = self.client.msg("chan.masquerade", | transfer = self.client.msg("chan.masquerade", | ||
{"message": "call.transfer", | {"message": "call.transfer", | ||
Line 167: | Line 167: | ||
else: | else: | ||
logger.debug("transfer success") | logger.debug("transfer success") | ||
− | + | ||
− | + | ||
reactor.callLater(3, sound) | reactor.callLater(3, sound) | ||
− | + | ||
− | + | ||
#yield yate.msg("call.drop", {"id": targetid}).dispatch() | #yield yate.msg("call.drop", {"id": targetid}).dispatch() | ||
#getResult() | #getResult() | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
except AbandonedException, e: | except AbandonedException, e: | ||
logger.debug("Call callid) | logger.debug("Call callid) | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
def start(host, port): | def start(host, port): | ||
logger.debug("dans start") | logger.debug("dans start") | ||
− | + | ||
local_addr = remote_addr = host | local_addr = remote_addr = host | ||
local_port = remote_port = port | local_port = remote_port = port | ||
− | + | ||
#go(route(yate)) | #go(route(yate)) | ||
def start_client(client_yate): | def start_client(client_yate): | ||
logger.debug("client started"); | logger.debug("client started"); | ||
myTest(client_yate) | myTest(client_yate) | ||
− | + | ||
client_factory = TCPDispatcherFactory(start_client) | client_factory = TCPDispatcherFactory(start_client) | ||
− | + | ||
reactor.connectTCP(local_addr, local_port, client_factory) | reactor.connectTCP(local_addr, local_port, client_factory) | ||
− | + | ||
if __name__ == _main_: | if __name__ == _main_: | ||
− | + | ||
hdlr = logging.StreamHandler() | hdlr = logging.StreamHandler() | ||
formatter = ConsoleFormatter('%(name)s %(levelname)s %(message)s') | formatter = ConsoleFormatter('%(name)s %(levelname)s %(message)s') | ||
hdlr.setFormatter(formatter) | hdlr.setFormatter(formatter) | ||
− | + | ||
yaypm.logger.addHandler(hdlr) | yaypm.logger.addHandler(hdlr) | ||
yaypm.logger.setLevel(logging.DEBUG) | yaypm.logger.setLevel(logging.DEBUG) | ||
Line 210: | Line 210: | ||
yaypm.logger_messages.setLevel(logging.DEBUG) | yaypm.logger_messages.setLevel(logging.DEBUG) | ||
yaypm.logger_messages.setLevel(logging.INFO) | yaypm.logger_messages.setLevel(logging.INFO) | ||
− | + | ||
logger.setLevel(logging.DEBUG) | logger.setLevel(logging.DEBUG) | ||
logger.addHandler(hdlr) | logger.addHandler(hdlr) | ||
− | + | ||
#f = TCPDispatcherFactory(start) | #f = TCPDispatcherFactory(start) | ||
#reactor.connectTCP("192.168.4.87", 5039, f) | #reactor.connectTCP("192.168.4.87", 5039, f) | ||
start("192.168.4.87", 5039) | start("192.168.4.87", 5039) | ||
reactor.run() | reactor.run() | ||
+ | |||
+ | '''See also''' | ||
+ | |||
+ | * [[YAYPM:Bridge and then redirect after a hangup]] | ||
+ | |||
+ | [[Category:YAYPM]] [[Category:Scripting]] [[Category:Extmodule]] |
Latest revision as of 14:04, 4 November 2013
This script is given as an example, don't forget to change some information such as yate's extmodule binding ip address and port.
This script will:
- 1 - make a phone call to 'jmfoucher'
- 2 - play a sound to jmfoucher
- 3 - wait for DTMF "1"
- 4 - once "1" is pressed, toto is called
- 5 - when toto answers, toto and jmfoucher are bridged
- 6 - after 3 seconds, toto and jmfoucher will be unbridged (the two new channels are independant)
- 7 - two different sounds are played on the different channels
#!/usr/bin/python from twisted.internet import reactor, defer from yaypm import TCPDispatcherFactory, AbandonedException from yaypm.flow import go, getResult from yaypm.utils import ConsoleFormatter import logging, yaypm logger = logging.getLogger("test3") class myTest(object): def __init__(self, client): logger.debug(">>> myTest.__init__()") self.client = client self.run() def run(self): logger.debug(">>> myTest.run()") go(self.test()) def test(self): logger.debug(">>> myTest.test()") firsttime = True yield self.client.installMsgHandler("call.route", prio = 50) getResult() yield self.client.installWatchHandler("call.answered") getResult() yield self.client.installWatchHandler("chan.hangup") getResult() execute = self.client.msg("call.execute", {"callto": "dumb/", "caller": "appelant", "target": "jmfoucher", "sip_prout": "value", "osip_toto": "tata" }) yield execute.dispatch() if not getResult(): logger.warn("can't create monitor connection on jmfoucher") monitor = None else: logger.debug("can monitor jmfoucher") try: end = self.client.onwatch( "chan.hangup", lambda m, callid = execute["id"] : m["id"] == callid) yield self.client.onwatch( "call.answered", lambda m : m["id"] == execute["targetid"], until = end) getResult() logger.debug("Monitor connection answered.") dumbid = execute["id"] monitorid = execute["targetid"] callid = execute["id"] logger.debug("callid = s" % (callid, monitorid)) logger.debug("on joue le son") self.client.msg("chan.masquerade", {"message" : "chan.attach", #"id": monitorid, "id": callid, "source": "wave/play/../../sounds/wp_star_seller_ask.wav"}).enqueue() logger.debug("le son est parti") while True: logger.debug("dans le while True") yield self.client.onmsg( "chan.dtmf", #lambda m : m["id"] == callid, lambda m : m["id"] == monitorid, end) dtmf = getResult() logger.debug("Dtmf dtmf["text"]) dtmf.ret(True) if dtmf["text"] == "1": if firsttime: firsttime = False self.client.msg("chan.masquerade", {"message" : "chan.attach", "id": callid, "source": "wave/play/../../sounds/wp_star_seller_wait.wav"}).enqueue() execute = self.client.msg("call.execute", {"callto": "dumb/", "caller": "jmfoucher", "target": 'toto'}) yield execute.dispatch() if not getResult(): logger.debug("FAILURE") else: logger.debug("SUCCESS") callid2 = execute["id"] targetid2 = execute["targetid"] logger.debug("callid=s" % (callid, monitorid)) logger.debug("callid2=s" % (callid2, targetid2)) end = self.client.onmsg("chan.hangup", lambda m, callid = targetid2 : m["id"] == callid, autoreturn = True) yield self.client.onwatch("call.answered", lambda m : m["targetid"] == callid2, until = end) answered = getResult() logger.debug("answered= answered) logger.debug("on appelle self.conf") logger.debug("callid = s" % (callid, callid2)) connect = self.client.msg( "chan.connect", {"id": monitorid, "targetid": targetid2}) yield connect.dispatch() if getResult(): logger.debug("connect SUCCESS") else: logger.debug("connect FAILURE") def sound(): go(sound2()) def sound2(): logger.debug("dans sound") transfer = self.client.msg("chan.masquerade", {"message": "call.transfer", "id": monitorid, "to": "wave/play/../../sounds/accueil_court.wav", "peerto": "wave/play/../../sounds/wp_star_seller_wait.wav" }) yield transfer.dispatch() if not getResult(): logger.debug("transfer failure") else: logger.debug("transfer success") reactor.callLater(3, sound) #yield yate.msg("call.drop", {"id": targetid}).dispatch() #getResult() except AbandonedException, e: logger.debug("Call callid) def start(host, port): logger.debug("dans start") local_addr = remote_addr = host local_port = remote_port = port #go(route(yate)) def start_client(client_yate): logger.debug("client started"); myTest(client_yate) client_factory = TCPDispatcherFactory(start_client) reactor.connectTCP(local_addr, local_port, client_factory) if __name__ == _main_: hdlr = logging.StreamHandler() formatter = ConsoleFormatter('%(name)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) yaypm.logger.addHandler(hdlr) yaypm.logger.setLevel(logging.DEBUG) #yaypm.flow.logger_flow.setLevel(logging.DEBUG) yaypm.logger_messages.setLevel(logging.DEBUG) yaypm.logger_messages.setLevel(logging.INFO) logger.setLevel(logging.DEBUG) logger.addHandler(hdlr) #f = TCPDispatcherFactory(start) #reactor.connectTCP("192.168.4.87", 5039, f) start("192.168.4.87", 5039) reactor.run()
See also