""" An inbound application that answers the call; prompts the caller to record a message (and to press any key to stop recording); plays a beep to indicate that it is recording and then records for a maximum of 60 seconds. The recorded filename is samples/voicemail1/recordedMessageFrom.wav The application will check that the recorded message file exists before exiting. Actions: - check the channel state - ring and answer - use TTS to play a prompt - record a file - check that the file exists - hang up This application is part of the online Voice Mail tutorial. """ from prosody.uas import Hangup, Error __uas_version__ = "0.0.1" __uas_identify__ = "application" def main (channel, application_instance_id, file_man, my_log, application_parameters): return_code = 0 try: # check the incoming channel state and answer the call 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") # use text-to-speech to pass instructions to the caller channel.FilePlayer.say("Please record a message after the tone. Press any digit to stop the recording.") # use DTMF to play the tone channel.DTMFPlayer.play ('#') # use the call_from information from the call details to identify this recording msg_file_name = "samples/voicemail1/recordedMessageFrom{0}.wav".format(channel.Details.call_from) # the recording will stop after three seconds of silence, or after one minute # (we don't want people speaking for ages), or if the caller presses a digit (barge_in). channel.FileRecorder.record(msg_file_name, milliseconds_max_silence=3000, seconds_timeout=60, barge_in=True) # check whether the file has been saved correctly, does it exist? if file_man.exists(msg_file_name): channel.FilePlayer.say("Your message has been saved. Good bye.") else: channel.FilePlayer.say("Your message could not be saved. Good bye.") # catch any Hangup exceptions, this is still regarded as a success but the 100 code # identifies that the caller hung up before the application completed except Hangup as exc: my_log.info ("Hangup exception: {0}.".format(exc)) return_code = 100 # catch any Error exceptions, these are often caused by application errors which can be fixed except Error as exc: my_log.error("Error exception caught: {0}".format(exc)) return_code = -101 # catch any other exceptions, these are almost certainly caused by application errors which should be fixed except Exception as exc: my_log.exception("Unexpected exception caught: {0}".format(exc)) return_code = -102 # now, if the caller has not already hung up, we do finally: if channel.state() != channel.State.IDLE: channel.hang_up() my_log.info("Example completed") return return_code