""" An inbound application that is provided as the default Developer Service Target Entry. It rings the call for 4 seconds, answers the call, then reads out the decoded pincode and username from the called address before hanging up. """ __uas_identify__ = "application" __uas_version__ = "1.0b3" import sys, os from prosody.uas import Hangup, Error SIP_PREFIX = "sip:" PINCODE_PREFIX = "--" PINCODE_LEN = 8 # Speak the pin code as separate digits. def get_tts_str_from_pincode(pincode): ttsstr = ', '.join(list(pincode)) return ttsstr # Get suitable string for TTS based on SIP username in supplied call_to. # The SIP username must start with "sip:". # If next characters are "--" and the remaining 8 digits are numeric then read as a pin code. def get_tts_str_from_username(my_log, log_pref, call_to): # check we have a sane call_to splat = call_to.split("@") if len(splat) != 2: return "an unknown SIP username" if not splat[0].startswith(SIP_PREFIX): return "an unknown SIP username" username = splat[0][len(SIP_PREFIX):] # if we have a pin code, try to speak it nicely if username.startswith(PINCODE_PREFIX): pincode = username[len(PINCODE_PREFIX):] try: int(pincode) except ValueError: pincode = "" if len(pincode) == PINCODE_LEN: my_log.info("{0} username [{1}] pincode [{2}]".format(log_pref, username, pincode)) tts_str = get_tts_str_from_pincode(pincode) return "user pin code{0}".format(tts_str) # otherwise, just return the username my_log.info("{0} username [{1}]".format(log_pref, username)) return "SIP username {0}".format(username) def main(channel, application_instance_id, file_man, my_log, application_parameters): return_code = 0 log_pref = "quickstart:" call_to = channel.Details.call_to my_log.info("{0} started with call_to [{1}]".format(log_pref, call_to)) tts_str = get_tts_str_from_username(my_log, log_pref, call_to) try: channel.ring(2) channel.answer() channel.FilePlayer.say("Hello, this is the quick start application for {0}. Have a good day. Goodbye.".format(tts_str)) channel.hang_up() except Hangup as exc: my_log.info("{0} completed with Hangup".format(log_pref)) except Error as exc: my_log.exception("{0} completed with Error exception! {1}".format(log_pref, exc)) return_code = -101 except Exception as exc: my_log.exception("{0} completed with exception! {1}".format(log_pref, exc)) return_code = -102 my_log.info("{0} completed".format(log_pref)) return return_code