__uas_identify__ = "application" __uas_version__ = "1.0b1" from prosody.uas import Hangup, Error from prosody.uas.highlevel import HighLevelCallChannel """ An inbound application that prompts the user to select a target to be connected to. The target names (to be read out) and addresses (to be connected 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 connect options menu - speak the options using TTS - listen for the caller's selection using DTMF - connect the call """ def connect_menu(application_parameters): # Split the application_parameters up into connect 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, connect_list, my_log): channel.DTMFDetector.clear_digits() my_log.info("connect_list: {0}".format(connect_list)) while True: key = 0 for key in range(len(connect_list)): msg = "Press {0} to {1}".format(key, connect_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 connect_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("Connect 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.") # Convert the application_parameters into a connect menu list for later use connect_list = connect_menu(application_parameters) if connect_list == []: raise Error("Invalid connect menu") # Prompt for digit channel.FilePlayer.say("Hello. ") target = get_menu_selection(channel, connect_list, my_log) # Connect to selected target channel.FilePlayer.say("Connecting to {0}".format(target[0])) high_level_channel = HighLevelCallChannel(channel, my_log) if high_level_channel.call_and_connect(other_call=out_channel, call_to=target[1]) is False: my_log.error("Connect to {0} failed.".format(target[1])) channel.FilePlayer.say("I wasn't able to connect you. Please try again later.") channel.hang_up() else: my_log.info ("Connect to conference {0} succeeded. Waiting for idle.".format (digits)) 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 = -102 except Exception as exc: my_log.exception("Got unexpected exception: {0}".format(exc)) return_code = -101 finally: if channel.state() != channel.State.IDLE: channel.hang_up() return return_code