YAYPM:Bridge and then unbridge
From Yate Documentation
				
								
				(Difference between revisions)
				
																
				
				
								
				| Line 28: | Line 28: | ||
|           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 90: | Line 90: | ||
|                       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 105: | Line 105: | ||
|                           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() | ||
| Line 186: | Line 186: | ||
|   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): | ||
| Line 200: | Line 200: | ||
|   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') | ||
Revision as of 12:53, 1 November 2012
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()
