""" This inbound application answers a call and then loops. The loop prompts the caller to record a message, then plays it back to them and asks whether he is satisfied with it. The recorded filename is samples/voicemail2/recordedMessageFrom.wav. The actions are: - check the channel state - ring and answer - record a file - play the file - save or loop on DTMF - hang up This application is part of the Voice Mail tutorial at https://cloud.aculab.com/documents/voicemail. """ 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 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 while True: cause = channel.FilePlayer.say ("Please record a message after the tone. Press any digit to stop the recording.") if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say please record failed: cause is {0}".format(cause)) msg_file_name = "samples/voicemail2/recordedMessageFrom{0}.wav".format(channel.Details.call_from) cause = channel.DTMFPlayer.play ('#') if cause != channel.DTMFPlayer.Cause.NORMAL: raise Error ("Play # failed: cause is {0}".format (digits, cause)) cause = channel.FileRecorder.record(msg_file_name, milliseconds_max_silence=3000, barge_in=True) if cause != channel.FileRecorder.Cause.SILENCE and cause != channel.FileRecorder.Cause.BARGEIN: raise Error ("Record message failed: cause is {0}".format(cause)) cause = channel.FilePlayer.say ("The following message has been recorded.") if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say message failed: cause is {0}".format(cause)) cause = channel.FilePlayer.play(msg_file_name) if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Play message failed: cause is {0}".format(cause)) # clear DTMF buffer before waiting for a 1 or 2 channel.DTMFDetector.clear_digits() cause = channel.FilePlayer.say ("Please press 1 to keep this message or 2 to record an alternative message") if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say alternatives failed: cause is {0}".format(cause)) dtmf = channel.DTMFDetector.get_digits(count=1, seconds_predigits_timeout=30, seconds_interdigit_timeout=1) cause = channel.DTMFDetector.cause() if cause == channel.DTMFDetector.Cause.COUNT: if dtmf == '1': break elif dtmf == '2': continue else: cause = channel.FilePlayer.say ("A non-specified digit has been pressed.") if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say non-specified failed: cause is {0}".format(cause)) break cause = channel.FilePlayer.say ("Your message has been saved. Good bye.") if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say good bye failed: cause is {0}".format(cause)) except Hangup as exc: my_log.info ("Hangup exception: {0}.".format(exc)) return_code = 100 except Error as exc: my_log.error("Error execption: {0}".format(exc)) return_code = -101 except Exception as exc: my_log.exception("Unexpected exception: {0}".format(exc)) return_code = -102 finally: if channel.state() != channel.State.IDLE: channel.hang_up() my_log.info("Example completed") return return_code