Monday, June 27, 2011

Weather info via Twitter

We wrote already about our Twitter mashup – Twitter 411 service. It is a web application lets you easily create your own Twitter bots. Bot here is an application that receives some requests via Twitter and responds with your own data. You define a form for the request as well as the content for the response. Here is an example for such a bot (stock market).

As a response for the bot within t411 service you can provide any CGI script (e.g. jsp, java servlet, php etc.) on your own site. See again here a complete example (JSP) for stock market bot.

And now let us see a yet another example. Now it is a weather bot.
How does it work? The source data provided by the site yr.no This site contains weather forecast around the world. And what is important, you can pickup weather data in XML or RSS format. So it is a quick deal to build some automation processing. We create a new bot for t411 service – code for the request is w

As a reaction for this bot we've set a JSP file that obtains RSS data from yr.no, presents them in mobile ready form and responds back with a link to the requested forecast. Here is a part of that file:

<div class="content">

<%@ taglib uri="/WEB-INF/taglib374.tld" prefix="w" %>

<w:setLocation url="http://www.yr.no/place/United_Kingdom/England/London/" ttl="36000" id="A"/>

<w:forEachDay feedId="A">

<strong><%=itemTitle%></strong><br/>
<img src="<%=itemImage%>" style="border:0;float:left;padding-right:5px" alt=''/>
<%=itemDescription%><br/><br/>
</w:forEachDay>

<br/>&copy; <w:getChannelCopyright feedId="A"/>
<w:getChannelLink feedId="A" id="url"/>
<br/><a href="<%=url%>">Original data</a><br/>

</div>

code uses the Weather taglib from Coldtags suite. Tag setLocation requests rss feed (and caches it for 10 hours. Tag forEachDay outputs data. And finally we prints a copyright information as well as the back link (as it is required by yr.no)

The rest of the deal is standard. Send to user @t411 a reply or direct message for the weather forecast you are interested in.

Here are examples (what the bot recognizes and what you have to send as reply or direct message to @t411):
w london  get forecast for London
w paris   get forecast for Paris
w msk     get forecast for Moscow
w spb     get forecast for Sankt-Petersburg

And the result (you will get a reply or direct message back) is a link to the mobile site with the forecast. Because it is a mobile site you will be able to open it right from your mobile phone.

The direct messages from Twitter could be delivered by SMS to user's mobile. And so – the mobile link will be delivered too. That was the original idea for t411 service – Twitter could be used as an alternative model for programming SMS services. Twitter is a new transport layer here.

No comments: