# -*- coding: utf-8 -*-
"""
A simple application that answers an inbound call and speaks some TTS.
The TTS to say is passed in through application_parameters, which is
configured on the inbound services page of the CWP. In addition, this
sample will speak the current date and time.
Actions:
- check the channel state
- ring and answer
- play some tts - provided in application_parameters
- say the date and time
- hang up
"""
from prosody.uas import Hangup, Error
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:
# 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
# Say a TTS prompt, the text is provided in application_parameters.
# This will use the default TTS engine and voice.
# Note that if your application_parameters contains Unicode characters,
# you may need to call an appropriate encode function here
cause = channel.FilePlayer.say(application_parameters)
if cause != channel.FilePlayer.Cause.NORMAL:
raise Error("Say hello failed: cause is {0}".format(cause))
# Now say the date and the time.
# You can use SSML tags to select a different TTS engine and voice.
# The engines and voices available may depend on your account settings.
# SSML is also used to change the way the text is spoken.
# See the online documentation for more information on SSML.
# Here we use SSML tags to specify some text as a date and time.
# Create a date string in the format year-month-day.
date = time.strftime("%Y/%m/%d", time.localtime(time.time()))
# Inform the TTS engine that the string should be spoken as a date.
cause = channel.FilePlayer.say("The date is {0}".format(date))
# Create a time string in the format hours-minutes-seconds
timestr = time.strftime("%H:%M:%S", time.localtime(time.time()))
# Inform the TTS engine that the string should be spoken as a time.
cause = channel.FilePlayer.say("The time is {0}".format(timestr))
if cause != channel.FilePlayer.Cause.NORMAL:
raise Error("TTS player returned {0}: expected {1}".format(cause, channel.FilePlayer.Cause.NORMAL))
# Bye bye, using the default settings.
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