""" 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 online Voice Mail tutorial. """ from prosody.uas import Hangup, Error, ICanRun __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 and answer the incoming call state = channel.state() if state == channel.State.CALL_INCOMING: state = channel.ring() if state == channel.State.RING_INCOMING: state = channel.answer() 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") my_log.debug("Call from: {0}".format(channel.Details.call_from)) # The message recording is now done in a loop # wait on i_can_run to prevent an accidental infinite loop i_can_run = ICanRun(channel) while i_can_run: channel.FilePlayer.say("Please record a message after the tone. Press any digit to stop the recording.") channel.DTMFPlayer.play('#') msg_file_name = "samples/voicemail2/recordedMessageFrom{0}.wav".format(channel.Details.call_from) 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)) # check whether the file has been saved correctly, does it exist? if not file_man.exists(msg_file_name): channel.FilePlayer.say("Your message could not be saved. Try again.") continue channel.FilePlayer.say("The following message has been recorded.") channel.FilePlayer.play(msg_file_name) # clear the DTMF buffer channel.DTMFDetector.clear_digits() digits = '' while i_can_run: # ask the caller to indicate whether he is happy with the message, press 1 or 2 channel.FilePlayer.say("Please press 1 to keep this message or 2 to record an alternative message") # wait for a 1 or a 2 to be pressed (detection will end on a 1 or 2) digits = channel.DTMFDetector.get_digits(end='12', seconds_predigits_timeout=30) # an END cause means that either a 1 or 2 was pressed if channel.DTMFDetector.cause() == channel.DTMFDetector.Cause.END: break # check the last digit recognised; 1 breaks the loop, 2 asks for another message if digits[-1] == '1': break elif digits[-1] == '2': continue channel.FilePlayer.say("Your message has been saved. Good bye.") except Hangup as exc: my_log.info("Hangup exception: {0}.".format(exc)) return_code = 100 except Error as exc: my_log.error("Error exception: {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