EppsNet Labs: VoiceXML RSS Reader

 

The Big Picture

We’re going to build application that takes RSS data — specifically the EppsNet.com feed — as input, and outputs a VXML file that can be read and spoken by a VoiceXML browser.

The RSS Source Format

The general structure of the EppsNet feed — or any RSS 2.0 feed — looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

  <channel>
    <title>EppsNet: Notes from the Golden Orange</title>
    <link>http://eppsnet.com</link>
    <description>Online journal based in Orange County, CA. 
        Hilarious anecdotes tempered by the icy chill of certain 
        death.</description>
    <pubDate>Fri, 05 Oct 2007 03:51:21 +0000</pubDate>
    <generator>http://wordpress.org/?v=2.2.2</generator>
    <language>en</language>
    <item>
      ...
    </item>
    <item>
      ...
    </item>
      ...
  </channel>
</rss>

Each item within the RSS feed has a format that looks (slightly simplified) like this:

    <item>
      <title>Post Title</title>
      <link>http://eppsnet.com/2007/10/post-title</link>
      <pubDate>Fri, 05 Oct 2007 03:51:21 +0000</pubDate>
      <dc:creator>PE</dc:creator>

      <category><!&#91;CDATA&#91;Category1&#93;&#93;></category>

      <category><!&#91;CDATA&#91;Category2&#93;&#93;></category>

      <description>
        <!&#91;CDATA&#91;
        Item summary
        &#93;&#93;>
      </description>
      <content:encoded>
        <!&#91;CDATA&#91;
        Full item content
        &#93;&#93;>
      </content:encoded>
    </item>

VXML Output

Consult the VoiceXML 2.1 specification for more details, but the output we want will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<vxml version="2.1">
  <form id="MainMenu">
    <field name="select_num" type="digits">
      <prompt>
        EppsNet: Notes from the Golden Orange
        <break size="small"/>
      </prompt>
      <prompt>Please select a story from the following list.</prompt>
      <prompt>
        1: Post Title 1
        <break size="small"/>
      </prompt>
      ...
      <prompt>
        5: Post Title 5
        <break size="small"/>
      </prompt>
      <noinput>
        Please select a number.
        <reprompt/>
      </noinput>
      <nomatch>
        Please select a valid number.
        <reprompt/>
      </nomatch>
    </field>
    <filled>
      <assign name="selection" expr="select_num"/>
      <if cond="selection =='1'">
        <prompt>
          Post Title 1. Post summary goes here [...]
          <break size="small"/>
        </prompt>
        ...
        <elseif cond="selection =='5'"/>
        <prompt>
          Post Title 5. Post summary goes here [...]
          <break size="small"/>
        </prompt>
      </if>
      <clear namelist="select_num"/>
      <reprompt/>
    </filled>
  </form>
</vxml>

What this will do when processed by a VoiceXML browser is:

  1. Say the title of the RSS feed.
  2. Offer the listener a numbered list of post titles to select from.
  3. Parse the user’s selection, by either voice or touch-tone input.
  4. Read out the selected post summary.
  5. Clear the input variable and offer the opportunity to select another item.

Generating VoiceXML from RSS

Because this is a WordPress site, we’re going to use PHP for the task of converting RSS input to VXML output. To simplify the task of parsing the input, we’ll use MagpieRSS, an RSS parser written in PHP.

The main loop in the code below processes up to 5 RSS items and simultaneously builds up two strings, one for the selection prompts and one for the item details.

<?php
/* We need this for MagpieRSS */
require_once 'rss_fetch.inc';

/* Read the RSS feed */
$url = 'http://eppsnet.com/feed';
$feed = fetch_rss($url);

$selection = '';
$detail = '';
$counter = 0;

$selection .= '<form id="MainMenu">';
$selection .= '<field name="select_num" type="digits">';
$selection .= '<prompt>';
$selection .= $feed->channel['title'] . '<break size="small"/>';
$selection .= '</prompt>';
$selection .= '<prompt>';
$selection .= 'Please select a story from the following list.';
$selection .= '</prompt>';

foreach ($feed->items as $item ) {
    /* Limit output to 5 items */
    if ($counter++ >= 5)
        break;

    if ($counter == 1)
    {
        $detail .= '<filled>';
        $detail .= '<assign name="selection" expr="select_num"/>';
        $detail .= "<if cond=\"selection =='$counter'\">";
    }
    else
    {
        $detail .= "<elseif cond=\"selection =='$counter'\"/>";
    }
    $detail .= sprintf('<prompt>%s. %s<break size="small"/></prompt>',
$item[title],$item[description]);

    $selection .= sprintf('<prompt>%d: %s<break size="small"/></prompt>',
$counter,$item[title]);
}

$selection .= '<noinput>Please select a number. 
                              <reprompt/></noinput>';
$selection .= '<nomatch>Please select a valid number. 
                              <reprompt/></nomatch>';
$selection .= '</field>';

$detail .= '</if>';
$detail .= '<clear namelist="select_num"/><reprompt/>';
$detail .= '</filled></form>';

/* Output the VXML */
echo '<vxml version="2.1">';
echo $selection;
echo $detail;
echo '</vxml>';
?>

Try It

I put the PHP script at http://eppsnet.com/lab/vxml but the output is not very interesting in a regular browser. Fortunately, Voxeo offers a free service that maps voice applications to phone numbers. You give them the URL of your voice app and they’ll point a phone number to it.

So — if you pick up the phone, call 800-289-5570 and enter PIN 9992002320, the Voxeo application will fetch the VXML output from our PHP script and read selected excerpts from the EppsNet feed to you over the phone.

Try it!

Limitations

The VXML output doesn’t contain the entire contents of each post, just the truncated version from the RSS <description> field. I tried using the <content:encoded> field instead but some markup constructs choked the Voxeo application. I think I could get it to work if I spent enough time on it, but for now, I’ve decided to leave it as an exercise for the reader.

  1 comment for “EppsNet Labs: VoiceXML RSS Reader

  1. 7 Nov 2007 at 4:04 pm

    Now… that’s bitchen!!!
    Good work.

Leave a Reply

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