# vim: tabstop=4 softtabstop=4 shiftwidth=4 textwidth=80 smarttab expandtab """ WARNING: only the main call path is tested, refer_to, qa_testphone, Alert-Info or any other of the customizations are untested but, if at all, should require only a tweak here and there ... ideally you can start now wrapping around freeswitch.Session data type to create a nice class that does not require session.getVariable() session.execute() and such and create a better API for executing APIs and Applications, retrieving variables, etc You have to call this script from the XML dialplan using: The FreeSWITCH python app looks for scripts in site-packages for our python2.7 interpreter and in /usr/local/nsc/conf/scripts as well Note the scripts/ folder is messed up now in NSC, someone decided to start throwing shit there when that folder was supposed to contain only scripts that freeswitch invokes """ import freeswitch import socket def handler(session, args): freeswitch.consoleLog('crit', 'Routing session in python: {}\n'.format(type(session).__name__)) dest = session.getVariable('sip_req_uri') # default destination refer_to = session.getVariable('sip_refer_to') profile = session.getVariable('sofia_profile_name') session.execute('info') # If there's refer information, route there ... if refer_to: freeswitch.consoleLog('crit', 'Routing to refer destination {}\n'.format(refer_to)) session.execute('bridge', 'sofia/{}/{}'.format(profile, refer_to)) return # Set a specific outbound Call-ID header if requested by the inbound leg outbound_cid = session.getVariable('sip_h_X-qa-outbound-cid') if outbound_cid: session.execute('export', 'nolocal:sip_h_Call-ID={}'.format(refer_to)) # Use a diferent destination if requested outbound_cb_did = session.getVariable('sip_h_X-qa-callback-did') if outbound_cb_did: dest = outbound_cb_did # Use a diferent destination if requested mh_alert = session.getVariable('sip_mh_Alert-Info') if mh_alert: dest = outbound_cb_did session.execute('export', 'nolocal:alert_info-{}'.format(mh_alert)) # Special destination for contacting a local phone registered in the SBC # IMO this should be re-arranged for something more flexible like # passing a SIP R-URI parameter 'qa_testphone' that indicates the # destination user is registered in the SBC which allows calling # different users registered on the SBC at any given point if dest == 'qa_testphone': hostname = socket.gethostname() local_ipv4 = session.getVariable('local_ip_v4') api = freeswitch.API() c1 = api.executeString('sofia_contact qa_testphone@{}.sangoma.local'.format(hostname)) c2 = api.executeString('sofia_contact qa_testphone@{}'.format(local_ipv4)) session.execute('bridge', 'sofia/{}/{}'.format(profile, dest)) return if dest: session.execute('bridge', 'sofia/{}/{}'.format(profile, dest)) else: freeswitch.consoleLog('crit', 'No destination! WTF!\n') session.execute('respond', '500 WTF')