""" A simple example that will answer an inbound call and play a wav file. The wav to play is passed in through application_parameters, which is configured on the inbound services page of the CWP. Actions: - check the channel state - ring and answer - play a file - file name in application_parameters - hang up """ from prosody.uas import Hangup, Error, AESCBCCipher import time __uas_version__ = "0.0.1" __uas_identify__ = "application" def main(channel, application_instance_id, file_man, my_log, application_parameters): return_code = 0 try: # This application will play a file. If the file is encrypted it must # be validated before it can be played. Validation should happen before # the call is answered. # In the bit of code below we assume that the cipher key and vector are supplied # in application_parameters, along with the file name (using ; as the delimiter). # my_file_name, my_cipher_key, my_initialisation_vector = application_parameters.split(';') # my_cipher = AESCBCCipher(key=my_cipher_key, vector=my_initialisation_vector) # # if file_man.validate(filename=my_file_name, cipher=my_cipher) is False: # raise Error("The encrypted file was not validated") # check the incoming channel state state = channel.state() if state == channel.State.CALL_INCOMING: state = channel.ring() # this can raise a Hangup exception if state == channel.State.RING_INCOMING: state = channel.answer() # this can raise a Hangup exception else: raise Hangup('No inbound call, state is {0}'.format(state)) if state != channel.State.ANSWERED: raise Hangup('Failed to answer inbound call, state is {0}'.format(state)) my_log.info("Answered an inbound call") # log at info level # If your file to play is encrypted supply the cipher in the play command # cause = channel.FilePlayer.play(filename=my_file_name, cipher=my_cipher) # play a wav file, file name is provided in application_parameters cause = channel.FilePlayer.play(application_parameters) if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Play failed: cause is {0}".format(cause)) # Bye bye cause = channel.FilePlayer.say("Bye bye.") if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say bye bye failed: cause is {0}".format(cause)) except Hangup as exc: my_log.info("Hangup exception reports: {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 reports: {0}".format(exc)) return_code = -101 except Exception as exc: # an unexpected exception, return a negative value my_log.exception("Unexpected exception reports: {0}".format(exc)) return_code = -102 finally: if channel.state() != channel.State.IDLE: channel.hang_up() return return_code