[ previous ] [ next ] [ threads ]
 To :  yate@v...
 From :  Maciek Kaminski <maciejka@t...>
 Subject :  for l-fy and markit: poor man's pbx
 Date :  Wed, 06 Jul 2005 02:28:52 +0200
That's for l-fy and markit.

A prototype. 92 lines in two hours. One * or # does blind transfer, two 
* or # supervised. When supervising call press # or * to connect 
patries. Needs polishing. You need rmodule, transfer, moh, yaypm.

mk




#!/usr/bin/python
# -*- coding: iso-8859-2; -*-

"""
 pbx.py

 Copyright (C) 2005 Maciek Kaminski

 Poor man's pbx.

"""

from yaypm import YateClientProtocolFactory, Message, AbandonedException, logger, logger_messages, examples
from yaypm.flow import sleep, build_start, go, logger_flow, getResult
from twisted.internet import reactor, defer

import sys, logging

def pbx(yate, execute):

    callid = execute.attrs["id"]
    targetid = execute.attrs["targetid"]
    
    end = yate.onmsg("chan.hangup", lambda m : m.attrs["id"] == callid, autoreturn = True)

    logger.info("pbx started: %s -> %s" % (callid, targetid))

    yield yate.onmsg("chan.dtmf", lambda m : m.attrs["id"] == targetid and m.attrs["text"] in ["*", "#"], end, autoreturn = True)
    dtmf = getResult()

    logger.debug("got %s from: %s" % (dtmf.attrs["text"], targetid))

    yield yate.onmsg("chan.dtmf", lambda m : m.attrs["id"] == targetid, end, autoreturn = True)
    dtmf = getResult()

    logger.debug("got %s from: %s" % (dtmf.attrs["text"], targetid))

    if(dtmf.attrs["text"] in ["*", "#"]):
        #supervised
        yield yate.onmsg("chan.dtmf", lambda m : m.attrs["id"] == targetid and not m.attrs["text"] in ["*", "#"], end, autoreturn = True)
        dtmf = getResult()

        yield yate.dispatch(Message(name = "call.route", attrs = {"id": callid, "called": dtmf.attrs["text"]}))
        route = getResult()

        logger.debug("doing supervised transfer to: %s" % route.retvalue)

        yate.dispatch(Message(
                 name = "call.transfer",
                attrs = {"userData": targetid, "transferto1": route.retvalue, "transferto2": "moh/default"}))

        yield yate.onwatch("call.answered", lambda m : m.attrs["targetid"] == targetid , end)
        answered = getResult()

        logger.debug("called: %s" % answered.attrs["id"])

        yield yate.onmsg("chan.dtmf", lambda m : m.attrs["id"] == targetid and m.attrs["text"] in ["*", "#"], end, autoreturn = True)
        dtmf = getResult()

        logger.debug("connecting: %s -> %s" % (targetid, answered.attrs["id"]))

        yield yate.dispatch(Message(
                     name = "call.connect",
                    attrs = {"id1": callid, "id2": answered.attrs["id"]}))
            
    else:
        #blind transfer
        logger.debug("doing blind transfer to %s" % dtmf.attrs["text"])        

        yield yate.dispatch(Message(name = "call.route", attrs = {"id": callid, "called": dtmf.attrs["text"]}))
        route = getResult()

        logger.debug("doing blind transfer to: %s" % route.retvalue)

        yield yate.dispatch(Message(name = "call.execute", attrs = {"userData": callid, "id": callid, "callto": route.retvalue}))
        getResult()


if __name__ == '__main__':
       hdlr = logging.StreamHandler()
       #formatter = logging.Formatter()
       formatter = examples.ConsoleFormater('%(name)s %(levelname)s %(message)s')
       hdlr.setFormatter(formatter)

       logger.setLevel(logging.DEBUG)
       logger.addHandler(hdlr)

       start = build_start(examples.route, "pbx", "iax/kamyk@1...", pbx)

       f = YateClientProtocolFactory(start)       
       reactor.connectTCP("localhost", 5038, f)
       reactor.run()