// increments the noinput failure counter from the ZIP code input // block, used to differentiate the retry prompts Variables.NoInputFailures++; // increments the nomatch failure counter from the ZIP code input // block, used to differentiate the retry prompts Variables.NoMatchFailures++; // this is the heart of the application - query the google URL with the entered Zip code, parse the results // using some built-in XML utilities, and then set local variables appropriately. This code sample includes a // couple of local functions (node2xml and dom2xml), used by the main routine. function node2xml(node) { var rv = new Object; for (var child = node.firstChild; child; child = child.nextSibling) { if (child.tagName == 'forecast_conditions') { if (typeof(rv[child.tagName]) == 'undefined') rv[child.tagName] = new Array; rv[child.tagName].push(node2xml(child)); } else rv[child.tagName] = node2xml(child); } rv.data = node.getAttribute('data'); return rv; } function dom2xml(doc) { return node2xml(doc.getDocumentElement()); } // the main function starts here Variables.ZIP = ('' + Variables.ZIP).replace(/\s+/g, ''); Log.info(Variables.ZIP); var xml; { var uri = new java.net.URL('http://www.google.com/ig/api?weather=' + Variables.ZIP); var uriConn = uri.openConnection(); var inp = new java.io.DataInputStream(uriConn.getInputStream()); var factory = Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance(); var builder = factory.newDocumentBuilder(); var xmldom = builder.parse(inp); xml = dom2xml(xmldom).weather; } Variables.City = xml.forecast_information.city.data; Variables.Today_CityFile = ''; if (Variables.City == 'Atlanta, GA') { Variables.Today_CityFile = 'atlanta_ga.vox' }; if (Variables.City == 'Boston, MA') { Variables.Today_CityFile = 'boston_ma.vox' }; if (Variables.City == 'Chicago, IL') { Variables.Today_CityFile = 'chicago_il.vox' }; if (Variables.City == 'Dallas, TX') { Variables.Today_CityFile = 'dallas_tx.vox' }; if (Variables.City == 'Los Angeles, CA') { Variables.Today_CityFile = 'los_angeles_ca.vox' }; if (Variables.City == 'Miami, FL') { Variables.Today_CityFile = 'miami_fl.vox' }; if (Variables.City == 'New York, NY') { Variables.Today_CityFile = 'new_york_ny.vox' }; if (Variables.City == 'San Francisco, CA') { Variables.Today_CityFile = 'san_fran_ca.vox' }; if (Variables.City == 'Seattle, WA') { Variables.Today_CityFile = 'seattle_wa.vox' }; if (Variables.City == 'Washington, DC') { Variables.Today_CityFile = 'wash_dc.vox' }; Variables.Today_Temperature = xml.current_conditions.temp_f.data; // process today's weather { var weather = ''; var condition = xml.current_conditions.condition; if(condition.data == 'Clear') { weather = 'clear'; } if(condition.data == 'Chance of Storm') { weather = 'chance_of_storms'; } if(condition.data == 'Mostly Cloudy') { weather = 'mostly_cloudy'; } if(condition.data == 'Mostly Sunny') { weather = 'mostly_sunny'; } if(condition.data == 'Showers') { weather = 'showers'; } if(condition.data == 'Sunny') { weather = 'sunny'; } if(condition.data == 'Thunderstorm') { weather = 'thunderstorms'; } if (weather != '') { weather = 'present_' + weather + '.vox';} Variables.Today_WeatherFile = weather; Log.info('Variables.Today_WeatherFile = ' + Variables.Today_WeatherFile); } // gather the future forecast information var iForecastIndex = 0; for each (var forecast in xml.forecast_conditions) { if (forecast.day_of_week.data != 'Today') { var day; if(forecast.day_of_week.data == 'Mon') { day = 'monday'; } if(forecast.day_of_week.data == 'Tue') { day = 'tuesday'; } if(forecast.day_of_week.data == 'Wed') { day = 'wednesday'; } if(forecast.day_of_week.data == 'Thu') { day = 'thursday'; } if(forecast.day_of_week.data == 'Fri') { day = 'friday'; } if(forecast.day_of_week.data == 'Sat') { day = 'saturday'; } if(forecast.day_of_week.data == 'Sun') { day = 'sunday'; } Variables['Forecast' + iForecastIndex + '_DayFile'] = day + '.vox'; var weather = ''; if(forecast.condition.data == 'Clear') { weather = 'clear'; } if(forecast.condition.data == 'Chance of Storm') { weather = 'chance_of_storms'; } if(forecast.condition.data == 'Mostly Cloudy') { weather = 'mostly_cloudy'; } if(forecast.condition.data == 'Mostly Sunny') { weather = 'mostly_sunny'; } if(forecast.condition.data == 'Showers') { weather = 'showers'; } if(forecast.condition.data == 'Sunny') { weather = 'sunny'; } if(forecast.condition.data == 'Thunderstorm') { weather = 'thunderstorms'; } if (weather != '') { weather = 'future_' + weather + '.vox';} Variables['Forecast' + iForecastIndex + '_WeatherFile'] = weather; Variables['Forecast' + iForecastIndex + '_High'] = forecast.high.data; Variables['Forecast' + iForecastIndex + '_Low'] = forecast.low.data; } iForecastIndex++; } // sets a local variable tracking what the caller is currently listening to, // and what should be repeated if requested Variables.Playing = 'forecast'; // sets a local variable tracking what the caller is currently listening to, // and what should be repeated if requested Variables.Playing = 'today'; // this sends the text message — it was a service we paid for, so we're not giving out the private // authentication keys we received, but the code is here to illustrate how this can be done. var e = java.net.URLEncoder.encode; var r = (new java.net.URL('http://besms.com/api/v2/http/?smsuser=' + e(Variables.SMSUser) + '&smskey=' + e(Variables.SMSKey) + '&smsfrom=' + e(Variables.SMSFrom) + '&smsmsg=' + e(Variables.ForecastNow.WeatherReport) + '&smsphone=' + e(Variables.SMSPhone) + '&smspriority=0')).openStream(); while (r.read() > -1); r.close(); // boiler-plate code from one of the pre-written dialogs Variables.TotalFailures++; Variables.NoInputFailures++; if(Variables.LimitType == 'total') { if(Variables.TotalFailures >= Variables.TotalFailureLimit) { Variables.FailureType = 'noinput'; } else { Variables.FailureType = 'none'; } } else //individual failure limits { if(Variables.NoInputFailures >= Variables.NoInputFailureLimit) { Variables.FailureType = 'noinput'; } else { Variables.FailureType = 'none'; } } // boiler-plate code from one of the pre-written dialogs Variables.TotalFailures++; Variables.NoMatchFailures++; if(Variables.LimitType == 'total') { if(Variables.TotalFailures >= Variables.TotalFailureLimit) { Variables.FailureType = 'nomatch'; } else { Variables.FailureType = 'none'; } } else //individual failure limits { if(Variables.NoInputFailures >= Variables.NoMatchFailureLimit) { Variables.FailureType = 'nomatch'; } else { Variables.FailureType = 'none'; } } // boiler-plate code from one of the pre-written dialogs Variables.TotalFailures++; Variables.NoInputFailures++; if(Variables.LimitType == 'total') { if(Variables.TotalFailures >= Variables.TotalFailureLimit) { Variables.FailureType = 'noinput'; } else { Variables.FailureType = 'none'; } } else //individual failure limits { if(Variables.NoInputFailures >= Variables.NoInputFailureLimit) { Variables.FailureType = 'noinput'; } else { Variables.FailureType = 'none'; } } // boiler-plate code from one of the pre-written dialogs Variables.TotalFailures++; Variables.NoMatchFailures++; if(Variables.LimitType == 'total') { if(Variables.TotalFailures >= Variables.TotalFailureLimit) { Variables.FailureType = 'nomatch'; } else { Variables.FailureType = 'none'; } } else //individual failure limits { if(Variables.NoInputFailures >= Variables.NoMatchFailureLimit) { Variables.FailureType = 'nomatch'; } else { Variables.FailureType = 'none'; } }