Weather Report

Building off the weather example in the class notes I created a call in weather report that will give you the time, temperature, humidity, and wind speed in New York.

Here’s the AGI script…

#!/usr/bin/ruby

require ‘rubygems’
require ‘ruby-agi’ #for Asterisk AGI
require ‘net/http’ #for http connections
require ‘rexml/document’ #for parsing XML

agi = AGI.new

weatherURL = “http://www.weather.gov/xml/current_obs/KNYC.xml” #NYC
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(weatherURL)).body
# extract event information
doc = REXML::Document.new(xml_data)
temp_f = doc.get_text(‘current_observation/temp_f’)
relative_humidity = doc.get_text(‘current_observation/relative_humidity’)
wind_mph = doc.get_text(‘current_observation/wind_mph’)

if temp_f
current_temp = temp_f
else
agi.noop(“couldn’t find temp in xml. quitting.”)
continue = false
exit #quit ruby
end
if relative_humidity
current_humidity = relative_humidity
else
agi.noop(“couldn’t find humidity in xml. quitting.”)
continue = false
exit #quit ruby
end
if wind_mph
current_wind = wind_mph
else
agi.noop(“couldn’t find wind in xml. quitting.”)
continue = false
exit #quit ruby
end

t = Time.now.to_i
agi.say_time(t, ‘#’)
agi.stream_file(“/home/mu407/asterisk_sounds/weather_report/weather_report_intro”)
agi.say_number(current_temp)
agi.stream_file(“/home/mu407/asterisk_sounds/weather_report/degrees”)
agi.stream_file(“/home/mu407/asterisk_sounds/weather_report/humidity”)
agi.say_number(current_humidity)
agi.stream_file(“/home/mu407/asterisk_sounds/weather_report/percent”)
agi.stream_file(“/home/mu407/asterisk_sounds/weather_report/wind”)
agi.say_number(current_wind)
agi.stream_file(“/home/mu407/asterisk_sounds/weather_report/miles_per_hour”)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>