Tropo is part of CiscoLearn More

Tropo SMS Wolfram Alpha Mashup

Posted on December 13, 2011 by Team Tropo

Tim Strimple joined us at the LA Holiday Hackathon to get in on the competition of building Tropo applications for prizes and won a $50 Tropo Production credit for his Tropo SMS and Wolfram Alpha mashup!

You can ask the application virtually any question via SMS using the following phone number 661-206-2681 and it responds to your inquiry via SMS using Wolfram Alpha’s search results. Here’s a video of Tim demonstrating his application in action!

Here’s the code behind Tim’s Tropo SMS Wolfram Alpha mashup! It’s written in PHP and uses the Tropo Scripting API.

<?php

function CheckForShortcut($request)
{
if(stripos($request, "siri") !== false)
{
return "I don't like to talk about her.";
}

if(stripos($request, "remind") !== false)
{
return "I am not your personal assistant.";
}

if(stripos($request, "tropo") !== false)
{
return "Tropo is great, I love it!";
}

return false;
}

function ParseResponse($response)
{
// Replace with real XML parsing
$min = strpos($response, "</plaintext>");
$startPos = strpos($response, "<plaintext>", $min) + 11;
$endPos = strpos($response, "</plaintext>", $startPos);
$length = $endPos - $startPos;

if($min > 0)
{
return substr($response, $startPos, $length);
}
else
{
return "Go ask Siri...";
}
}

function GetResults($request)
{
$shortcut = CheckForShortcut($request);

if($shortcut)
{
return $shortcut;
}

$request = str_replace (" ", "%20",$request);
$wolframApiKey = "XXXXXX-XXXXXXXXXX";
$url = "http://api.wolframalpha.com/v2/query?appid=" . $wolframApiKey . "&input=" . $request;
$response = file_get_contents($url);

return ParseResponse($response);
}

if($currentCall->channel == "TEXT")
{
$result = GetResults($currentCall->initialText);
say($result);
}
else
{
say("I do not support voice currently. Try sending me a text message instead.");
}
?>

Happy Hacking!

Leave a Reply