By using the Aculab site, you agree with our use of cookies.
right  Talk To Us!

UAS API - Inbound Play Wav Sample Application

  • Filename:

    samples/inbound_play_wav.py

    Description:

    This sample will answer an inbound call and play a wav file. It requires that the wav file to play has been uploaded to the cloud, this can be done on the Media File Management page (Manage -> Media Files). The name of the wav file is passed to the application via the application_parameters variable which is configured on the Inbound Services page (Manage -> Inbound Services).

    After ringing and answering the call, the channel.FilePlayer.play() function is used to play the wav file. The name of the wav file is taken from application_parameters. This will halt our application until the wave. This function will block until the file has finished playing - it is possible to start playing a file and then continue with the application by using the channel.FilePlayer.start() function instead.

    Code:

    """
    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).
    
            # 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
    
            # 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