""" An inbound application that prompts the caller to enter a US Zipcode and then reads the weather for that location to the caller. If you are running the UAS as a Windows service, you may need to add the path to your local Python distribution before attempting to upload this sample to your UAS. For example, add sys.path.append(os.path.abspath('C:/python26/lib')) before import xml.dom.minidom See the troubleshooting guide for more information. """ __uas_identify__ = "application" __uas_version__ = "1.0b3" import httplib import sys, os import xml.dom.minidom from prosody.uas import Hangup, Error def get_US_weather(zip_code): query = "/forecastrss?p={0}&u=f".format(zip_code) http_connect = httplib.HTTPConnection ("weather.yahooapis.com") http_connect.request("GET", query) response = http_connect.getresponse() weather_text = [] if(response.status == 200): data = response.read() xml_dom = xml.dom.minidom.parseString(data) title = xml_dom.getElementsByTagName("yweather:location") if title.length == 1: weather_text.append("The weather for {0}, {1} ".format(title[0].getAttribute("city"), title[0].getAttribute("region"))) else: weather_text.append("The weather ") weather = xml_dom.getElementsByTagName("yweather:condition"); if weather.length == 1: weather_text.append(", on {0}, is generally {1}, {2} degrees Fahrenheit.".format(weather[0].getAttribute("date"), weather[0].getAttribute("text"), weather[0].getAttribute("temp"))) else: weather_text.append("is a mystery to me!") return ''.join(weather_text) http_connect.close() def main(channel, application_instance_id, file_man, my_log, application_parameters): my_log.info ("Get US weather started") dtmf = "" try: channel.ring(2) channel.answer() cause = channel.FilePlayer.say("Hello, please enter your zipcode, followed by the hash key.", barge_in=True) if cause != channel.FilePlayer.Cause.NORMAL and cause != channel.FilePlayer.Cause.BARGEIN: raise Error("Say Hello failed: cause is {0}".format(cause)) while True: dtmf = channel.DTMFDetector.get_digits(end='#*', seconds_predigits_timeout=30) cause = channel.DTMFDetector.cause() if cause == channel.DTMFDetector.Cause.END: dtmf = dtmf[:-1] # check results if len(dtmf) == 5: break if dtmf == '': dtmf = "90212" # Beverly Hills, CA break channel.DTMFDetector.clear_digits() cause = channel.FilePlayer.say("I think that 5 digits are required, please try again.", barge_in=True) if cause != channel.FilePlayer.Cause.NORMAL and cause != channel.FilePlayer.Cause.BARGEIN: raise Error("Say I think failed: cause is {0}".format(cause)) my_log.info ("Zip code entered is {0}".format(dtmf)) response = get_US_weather(dtmf) cause = channel.FilePlayer.say ("{0}. Thank you for calling, goodbye".format(response)) if cause != channel.FilePlayer.Cause.NORMAL: raise Error("Say weather failed: cause is {0}".format(cause)) channel.hang_up() my_log.info("Get weather completed") return 0 except Hangup as exc: my_log.info("Get weather completed with Hangup") except Error as exc: my_log.error("Get weather completed with Error exception! {0}".format(exc)) return -101 except Exception as exc: my_log.exception("Get weather completed with exception! {0}".format(exc)) return -102 finally: if channel.state() != channel.State.IDLE: channel.hang_up()