__uas_identify__ = "application" __uas_version__ = "1.0b1" from prosody.uas import Hangup, Error, ToneManager """ An inbound application that prompts the user to select a target to be transfered to. The target names (to be read out) and addresses (to be transfered to) are given as application parameters during service registration on the inbound service registration page of the CWP. The target addresses are other inbound services that have been registered on the inbound services page of the CWP. This example requires that application_parameters is a semicolon delimited list in the format: ,;, for example: join a conference,sip:AddToConfService@sip-0-2-0.aculab.com; listen to music,sip:InBoundMusic@sip-0-2-0.aculab.com This application requires 1 extra channel. Actions: - wait for anbound call - generate the transfer options menu - speak the options using TTS - listen for the caller's selection using DTMF - transfer the call """ def transfer_menu(application_parameters): # Split the application_parameters up into transfer options # (split with semicolon), and then again with commas. Return the # data as a list of tuples try: return [tuple(a.split(',')) for a in application_parameters.split(';')] except: return [] def get_menu_selection(channel, transfer_list, my_log): channel.DTMFDetector.clear_digits() my_log.info("transfer_list: {0}".format(transfer_list)) while True: key = 0 for key in range(len(transfer_list)): msg = "Press {0} to {1}".format(key, transfer_list[key][0]) my_log.info("key: {0}; Message {1}".format(key, msg)) cause = channel.FilePlayer.say(msg, barge_in=True) if cause == channel.FilePlayer.Cause.BARGEIN: break digits = channel.DTMFDetector.get_digits(count=1, seconds_predigits_timeout=10) try: return transfer_list[int(digits[0])] except: channel.FilePlayer.say("Invalid selection. Try again.") def main(channel, application_instance_id, file_man, my_log, application_parameters): my_log.info("Transfer menu started") return_code = 0 try: channel.ring(1) channel.answer() my_log.info("Call answered") try: out_channel = channel.ExtraChannel[0] except: raise Error("You need to register an extra channel for this application.") # Create a tone-manager tone_manager = ToneManager(my_log) channel.create_tone_player(tone_manager) # Convert the application_parameters into a transfer menu list for later use transfer_list = transfer_menu(application_parameters) if transfer_list == []: raise Error("Invalid transfer menu") # Prompt for digit channel.FilePlayer.say("Hello. ") target = get_menu_selection(channel, transfer_list, my_log) # Transfer to selected target channel.FilePlayer.say("Transfering to {0}".format(target[0])) status = channel.transfer_to_inbound_service(out_channel, call_to=target[1], seconds_timeout = 120) if status is False: channel.FilePlayer.say("I wasn't able to transfer you. Please try again later.") channel.hang_up() channel.wait_for_idle() except Hangup as exc: my_log.info("Got Hangup") return_code = 100 except Error as exc: my_log.error("Got Error: {0}".format(exc)) return_code = -101 except Exception as exc: my_log.exception("Got unexpected exception: {0}".format(exc)) return_code = -100 finally: if channel.state() != channel.State.IDLE: channel.hang_up() return return_code