Creating your first VoiceXML application – Part2
Hello. In previous part of the post I talked about definitions of VoiceXML and CCXML. Also we created simple static VXML file with weather condition for predefined cities list, deployed and tested it. In this second part we will generate our VXML file dynamically using real weather condition.
Google Weather API
One of the easiest ways to get weather condition and forecast is use Google Weather API. You just need to make the following request:
and you will receive replay in XML format.
I’ve created small PHP class that loads weather condition for specified city. Save it as weather.php
/*
* Load weather condition for specified city using Google Weather API
* Author Oleg Puzanov
*/
class Weather {
private static $_apiUrl = 'http://www.google.com/ig/api?weather=';
public static function get($city) {
$weather = '';
// load weather conditions using Goolge weather api
$xml = simplexml_load_file(self::$_apiUrl . $city);
// if xml loads succsessfully
if ( $xml !== false ) {
// parse xml using SimpleXML
$weather = 'Now in ' . $city . ' is ' . (string)$xml->weather->current_conditions->condition['data'] . ', ';
$weather .= 'temperature is ' . (string)$xml->weather->current_conditions->temp_c['data'] . ' degrees Celsius, ';
$weather .= (string)$xml->weather->current_conditions->humidity['data'];
} else {
// notify customer about current problem
$weather = 'Sorry, could not load weather condition for this city at the moment';
}
return $weather;
}
}
Generate VXML file
Here is our final script. Save it as index.php
// set header to text/xml for browser
header('Content-type: text/xml');
// include previously created class for getting weather
require_once('weather.php');
// define our VXML template with variables %WEATHER_LONDON%, %WEATHER_PARIS% and %WEATHER_MILAN%
$vxml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<vxml version="2.1">
<form id="choiceMenu">
<field name="city">
<prompt>Please, specify your city</prompt>
<!-- Insert an inline grammar -->
<grammar type="text/gsl">
[london paris milan]
</grammar>
<!-- Reprompt user if he doesn't provide an answer -->
<noinput>
Sorry, what did you say?
<reprompt />
</noinput>
<!-- Handle the case when no match is found -->
<nomatch>
Sorry, but such city is not in my list. Please, try again.
<reprompt />
</nomatch>
</field>
<!-- Set our options. -->
<filled namelist="city">
<if cond="city == 'london'">
<prompt>%WEATHER_LONDON%</prompt>
<elseif cond="city == 'paris'" />
<prompt>%WEATHER_PARIS%</prompt>
<elseif cond="city == 'milan'" />
<prompt>%WEATHER_MILAN%</prompt>
</if>
</filled>
</form>
</vxml>
XML;
// replace variables with actual weather condition values
$vxml = str_replace('%WEATHER_LONDON%', Weather::get("London"), $vxml);
$vxml = str_replace('%WEATHER_PARIS%', Weather::get("Paris"), $vxml);
$vxml = str_replace('%WEATHER_MILAN%', Weather::get("Milan"), $vxml);
// output final VXML
echo $vxml;
You can check the final result here – http://puzanov.info/examples/voicexml/index.php.
Setup your application
Let’s setup our application using Voxeo services. To do this create new application using “Account” => “Application manager” menu item (please check previous part which have detailed description with screenshots how to do this) using script that we created as Voice URL.

Test it
You will find application contact methods in special tab

You should hear a female voice prompt “Please, specify your city”. Say “London” for instance and get an answer about current weather condition in London.
Summary
In this short 2 series article I introduced the process of developing telephony applications and familiarized you with two standards – VoiceXML and CCMXL. The W3C’s Speech Interface Framework also defines these other standards closely associated with VoiceXML: Speech Recognition Grammar Specification (SRGS), Speech Synthesis Markup Language (SSML), Pronunciation Lexicon Specification (PLS) and many others.
There many companies that works in this area and provide tools for developers which you could try for free. One of them is Teleku which support different phone markup script languages and provide HTTP RESTFUL XML/JSON. You could try them as alternative to Voxeo.com.
Further Reading
If you are interested in these theme I would recommend you to read great set of articles on IBM developerWorks section.
TweetTagged as google weather api, voicexml, voxeo