""" A simple example that will answer an inbound call, record until a DTMF tone is heard, and then play it back. Actions: - check the channel state - ring and answer - record with barge-in. - play the recording - hang up """ import time from prosody.uas import Hangup, Error, AESCBCCipher __uas_version__ = "0.0.1" __uas_identify__ = "application" def main(channel, application_instance_id, file_man, my_log, application_parameters): return do_call(channel, application_instance_id, file_man, my_log, application_parameters) def do_call(channel, application_instance_id, file_man, my_log, application_parameters): try: return_code = 0 # 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") # To encrypt the recorded message, supply the cipher to the record command # In the bit of code below we assume that the cipher key and vector are supplied # in application_parameters, using ; as the delimiter. # my_cipher_key, my_initialisation_vector = application_parameters.split(';') # my_cipher = AESCBCCipher(key=my_cipher_key, vector=my_initialisation_vector) # cause = channel.FileRecorder.record('recorded_message_{0}.wav'.format(application_instance_id), # barge_in=True, cipher=my_cipher) # record a message cause = channel.FileRecorder.record('recorded_message_{0}.wav'.format(application_instance_id), barge_in=True) if cause != channel.FileRecorder.Cause.BARGEIN: raise Error("file record returned {0}: expected {1}".format(cause, channel.FileRecorder.Cause.BARGEIN)) # play recording (if playing an encrypted file, supply the cipher here as well) channel.FilePlayer.play('recorded_message_{0}.wav'.format(application_instance_id)) except Hangup as exc: my_log.info("Hangup exception reports: {0}".format(exc)) return_code = -103 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