Friday, October 24, 2008

Cisco Callmanager Express, XML Services

I've played a little bit with the integration of XML Services to the CME. The service is hosted on a Linux system with Apache as a web server.
 Works quite well ...

The necessary CME config:

telephony-service
url services http://172.20.29.88/html/ciscoxml/menu.php


My menu.php:

root@home-nas:/usr/lib/cgi-bin# cat /var/www/html/ciscoxml/menu.php
<ciscoipphonemenu>
 <title>My XML Services</title>
 <prompt>Please make your selection.</prompt>
 <menuitem>
   <name>Telefonbuch</name>
   <url>http://172.20.29.88/html/ciscoxml/Phonebook.php</url>
 </menuitem>
 <menuitem>
   <name>RSS_Feed</name>
   <url>http://172.20.29.88/cgi-bin/rss2cisco.pl</url>
 </menuitem>
 <menuitem>
   <name>Weather, News and Stocks</name>
   <url>http://phone-xml.berbee.com/menu.xml?opts=13456</url>
 </menuitem>
</ciscoipphonemenu>
root@home-nas:/usr/lib/cgi-bin#


My Phonebook.php:

root@home-nas:/usr/lib/cgi-bin# cat /var/www/html/ciscoxml/Phonebook.php
<ciscoipphonedirectory>
 <title>XML Directory</title>
 <prompt>Please Choose:</prompt>
 <directoryentry>
   <name>pepe, schnitzel</name>
   <telephone>01234</telephone>
 </directoryentry>
 <directoryentry>
   <name>pepe, schnatzel</name>
   <telephone>3453456346</telephone>
 </directoryentry>
 <directoryentry>
   <name>pepe, schnetzel</name>
   <telephone>234523</telephone>
 </directoryentry>
</ciscoipphonedirectory>
root@home-nas:/usr/lib/cgi-bin#

My rss2cisco.pl:
You get the original one from http://dontpokebadgers.com/rss2cisco/.
Thank you Joshua!!

root@home-nas:/usr/lib/cgi-bin# cat rss2cisco.pl
#!/usr/bin/perl
use strict;
#use warnings;
use CGI;
use LWP::Simple qw($ua get);
use XML::RSS;
use Cisco::IPPhone;
use vars qw($pathto @descriptions @feeds);
($#descriptions,$#feeds) = (-1,-1);
# RSS2cisco, An RSS feed to Cisco IP Phone Script version 2.0
# Copyright 2007, Joshua Cantara
# This program is licensed under the GPL: http://www.gnu.org/licenses/gpl.txt
# Newest version can always be found at: http://dontpokebadgers.com/rss2cisco/
# ************************************************************
# ATTENTION: EDIT THE FOLLOWING VARIABLES!!
# ************************************************************
# Change the following to the folder location of rss2cisco.pl on your server
# --------> DO NOT ADD A TRAILING SLASH. <--------
$pathto = 'http://172.20.23.88/cgi-bin';
# Add/Remove RSS feeds below. An example is provided.
# --------> REMOVE "http://" <--------
push(@descriptions,'Wetter');
push(@feeds,'wetter.com/wetter_rss/wetter.xml');
push(@descriptions,'BBC World News');
push(@feeds,'newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml');
push(@descriptions,'Cisco Press Promos');
push(@feeds,'www.ciscopress.com/promotions/promotion_rss.asp');
push(@descriptions,'Cisco News');
push(@feeds,'www.channelinsider.com/rss-feeds-1.xml');
push(@descriptions,'Queenstown weather');
push(@feeds,'www.rssweather.com/wx/za//queenstown/rss.php');
# --------> There is a CISCO IMPOSED MAXIMUM of 64 feeds. <--------
# ************************************************************
# ATTENTION: DON'T CHANGE ANYTHING BELOW THIS LINE!
# ************************************************************
my $query = new CGI;  if ($query->param('rssurl') eq '') { &printmenu; }
elsif ($query->param('rssurl') ne '') { &printfeed; }
else { &error; }
exit;
################################################
# PRINT A MENU OF FEEDS
################################################
sub printmenu {
my $xmlmenu = new Cisco::IPPhone;
$xmlmenu->Menu( { Title => "Your RSS Feeds", Prompt => "Choose a Feed", Text => "" });
my $i = 0;
foreach my $item (@descriptions)
 {
 $item = encode_entities($item);
 my $url = "$pathto/rss2cisco.pl?rssurl=$i";
 $url = encode_entities($url);
 $xmlmenu->AddMenuItem({ Name => $item, URL => $url});
 $i++;
 }
print $xmlmenu->Content;
}
################################################
# PRINT A SINGLE RSS FEED
################################################
sub printfeed {
# Get and parse RSS feed
my $query = new CGI;
my $feednum = $query->param('rssurl');
my $rss = new XML::RSS();
my $rssfeed = 'http://' . $feeds[$feednum];
$ua->timeout(15);
my $raw = get($rssfeed);
$rss->parse($raw);
# Read RSS news items and convert
my $body = "";
foreach my $item (@{$rss->{'items'}})
 {
 my $itemtitle = encode_entities($item->{'title'});
 my $itemdescription = $item->{'description'};
 $itemdescription = encode_entities($itemdescription);
 $body .= $itemtitle . "\n------------------------------\n" . $itemdescription . "\n\n";
 }
if (length($body) > 3600)
 {
 $body = substr($body,0,3600);
 $body .= qq|\n------------------------------\n|;
 $body .= qq|Sorry, this feed has exceeded the maximum display size and has been truncated.|;
 }
# Prepare and return final Cisco XML document
my $xmloutput = new Cisco::IPPhone;
my $title = encode_entities($descriptions[$feednum]);
$xmloutput->Text({ Title => $title, Prompt => "Viewing Feed...", Text => $body });
print $xmloutput->Content;
}
###############################################################
# PRINT ERROR MESSAGE
###############################################################
sub error {
my $xmloutput = new Cisco::IPPhone;
$xmloutput->Text({ Title => "Error", Prompt => "Please Go Back", Text => "Sorry, an error has occured." });
print $xmloutput->Content;
}
###############################################################
# ENCODE/DECODE ENTITIES
###############################################################
sub encode_entities {
my $text = shift (@_);
$text =~ s/<(.|\n)+?>//g;
$text =~ s/’/'/g;
$text =~ s/&amp;/&amp;/g;
$text =~ s/<!--</g; $text =~ s/-->/&gt;/g;
return $text;
}
root@home-nas:/usr/lib/cgi-bin#

To get this to work you should follow the instruction on the homepage of the script (http://dontpokebadgers.com/rss2cisco/).
Additionaly, to get the installation of XML::RSS working, I had to install expat-2.0.1. Download the XML parser expat at
http://sourceforge.net/projects/expat/.