Tuesday, August 11, 2009

Instant Bots - Now with Groovy

Over the years, Instant has developed Sametime bots in Java, .Net, and VB. We have also created OCS bots in VB and .Net. These have all been fun projects.

However, nothing matches the pleasure of developing IM applications using groovy.

Groovy is light weight and just plain fun. I'm especially impressed with XMLSlurper - which makes dealing with XML painless (well, actually productive).

Here is a little weather bot developed in groovy.

1: Let's login to Sametime

public weatherBot() {
super()
registerAndLogin("ST", "stserver.instant-tech.com", "weather bot", "");
}

2: Let's say Hello when we receive an IM:

public void ImReceived(ITFIMEvent ImEvent)
{
super.ImReceived(ImEvent)
String msgType = ImEvent.getMsgServiceType()
String prefix = "Welcome to the Instant Weather bot. Please enter a zipcode:"
MsgServiceInterface myService = getMsgService(msgType)

myService.sendText(ImEvent.getSenderId(),
prefix ,
ImEvent.getConfigId())
}

3: Let's return the weather (with a little help from XMLSlurper)

public void ImTextReceived(ITFIMEvent ImEvent)
{
super.ImTextReceived(ImEvent)
String msgType = ImEvent.getMsgServiceType()
MsgServiceInterface myService = getMsgService(msgType)

def baseUrl = "http://weather.yahooapis.com/forecastrss"
String zip = ""
String outPut = ""

//def zip = '03824'
zip = ImEvent.getText()
def url = baseUrl + "?p=" + zip
def xml = url.toURL().text

def rss = new XmlSlurper().parseText(xml)
outPut = "Your temp is " + rss.channel.item.condition.@temp + " and conditions are: " + rss.channel.item.condition.@text

myService.sendText(ImEvent.getSenderId(),
outPut,
ImEvent.getConfigId())
}

No comments: