""" An outbound application that makes a call to a destination address and says a text string and optionally plays a wav file. The destination address, text string and wav file name are supplied in outbound_parameters as a semi-colon separated list. The wav file name is optional. This application requires that call_from is supplied in application_parameters, which is configured on the outbound service page of the CWP. For information on placing outbound calls please read the online documentation at: https://cloud.aculab.com/documents/web_services_outbound Also have a look at the invoke_outbound_service sample code in the samples_ws directory. Actions: - place an outbound call - play tts - play a wav file - 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 the outbound args, we're expecting "destination_address;text_to_say;file_to_play" my_log.info("Out Args: {0}".format(outbound_parameters)) out_arg_splits = outbound_parameters.split(';') text_to_say = None file_to_play = None destination = out_arg_splits[0] # we expect call_from to be in application_parameters # call_from is used in CLI validation for PSTN calls call_from = application_parameters try: text_to_say = out_arg_splits[1] file_to_play = out_arg_splits[2] except: pass # 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(destination, 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 message if text_to_say is not None: 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 else: my_log.info("No TTS to play") # log at info level # Play message if file_to_play is not None: cause = channel.FilePlayer.play(file_to_play, barge_in=True) 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 else: my_log.info("No File to play") # 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