redial

Temp via phone

February 20th, 2012 in Interactive Telecommunications by 0 Comments

Assignment:

  • Get familiar with PHP or Ruby programming and try some simple AGI scripting
  • One idea might be to write an AGI script that reads the time and temperature

Response:

  • I’ve decided to continue with PHP, in order to further my study in the language. For the assignment, I assigned four major cities, one per timezone, to the touchpad.
    1. Boston
    2. Minneapolis
    3. Denver
    4. Las Vegas
  • The server will respond by stating the city, the time in said timezone, and weather.

Code:

#!/usr/bin/php -q
answer();

$cities = array();
$cities[1] = array("BOS", "Boston", "http://www.weather.gov/xml/current_obs/KBOS.xml", 0);
$cities[2] = array("MSP", "Minneapolis", "http://www.weather.gov/xml/current_obs/KMSP.xml", 3600);
$cities[3] = array("DEN", "Denver", "http://www.weather.gov/xml/current_obs/KDEN.xml", 7200);
$cities[4] = array("LAS", "Las Vegas", "http://www.weather.gov/xml/current_obs/KLAS.xml", 10800);
$continue = true;

while($continue) {
$agi->stream_file("vm-extension");
$return = $agi->wait_for_digit(-1); // -1 = wait forever
if ($return['result'] >= 0) {
$ascii = chr($return['result']);
} else {
$continue = false;
}
$weatherPage=file_get_contents($cities[$ascii][2]);
$currentTemp = -100;
if (preg_match("/([0-9.-]+)<\/temp_f>/i",$weatherPage,$matches)) {
$currentTemp=$matches[1];
} else {
noop("no match");
}

if ($currentTemp > -100) {
$agi->text2wav($cities[$ascii][1]);
$agi->say_time(time() - $cities[$ascii][3]);
$agi->say_number($currentTemp);
$agi->text2wav("Degrees");
} else {
$continue = false;
}
}

//will print to Asterisk console. Useful for debugging
function noop($message){
echo ("NOOP " . $message . "\n");
}

/* End AGI Scripting */

?>

Author: ezraezra

I see you looking...

Leave a Reply