Z. Code Samples

These code samples are designed to be fully executable samples of various programming  capabilities.  A single example is enclosed between double lines (=====).  The actual code is enclosed between single lines (------).  In some cases documentation in other sections will refer to sample code that has been placed here.  If this is the case the title block of the sample will contain a reference back to the section that references it.
 
===================================
GROOVY SAMPLES
    Awareness Change Bot
    Date: 10/11/2010
    Reference: "Programmers (API)/B. Groovy Bots/ITFramework Groovy SDK"
--------------------------------------------
import com.instant.applications.bots.*
import com.instant.services.*;
import com.instant.services.im.*;
import groovy.text.Template;
import groovy.text.SimpleTemplateEngine;
import com.instant.applications.bots.*
import com.instant.services.*;
import com.instant.services.im.*;
import com.instant.applications.dynamic.*
import com.instant.applications.*;
import com.instant.services.database.*;
import com.instant.util.*;
public class StatusBot extends MultiServiceBot
{
        def botObjects = [:]
 def botName
 
 
public StatusBot() {
super()
println "StatusBot starting now"
}
public void start()
{
 def myLoader = new ServiceLoader("")
 // This should spin up a agent
 
   botName = "Groovy Awareness"
  //Groovy Awareness
  println "state machine loader is trying to load $botName with--------------- $_LoadUrl"
  def myBot = new StatusBot_process("ST", "st802.instant-tech.com", botName, "password")
  myBot.setLoadUrl(_LoadUrl)
  myBot.setBotUrl(_BotUrl)
  myLoader.loadBotThread(myBot) 
  
    }
 
}
public class StatusBot_process extends MultiServiceBot
{
   String m_serviceType
   String m_serverName
   String m_userName
   String m_password
   def _Initialized = false
   def _LoginStatus = false
public StatusBot_process(serviceType, serverName, userName, password)
 {
 super()
 m_serviceType = serviceType
 m_serverName = serverName
 m_userName = userName
 m_password = password
 _MsgServicesInterface =
      new MsgServices(new ITFSametimeIMService(), new ITFOCSIMService(),
      new ITFXMPPIMService(),new ITFFaceBookIMService()) 
 }

public void start()
{
   println "start Status Bot:::::::::::"
    if (_Initialized==false)
  {
  registerAndLogin(m_serviceType, m_serverName, m_userName, m_password);
   _Initialized=true;
  }
}
public void stop()
 {
  if (_Initialized)
  {  
    getMsgService(m_serviceType).logout()
   _Initialized = false;
   _Logger.info("Stop Status Bot ");
   
    
  } else
  {
   _Logger.info("StatusBot already stopped, check logs if restart is failing");
  }
 }

 void loggedIn(ITFLoginEvent arg0) {
 try {
  _LoginStatus = true
      println "Logged in"
       UpdateBotStatus(arg0.getLoginName(), this, "Groovy Awareness", false, true,"/na");
     } catch (Exception e)
      {
            e.printStackTrace()
        }
    }   
 void loggedOut(ITFLoginEvent loginEvent) {
  _LoginStatus = false
 UpdateBotStatus(m_userName, this, "Groovy Awareness", false, false,"/na");
        println "Logged Out :"+loginEvent.getReason()
        //disconnect(dirContext)
        if(loginEvent.getReason() != 0)
        {
            try
     {
                 Thread.sleep(30000)   //don't attempt login in a tight loop
            }    catch (InterruptedException e) {}           
            registerAndLogin(m_serviceType, m_serverName, m_userName, m_password)
         }
  } 
public void ImReceived(ITFIMEvent ImEvent)
{
  super.ImReceived(ImEvent)
 
  println "Im Recevied.............."
  String msgType = ImEvent.getMsgServiceType()
  MsgServiceInterface myService = getMsgService(msgType)
  myService.sendText(ImEvent.getSenderId(),
  "Welcome to the Status Bot \nSelect Option to change status\n1. active\n2. dnd\n3. away" ,
  ImEvent.getConfigId())
 }
public void ImTextReceived(ITFIMEvent ImEvent)
{
  super.ImTextReceived(ImEvent)
  String msgType = ImEvent.getMsgServiceType()
  MsgServiceInterface myService = getMsgService(msgType)
 if(ImEvent.getText().equalsIgnoreCase("1")){
  myService.ChangeSelfStatus("active")
  myService.sendText(ImEvent.getSenderId(),"Welcome to the Status Bot \nSelect Option to change status\n1. active\n2. dnd\n3. away",ImEvent.getConfigId())
 
 }
 else if(ImEvent.getText().equalsIgnoreCase("2")){
  myService.ChangeSelfStatus("dnd")
  myService.sendText(ImEvent.getSenderId(),"Welcome to the Status Bot \nSelect Option to change status\n1. active\n2. dnd\n3. away",ImEvent.getConfigId())
 
 }
else if(ImEvent.getText().equalsIgnoreCase("3")){
 myService.ChangeSelfStatus("away")
  myService.sendText(ImEvent.getSenderId(), "Welcome to the Status Bot \nSelect Option to change status\n1. active\n2. dnd\n3. away",ImEvent.getConfigId())
 
 }
else{
println("you press unknown option")
  myService.sendText(ImEvent.getSenderId(), ImEvent.getText(),ImEvent.getConfigId())
 
 
 }
}

}
--------------------------------------------
    End of Awareness Change Bot
===================================
 
Comments