""" An outbound application that makes a call to a destination address and uses text to speech (TTS) to say a text string. The destination address (call_to), caller id (call_from) and TTS text string are supplied in outbound_parameters as a semi-colon separated list. An optional file to play can be added to the semi-colon separated list. For information on placing outbound calls please read the online web services documentation. For additional information regarding outbound PSTN calls, please see the online documentation. Also have a look at the invoke_outbound_service sample code in the samples_ws directory. Actions: - place an outbound call - play tts - optionally, play wav - hang up """ from prosody.uas import Hangup, Error import time __uas_version__ = "0.0.1" __uas_identify__ = "application" def main(channel, application_instance_id, file_man, my_log, application_parameters, outbound_parameters): return_code = 0 try: # read outbound_parameters, we're expecting "call_to;call_from;text_to_say" my_log.info("Outbound parameters: {0}".format(outbound_parameters)) out_arg_splits = outbound_parameters.split(';') num_args = len(out_arg_splits) if num_args == 3: call_to, call_from, text_to_say = out_arg_splits file_to_play = None elif num_args == 4: call_to, call_from, text_to_say, file_to_play = out_arg_splits else: raise Error("outbound parameters must be 'call_to;call_from;text_to_say'. I got {0}".format(repr(outbound_parameters))) # Place an outbound call. If the destination is BUSY, # wait ten seconds and try again; try for one minute. endTime = time.time() + 60 while channel.call(call_to=call_to, call_from=call_from) != channel.State.ANSWERED: cause = channel.cause() if cause != channel.Cause.BUSY: raise Error("Call destination returned cause {0}".format(cause)) if time.time() > endTime: raise Hangup("Call destination is busy.") time.sleep(10) continue my_log.info("Placed an outbound call") # log at info level # Play a wake-up call TTS message cause = channel.FilePlayer.say(text_to_say) if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say good morning failed: cause is {0}".format(cause)) # log at error level my_log.info("Played TTS") # log at info level # Optionally, Play an audio file. if file_to_play is not None: cause = channel.FilePlayer.play(file_to_play) if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Play message failed: cause is {0}".format(cause)) my_log.info("Played File") # log at info level channel.hang_up() except Hangup as exc: my_log.info("Hangup exception: {0}".format(exc)) # in this app a hangup is not an error, return a positive value return_code = 100 except Error as exc: # for error conditions return a negative value my_log.error("Error exception: {0}".format(exc)) return_code = -101 except Exception as exc: # an unexpected exception, return a negative value my_log.exception("Unexpected exception: {0}".format(exc)) return_code = -102 finally: if channel.state() != channel.State.IDLE: channel.hang_up() return return_code