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...") | |||
| 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__()") | ||
| 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") | ||
| 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: | ||
| Line 174: | Line 174: | ||
|                               #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") | ||
| Line 194: | Line 194: | ||
|           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_: | ||
| Line 204: | Line 204: | ||
|       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() | ||
Revision as of 12:50, 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()
